Skip to content

Commit

Permalink
Implement user-agent support. Fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
smithrobs committed Nov 5, 2016
1 parent 86a8544 commit 701b7b7
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Nexmo.Api.Test.Integration/Nexmo.Api.Test.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
<HintPath>..\packages\Microsoft.Extensions.FileSystemGlobbing.1.0.0\lib\net451\Microsoft.Extensions.FileSystemGlobbing.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.Logging.Abstractions.1.0.0\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Extensions.Primitives, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.Primitives.1.0.0\lib\netstandard1.0\Microsoft.Extensions.Primitives.dll</HintPath>
<Private>True</Private>
Expand All @@ -92,7 +96,7 @@
<Private>True</Private>
</Reference>
<Reference Include="Nexmo.Api, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nexmo.Csharp.Client.2.0.0\lib\net452\Nexmo.Api.dll</HintPath>
<HintPath>..\packages\Nexmo.Csharp.Client.2.1.0\lib\net452\Nexmo.Api.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=3.2.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
Expand Down
4 changes: 4 additions & 0 deletions Nexmo.Api.Test.Integration/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
<package id="Microsoft.Extensions.FileProviders.Abstractions" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.FileProviders.Physical" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.FileSystemGlobbing" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.Primitives" version="1.0.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="Nexmo.Csharp.Client" version="2.1.0" targetFramework="net452" />
<package id="NUnit" version="3.2.0" targetFramework="net452" />
<package id="System.Collections" version="4.0.11" targetFramework="net452" />
<package id="System.Collections.Concurrent" version="4.0.12" targetFramework="net452" />
<package id="System.Diagnostics.Debug" version="4.0.11" targetFramework="net452" />
<package id="System.Globalization" version="4.0.11" targetFramework="net452" />
<package id="System.IO" version="4.1.0" targetFramework="net452" />
<package id="System.Linq" version="4.1.0" targetFramework="net452" />
<package id="System.Net.Http" version="4.0.0" targetFramework="net452" />
<package id="System.Reflection" version="4.1.0" targetFramework="net452" />
<package id="System.Resources.ResourceManager" version="4.0.1" targetFramework="net452" />
<package id="System.Runtime" version="4.1.0" targetFramework="net452" />
<package id="System.Runtime.Extensions" version="4.1.0" targetFramework="net452" />
Expand Down
1 change: 1 addition & 0 deletions Nexmo.Api.Test.Unit/settings.json.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"appSettings": {
"Nexmo.UserAgent": "myApp/1.0",
"Nexmo.Url.Rest": "https://rest.nexmo.com",
"Nexmo.Url.Api": "https://api.nexmo.com",
"Nexmo.api_key": "deadbeef",
Expand Down
2 changes: 1 addition & 1 deletion Nexmo.Api/Nexmo.Api.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency id="Microsoft.Extensions.Configuration.Json" version="1.0.*" />
<dependency id="Microsoft.Extensions.Logging.Abstractions" version="1.0.*" />
<dependency id="System.Net.Http" version="4.*" />
<dependency id="System.Runtime.Extensions" version="4.*" />
<dependency id="System.Runtime.Extensions" version="4.1.*" />

<dependency id="Newtonsoft.Json" version="9.0.*" />
<dependency id="jose-jwt" version="2.0.*" />
Expand Down
2 changes: 2 additions & 0 deletions Nexmo.Api/Request/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public static string DoRequest(Uri uri)
RequestUri = uri,
Method = HttpMethod.Get,
};
VersionedApiRequest.SetUserAgent(ref req);

var sendTask = Configuration.Instance.Client.SendAsync(req);
sendTask.Wait();
Expand Down Expand Up @@ -124,6 +125,7 @@ private static NexmoResponse DoRequest(string method, Uri uri, Dictionary<string
RequestUri = uri,
Method = new HttpMethod(method),
};
VersionedApiRequest.SetUserAgent(ref req);

