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

[DEVX-6967] Custom user-agent #219

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 @@ -52,6 +52,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