Skip to content

Commit

Permalink
Implement custom user-agent value (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Jun 20, 2023
1 parent 9f20eaf commit 5fac52d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions OpenTok/OpenTok.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public partial class OpenTok
/// </summary>
public HttpClient Client { internal get; set; }

/// <summary>
/// Sets a custom user-agent value. The HttpClient will append this value, if it exists, to the initial user-agent value.
/// </summary>
/// <param name="value">The custom user-agent value.</param>
public void SetCustomUserAgent(string value) => this.Client.CustomUserAgent = value;

private bool _debug;

private IEnumerable<string> _validResolutions = new[]
Expand Down
9 changes: 8 additions & 1 deletion OpenTok/Util/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class HttpClient
1970, 1, 1, 0, 0, 0, DateTimeKind.Utc
);

/// <summary>
/// The custom user-agent value. The HttpClient will append this value, if it exists, to the initial user-agent value.
/// </summary>
public string CustomUserAgent { get; internal set; }

internal HttpClient()
{
}
Expand Down Expand Up @@ -300,7 +305,9 @@ private HttpWebRequest CreateRequest(string url, Dictionary<string, string> 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"];
Expand Down
2 changes: 1 addition & 1 deletion OpenTokTest/OpenTokTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="Enums.NET" Version="4.0.0" />
<PackageReference Include="Moq" Version="4.14.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4"/>
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
Expand Down
19 changes: 19 additions & 0 deletions OpenTokTest/OpenTokTests.cs
Original file line number Diff line number Diff line change
@@ -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<string>();
var sut = new OpenTok(fixture.Create<int>(), fixture.Create<string>());
sut.SetCustomUserAgent(customUserAgent);
Assert.Equal(customUserAgent, sut.Client.CustomUserAgent);
}
}
}
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fac52d

Please sign in to comment.