Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update the user agent string for twilio-csharp #606

Merged
merged 26 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a56bdad
[DI-1569] modify user agent string format
claudiachua May 9, 2022
e402e30
[DI-1569] remove todo comments
claudiachua May 9, 2022
62b9b0d
remove linq functions as its not supported by net451
May 11, 2022
57f1dff
[DI-1569] remove front extra space from platform version
claudiachua May 11, 2022
f62987d
include twilio.sln
May 12, 2022
341ccc4
Merge branch 'DI-1569' of https://github.com/twilio/twilio-csharp int…
May 12, 2022
882b4a1
[DI-1569] Revert "include twilio.sln"
claudiachua May 12, 2022
02e7c8f
[DI-1569] remove user agent extensions from rest constructor
claudiachua May 12, 2022
a8f6a10
[DI-1569] Remove User Agent Extensions from Request object constructor
claudiachua May 13, 2022
5528c8c
[DI-1569] debug: commenting off UA extensions functionality
claudiachua May 13, 2022
887efdf
[DI-1569] debug: remove osArch
claudiachua May 13, 2022
2dde200
Revert "[DI-1569] debug: remove osArch"
claudiachua May 13, 2022
b4b0a22
[DI-1569] debug: comment off osArch to find test errors
claudiachua May 13, 2022
f302c06
[DI-1569] modify test case for WebRequestClient
claudiachua May 13, 2022
45366e0
[DI-1569] simplify WebRequestClient
claudiachua May 13, 2022
d8fad21
[DI-1569] debug: modify osArch to return Unknown if processor is not …
claudiachua May 13, 2022
ff06ba6
[DI-1569] adding back user-agent extensions functionality
claudiachua May 13, 2022
0d41ca9
[DI-1569] adding useragentextension backto request constructor
claudiachua May 13, 2022
b6033a0
[DI-1569] add useragentextension as class variables in restclient and…
claudiachua May 13, 2022
c11c4c3
[DI-1569] add test to RestClient
claudiachua May 13, 2022
945cc06
[DI-1569] add RequestAsync test for RestClient
claudiachua May 13, 2022
1e5f672
[DI-1569] move WebRequestClientTest to NET35 folder
claudiachua May 13, 2022
cc0633a
Revert "[DI-1569] move WebRequestClientTest to NET35 folder"
claudiachua May 13, 2022
e1427ed
[DI-1569] edits from code reviews
claudiachua May 18, 2022
c1afc1f
[DI-1569] use linq lib functions
claudiachua May 18, 2022
64b8f05
Merge branch 'main' into DI-1569
claudiachua May 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Twilio/Clients/TwilioRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class TwilioRestClient : ITwilioRestClient
/// </summary>
public string Edge { get; set; }

/// <summary>
/// Additions to the user agent string
/// </summary>
public string[] UserAgentExtensions { get; set; }