var data = Encoding.ASCII.GetBytes(sb.ToString());
req.Content = new ByteArrayContent(data);
Expand Down
41 changes: 41 additions & 0 deletions Nexmo.Api/Request/VersionedApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using Newtonsoft.Json;
using System.Net.Http;
using System.Reflection;

namespace Nexmo.Api.Request
{
Expand All @@ -28,6 +29,7 @@ private static string DoRequest(Uri uri)
RequestUri = uri,
Method = HttpMethod.Get,
};
SetUserAgent(ref req);
// attempt bearer token auth
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",
Jwt.CreateToken(Configuration.Instance.Settings["appSettings:Nexmo.Application.Id"], Configuration.Instance.Settings["appSettings:Nexmo.Application.Key"]));
Expand All @@ -48,13 +50,52 @@ private static string DoRequest(Uri uri)
return json;
}

private static string _userAgent;
public static void SetUserAgent(ref HttpRequestMessage request)
{
if (string.IsNullOrEmpty(_userAgent))
{
#if NETSTANDARD1_6
// TODO: watch the next core release; may have functionality to make this cleaner
var runtimeVersion = (System.Runtime.InteropServices.RuntimeInformation.OSDescription + System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription)
.Replace(" ", "")
.Replace("/", "")
.Replace(":", "")
.Replace(";", "")
.Replace("_", "")
;
#else
var runtimeVersion = System.Diagnostics.FileVersionInfo
.GetVersionInfo(typeof(int).Assembly.Location)
.ProductVersion;
#endif
var libraryVersion = typeof(VersionedApiRequest)
.GetTypeInfo()
.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;

_userAgent = $"nexmo-dotnet/{libraryVersion} dotnet/{runtimeVersion}";

var appVersion = Configuration.Instance.Settings["appSettings:Nexmo.UserAgent"];
if (!string.IsNullOrWhiteSpace(appVersion))
{
_userAgent += $" {appVersion}";
}
Console.Write("#");
}

request.Headers.UserAgent.ParseAdd(_userAgent);
}

public static NexmoResponse DoRequest(string method, Uri uri, object payload)
{
var req = new HttpRequestMessage
{
RequestUri = uri,
Method = new HttpMethod(method),
};
SetUserAgent(ref req);
// attempt bearer token auth
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",
Jwt.CreateToken(Configuration.Instance.Settings["appSettings:Nexmo.Application.Id"], Configuration.Instance.Settings["appSettings:Nexmo.Application.Key"]));
Expand Down
2 changes: 1 addition & 1 deletion Nexmo.Api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"Microsoft.Extensions.Logging": "1.0.*",
"System.Collections.Immutable": "1.2.*",
"System.Net.Http": "4.*",
"System.Runtime.Extensions": "4.*",
"System.Runtime.Extensions": "4.1.*",

"Newtonsoft.Json": "9.0.*",
"jose-jwt": "2.0.*"
Expand Down
1 change: 1 addition & 0 deletions Nexmo.Samples.Voice.FirstTTS/settings.json.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"appSettings": {
"Nexmo.UserAgent": "myApp/1.0",
"Nexmo.Url.Rest": "https://rest.nexmo.com",
"Nexmo.Url.Api": "https://api.nexmo.com",
"Nexmo.Application.Id": "ffffffff-ffff-ffff-ffff-ffffffffffff",
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Configuration:
```json
{
"appSettings": {
"Nexmo.UserAgent": "myApp/1.0",
"Nexmo.Url.Rest": "https://rest.nexmo.com",
"Nexmo.Url.Api": "https://api.nexmo.com",
"Nexmo.api_key": "<YOUR KEY>",
Expand All @@ -51,6 +52,7 @@ Configuration:

```xml
<appSettings>
<add key="Nexmo.UserAgent" value="myApp/1.0" />
<add key="Nexmo.Url.Rest" value="https://rest.nexmo.com" />
<add key="Nexmo.Url.Api" value="https://api.nexmo.com" />
<add key="Nexmo.api_key" value="<YOUR KEY>" />
Expand Down

0 comments on commit 701b7b7

Please sign in to comment.