From 5fac52d6004d11cb9a1b9e2e467e4f9a9df0a152 Mon Sep 17 00:00:00 2001 From: Guillaume Faas <59444272+Tr00d@users.noreply.github.com> Date: Tue, 20 Jun 2023 14:34:20 +0200 Subject: [PATCH] Implement custom user-agent value (#219) --- OpenTok/OpenTok.cs | 6 ++++++ OpenTok/Util/HttpClient.cs | 9 ++++++++- OpenTokTest/OpenTokTest.csproj | 2 +- OpenTokTest/OpenTokTests.cs | 19 +++++++++++++++++++ README.md | 11 +++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 OpenTokTest/OpenTokTests.cs diff --git a/OpenTok/OpenTok.cs b/OpenTok/OpenTok.cs index 976e957e..0d5a21eb 100644 --- a/OpenTok/OpenTok.cs +++ b/OpenTok/OpenTok.cs @@ -34,6 +34,12 @@ public partial class OpenTok /// public HttpClient Client { internal get; set; } + /// + /// Sets a custom user-agent value. The HttpClient will append this value, if it exists, to the initial user-agent value. + /// + /// The custom user-agent value. + public void SetCustomUserAgent(string value) => this.Client.CustomUserAgent = value; + private bool _debug; private IEnumerable _validResolutions = new[] diff --git a/OpenTok/Util/HttpClient.cs b/OpenTok/Util/HttpClient.cs index c53cafc0..c1d8618d 100644 --- a/OpenTok/Util/HttpClient.cs +++ b/OpenTok/Util/HttpClient.cs @@ -30,6 +30,11 @@ public class HttpClient 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc ); + /// + /// The custom user-agent value. The HttpClient will append this value, if it exists, to the initial user-agent value. + /// + public string CustomUserAgent { get; internal set; } + internal HttpClient() { } @@ -300,7 +305,9 @@ private HttpWebRequest CreateRequest(string url, Dictionary head } request.ContentLength = data.Length; - request.UserAgent = OpenTokVersion.GetVersion(); + request.UserAgent = this.CustomUserAgent != null + ? $"{OpenTokVersion.GetVersion()}/{this.CustomUserAgent}" + : OpenTokVersion.GetVersion(); if (headers.ContainsKey("Content-Type")) { request.ContentType = headers["Content-Type"]; diff --git a/OpenTokTest/OpenTokTest.csproj b/OpenTokTest/OpenTokTest.csproj index 8d0efa45..3b9994f9 100644 --- a/OpenTokTest/OpenTokTest.csproj +++ b/OpenTokTest/OpenTokTest.csproj @@ -289,7 +289,7 @@ - + diff --git a/OpenTokTest/OpenTokTests.cs b/OpenTokTest/OpenTokTests.cs new file mode 100644 index 00000000..9fb69277 --- /dev/null +++ b/OpenTokTest/OpenTokTests.cs @@ -0,0 +1,19 @@ +using AutoFixture; +using OpenTokSDK; +using Xunit; + +namespace OpenTokSDKTest +{ + public class OpenTokTests + { + [Fact] + public void SetCustomUserAgent_ShouldSetUserAgentOnClient() + { + var fixture = new Fixture(); + var customUserAgent = fixture.Create(); + var sut = new OpenTok(fixture.Create(), fixture.Create()); + sut.SetCustomUserAgent(customUserAgent); + Assert.Equal(customUserAgent, sut.Client.CustomUserAgent); + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index e48a6412..afdb18d8 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,17 @@ string ApiSecret = "YOUR API SECRET"; var OpenTok = new OpenTok(ApiKey, ApiSecret); ``` +#### Overriding the request's user-agent value + +You can decide to add a custom value to the user-agent value passed with every request. + +```csharp +var OpenTok = new OpenTok(ApiKey, ApiSecret); +OpenTok.SetCustomUserAgent(customUserAgent); +``` + +If the custom value has been set, the user-agent will comply to the following format: `Opentok-DotNet-SDK/{version}/{customValue}`. + ### Creating Sessions To create an OpenTok Session, call the `OpenTok` instance's