/// <summary>
/// Log level for logging
/// </summary>
Expand All @@ -57,13 +62,15 @@ public class TwilioRestClient : ITwilioRestClient
/// <param name="region">region to make requests for</param>
/// <param name="httpClient">http client used to make the requests</param>
/// <param name="edge">edge to make requests for</param>
/// <param name="userAgentExtensions">Additions to the user agent string</param>
public TwilioRestClient(
string username,
string password,
string accountSid = null,
string region = null,
HttpClient httpClient = null,
string edge = null
string edge = null,
string[] userAgentExtensions = null
claudiachua marked this conversation as resolved.
Show resolved Hide resolved
)
{
_username = username;
Expand All @@ -74,6 +81,7 @@ public TwilioRestClient(

Region = region;
Edge = edge;
UserAgentExtensions = userAgentExtensions;
}

/// <summary>
Expand All @@ -95,6 +103,9 @@ public Response Request(Request request)
if (Edge != null)
request.Edge = Edge;

if (UserAgentExtensions != null)
request.UserAgentExtensions = UserAgentExtensions;

Response response;
try
{
Expand Down Expand Up @@ -132,6 +143,9 @@ public async Task<Response> RequestAsync(Request request)
if (Edge != null)
request.Edge = Edge;

if (UserAgentExtensions != null)
request.UserAgentExtensions = UserAgentExtensions;

Response response;
try
{
Expand Down
21 changes: 19 additions & 2 deletions src/Twilio/Http/Net35/WebRequestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Twilio.Http.Net35
/// </summary>
public class WebRequestClient : HttpClient
{
private const string PlatVersion = " (.NET 3.5)";
private const string PlatVersion = ".NET 3.5";
private HttpWebRequestFactory factory;

public WebRequestClient(HttpWebRequestFactory factory = null)
Expand Down Expand Up @@ -63,7 +63,24 @@ private IHttpWebRequestWrapper BuildHttpRequest(Request request)
{
IHttpWebRequestWrapper httpRequest = this.factory.Create(request.ConstructUrl());

httpRequest.UserAgent = "twilio-csharp/" + AssemblyInfomation.AssemblyInformationalVersion + PlatVersion;
int lastSpaceIndex = PlatVersion.LastIndexOf(" ");
System.Text.StringBuilder PlatVersionSb= new System.Text.StringBuilder(PlatVersion);
PlatVersionSb[lastSpaceIndex] = '/';

string helperLibVersion = AssemblyInfomation.AssemblyInformationalVersion;
string osName = System.Environment.OSVersion.Platform.ToString();
string osArch = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE").ToString();
var libraryVersion = System.String.Format("twilio-csharp/{0} ({1} {2}) {3}", helperLibVersion,osName,osArch,PlatVersionSb);

if (request.UserAgentExtensions != null)
{
foreach (var extension in request.UserAgentExtensions)
{
libraryVersion += " " + extension;
}
}

httpRequest.UserAgent = libraryVersion;

httpRequest.Method = request.Method.ToString();
httpRequest.Accept = "application/json";
Expand Down
11 changes: 10 additions & 1 deletion src/Twilio/Http/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public class Request
/// </summary>
public string Edge { get; set; }

/// <summary>
/// Additions to the user agent string
/// </summary>
public string[] UserAgentExtensions { get; set; }

/// <summary>
/// Query params
/// </summary>
Expand Down Expand Up @@ -86,6 +91,8 @@ public Request(HttpMethod method, string url)
/// <param name="postParams">Post data</param>
/// <param name="edge">Twilio edge</param>
/// <param name="headerParams">Custom header data</param>
/// <param name="userAgentExtensions">Additions to the user agent string</param>

claudiachua marked this conversation as resolved.
Show resolved Hide resolved
public Request(
HttpMethod method,
Domain domain,
Expand All @@ -94,13 +101,15 @@ public Request(
List<KeyValuePair<string, string>> queryParams = null,
List<KeyValuePair<string, string>> postParams = null,
string edge = null,
List<KeyValuePair<string, string>> headerParams = null
List<KeyValuePair<string, string>> headerParams = null,
string[] userAgentExtensions = null
)
{
Method = method;
Uri = new Uri("https://" + domain + ".twilio.com" + uri);
Region = region;
Edge = edge;
UserAgentExtensions = userAgentExtensions;

QueryParams = queryParams ?? new List<KeyValuePair<string, string>>();
PostParams = postParams ?? new List<KeyValuePair<string, string>>();
Expand Down
44 changes: 41 additions & 3 deletions src/Twilio/Http/SystemNetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace Twilio.Http
public class SystemNetHttpClient : HttpClient
{
#if NET451
private string PlatVersion = " (.NET Framework 4.5.1+)";
private string PlatVersion = ".NET Framework 4.5.1+";
#else
private string PlatVersion = $" ({RuntimeInformation.FrameworkDescription})";
private string PlatVersion = $"{RuntimeInformation.FrameworkDescription}";
claudiachua marked this conversation as resolved.
Show resolved Hide resolved
#endif

private readonly System.Net.Http.HttpClient _httpClient;
Expand Down Expand Up @@ -91,7 +91,45 @@ private HttpRequestMessage BuildHttpRequest(Request request)
httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpRequest.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));

var libraryVersion = "twilio-csharp/" + AssemblyInfomation.AssemblyInformationalVersion + PlatVersion;
int lastSpaceIndex = PlatVersion.LastIndexOf(" ");
System.Text.StringBuilder PlatVersionSb= new System.Text.StringBuilder(PlatVersion);
PlatVersionSb[lastSpaceIndex] = '/';

string helperLibVersion = AssemblyInfomation.AssemblyInformationalVersion;

string osName = "Unknown";
#if !NETSTANDARD1_4
osName = Environment.OSVersion.Platform.ToString();
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
osName = "Windows";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
osName = "MacOS";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
osName = "Linux";
}
#endif
string osArch = "Unknown";
#if !NET451
osArch = RuntimeInformation.OSArchitecture.ToString();
#else
osArch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE").ToString();
#endif
var libraryVersion = String.Format("twilio-csharp/{0} ({1} {2}) {3}", helperLibVersion,osName,osArch,PlatVersionSb);

if (request.UserAgentExtensions != null)
{
foreach (var extension in request.UserAgentExtensions)
{
libraryVersion += " " + extension;
}
}

httpRequest.Headers.TryAddWithoutValidation("User-Agent", libraryVersion);

foreach (var header in request.HeaderParams)
Expand Down
25 changes: 24 additions & 1 deletion test/Twilio.Test/Http/SystemNetHttpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Twilio.Http;
using HttpMethod = Twilio.Http.HttpMethod;
Expand Down Expand Up @@ -237,7 +238,29 @@ public void TestMakeRequestAddsHeadersAndUserAgent()
Assert.AreEqual("utf-8", internalRequest.Headers.AcceptEncoding.ToString());

Assert.IsNotNull(internalRequest.Headers.UserAgent);
StringAssert.StartsWith("twilio-csharp/", internalRequest.Headers.UserAgent.ToString());
Regex rgx = new Regex(@"^twilio-csharp/[0-9.]+\s\(\w+\s\w+\)\s[.\s\w]+/[^\s]+$");
Assert.IsTrue(rgx.IsMatch(internalRequest.Headers.UserAgent.ToString()));
}

[Test]
public void TestMakeRequestAddUserAgentExtensions()
{
string[] userAgentExtensions = new string[] { "twilio-run/2.0.0-test", "flex-plugin/3.4.0" };

this._mockHttp.Respond("https://api.twilio.com/v1/Resource.json", HttpStatusCode.OK);

Request testRequest = new Request(HttpMethod.Get, "https://api.twilio.com/v1/Resource.json");
testRequest.UserAgentExtensions = userAgentExtensions;
testRequest.SetAuth("username", "password");

this.TwilioHttpClient.MakeRequest(testRequest);

HttpRequestMessage internalRequest = this._mockHttp.InternalRequest;
string userAgent = internalRequest.Headers.UserAgent.ToString();
var actualUserAgent = userAgent.Split(' ');
var actualUserAgentExtensions = new string[userAgentExtensions.Length];
Array.Copy(actualUserAgent, actualUserAgent.Length - userAgentExtensions.Length, actualUserAgentExtensions, 0, userAgentExtensions.Length);
CollectionAssert.AreEqual(userAgentExtensions, actualUserAgentExtensions);
}
}
}
Expand Down