Skip to content

Commit

Permalink
sharing variables between clients
Browse files Browse the repository at this point in the history
  • Loading branch information
AsabuHere committed Aug 12, 2024
1 parent a2a72e6 commit e3f4868
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 49 deletions.
53 changes: 30 additions & 23 deletions src/Twilio/Base/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,43 @@ public static Page<T> FromJson(string recordKey, string json)
var parsedRecords = records.Children().Select(
record => JsonConvert.DeserializeObject<T>(record.ToString())
).ToList();

if(root["uri"] != null){
var uriNode = root["uri"];
if (uriNode != null)
{
JToken pageSize;
JToken firstPageUri;
JToken nextPageUri;
JToken previousPageUri;

var uriNode = root["uri"];
if (uriNode != null)
{
JToken pageSize;
JToken firstPageUri;
JToken nextPageUri;
JToken previousPageUri;
// v2010 API
return new Page<T>(
parsedRecords,
root.TryGetValue("page_size", out pageSize) ? root["page_size"].Value<int>() : parsedRecords.Count,
uri: uriNode.Value<string>(),
firstPageUri: root.TryGetValue("first_page_uri", out firstPageUri) ? root["first_page_uri"].Value<string>() : null,
nextPageUri: root.TryGetValue("next_page_uri", out nextPageUri) ? root["next_page_uri"].Value<string>() : null,
previousPageUri: root.TryGetValue("previous_page_uri", out previousPageUri) ? root["previous_page_uri"].Value<string>() : null
);
}
}

// v2010 API
// next-gen API
if(root["meta"] != null){
var meta = root["meta"];
return new Page<T>(
parsedRecords,
root.TryGetValue("page_size", out pageSize) ? root["page_size"].Value<int>() : parsedRecords.Count,
uri: uriNode.Value<string>(),
firstPageUri: root.TryGetValue("first_page_uri", out firstPageUri) ? root["first_page_uri"].Value<string>() : null,
nextPageUri: root.TryGetValue("next_page_uri", out nextPageUri) ? root["next_page_uri"].Value<string>() : null,
previousPageUri: root.TryGetValue("previous_page_uri", out previousPageUri) ? root["previous_page_uri"].Value<string>() : null
meta["page_size"].Value<int>(),
url: meta["url"].Value<string>(),
firstPageUrl: meta["first_page_url"].Value<string>(),
nextPageUrl: meta["next_page_url"].Value<string>(),
previousPageUrl: meta["previous_page_url"].Value<string>()
);
}

// next-gen API
var meta = root["meta"];
return new Page<T>(
parsedRecords,
meta["page_size"].Value<int>(),
url: meta["url"].Value<string>(),
firstPageUrl: meta["first_page_url"].Value<string>(),
nextPageUrl: meta["next_page_url"].Value<string>(),
previousPageUrl: meta["previous_page_url"].Value<string>()
);
return new Page<T>(parsedRecords, 0, null, null, null, null);

}
}
}
45 changes: 45 additions & 0 deletions src/Twilio/ClientProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Twilio.Clients;
using Twilio.Clients.BearerToken;
using Twilio.Exceptions;
using Twilio.Http.BearerToken;

namespace Twilio
{
public class ClientProperties
{
public static string region;
public static string edge;
public static string logLevel;

public ClientProperties() { }


/// <summary>
/// Set the client region
/// </summary>
/// <param name="region">Client region</param>
public static void SetRegion(string region)
{
region = region;

Check warning on line 23 in src/Twilio/ClientProperties.cs

View workflow job for this annotation

GitHub Actions / Test

Assignment made to same variable; did you mean to assign something else?
}

/// <summary>
/// Set the client edge
/// </summary>
/// <param name="edge">Client edge</param>
public static void SetEdge(string edge)
{
edge = edge;

Check warning on line 32 in src/Twilio/ClientProperties.cs

View workflow job for this annotation

GitHub Actions / Test

Assignment made to same variable; did you mean to assign something else?
}

/// <summary>
/// Set the logging level
/// </summary>
/// <param name="loglevel">log level</param>
public static void SetLogLevel(string loglevel)
{
logLevel = loglevel;
}

}
}
16 changes: 8 additions & 8 deletions src/Twilio/Clients/BearerToken/TwilioBearerTokenRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,11 @@ public bool tokenExpired(String accessToken){
public Response Request(BearerTokenRequest request)
{
if ((_accessToken == null )|| tokenExpired(_accessToken)) {
throw new ApiConnectionException("Token expired");
// lock (lockObject){
// if ((_accessToken == null) || tokenExpired(_accessToken)) {
// _accessToken = _tokenManager.fetchAccessToken();
// }
// }
lock (lockObject){
if ((_accessToken == null) || tokenExpired(_accessToken)) {
_accessToken = _tokenManager.fetchAccessToken();
}
}
}
request.SetAuth(_accessToken);

Expand Down Expand Up @@ -280,6 +279,7 @@ private static Response ProcessResponse(Response response)
{
throw new ApiConnectionException("Connection Error: No response received.");
}
throw new ApiConnectionException("response.StatusCode" + response.StatusCode);

if (response.StatusCode >= HttpStatusCode.OK && response.StatusCode < HttpStatusCode.BadRequest)

Check warning on line 284 in src/Twilio/Clients/BearerToken/TwilioBearerTokenRestClient.cs

View workflow job for this annotation

GitHub Actions / Test

Unreachable code detected
{
Expand All @@ -298,11 +298,11 @@ private static Response ProcessResponse(Response response)
{
throw new ApiException("Api Error: " + response.StatusCode + " - " + (response.Content ?? "[no content]"));
}

//"Unable to make request with, "
throw new ApiException(
restException.Code,
(int)response.StatusCode,
restException.Message ?? "Unable to make request, " + response.StatusCode,
restException.Message ?? response.StatusCode.ToString(),
restException.MoreInfo,
restException.Details
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private HttpRequestMessage BuildHttpRequest(BearerTokenRequest request)
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authBytes);

httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/scim+json"));
httpRequest.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));

int lastSpaceIndex = PlatVersion.LastIndexOf(" ");
Expand Down
2 changes: 1 addition & 1 deletion src/Twilio/Rest/PreviewIam/Organizations/UserResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ public static UserResource FromJson(string json)
}
catch (JsonException e)
{
throw new ApiException(e.Message, e);
throw new ApiException(e.Message, e);
}
}
/// <summary>
Expand Down
18 changes: 4 additions & 14 deletions src/Twilio/TwilioNoAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class TwilioNoAuthClient
private static string _edge;
private static TwilioNoAuthRestClient _restClient;
private static string _logLevel;
private static ClientProperties clientProperties;

private TwilioNoAuthClient() { }

Expand All @@ -28,27 +29,16 @@ public static TwilioNoAuthRestClient GetRestClient()
return _restClient;
}

if(_region == null && ClientProperties.region != null) _region = ClientProperties.region;
if(_edge == null && ClientProperties.edge != null) _edge = ClientProperties.edge;
if(_logLevel == null && ClientProperties.logLevel != null) _logLevel = ClientProperties.logLevel;
_restClient = new TwilioNoAuthRestClient(region: _region, edge: _edge)
{
LogLevel = _logLevel
};
return _restClient;
}

/// <summary>
/// Set the logging level
/// </summary>
/// <param name="loglevel">log level</param>
public static void SetLogLevel(string loglevel)
{
if (loglevel != _logLevel)
{
Invalidate();
}

_logLevel = loglevel;
}

/// <summary>
/// Set the rest client
/// </summary>
Expand Down
7 changes: 4 additions & 3 deletions src/Twilio/TwilioOrgsTokenAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class TwilioOrgsTokenAuthClient
private static TwilioBearerTokenRestClient _restClient;
private static string _logLevel;
private static TokenManager _tokenManager;
private static ClientProperties clientProperties;

Check warning on line 19 in src/Twilio/TwilioOrgsTokenAuth.cs

View workflow job for this annotation

GitHub Actions / Test

The field 'TwilioOrgsTokenAuthClient.clientProperties' is never used

private TwilioOrgsTokenAuthClient() { }

Expand Down Expand Up @@ -74,7 +75,7 @@ public static void SetRegion(string region)
{
Invalidate();
}

ClientProperties.SetRegion(region);
_region = region;
}

Expand All @@ -88,7 +89,7 @@ public static void SetEdge(string edge)
{
Invalidate();
}

ClientProperties.SetEdge(edge);
_edge = edge;
}

Expand All @@ -102,7 +103,7 @@ public static void SetLogLevel(string loglevel)
{
Invalidate();
}

ClientProperties.SetLogLevel(_logLevel);
_logLevel = loglevel;
}

Expand Down

0 comments on commit e3f4868

Please sign in to comment.