Skip to content

Commit

Permalink
Merge pull request #25 from yrmartinez/issue-24-add-sdk-usage-header
Browse files Browse the repository at this point in the history
Issue 24 add sdk usage header
  • Loading branch information
Simply007 authored Oct 5, 2018
2 parents 1e8cf8f + 19caeac commit 546c062
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System.Net;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

using KenticoCloud.ContentManagement.Exceptions;
using KenticoCloud.ContentManagement.Modules.HttpClient;
using KenticoCloud.ContentManagement.Modules.Extensions;

using NSubstitute;
using Xunit;
using System.Collections.Generic;

namespace KenticoCloud.ContentManagement.Tests
{
Expand All @@ -22,6 +25,27 @@ public ContentManagementHttpClientTests()
_client = new ContentManagementHttpClient(delay, httpClient);
}

[Fact]
public void CorrectSdkVersionHeaderAdded()
{
// Assamble
var assembly = typeof(ContentManagementHttpClient).Assembly;
var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
var sdkVersion = fileVersionInfo.ProductVersion;
var sdkPackageId = assembly.GetName().Name;

var httpRequestMessage = new HttpRequestMessage();

// Act
httpRequestMessage.Headers.AddSdkTrackingHeader();
IEnumerable<string> headerContent = new List<string>();
httpRequestMessage.Headers.TryGetValues("X-KC-SDKID", out headerContent);

// Assert
Assert.True(httpRequestMessage.Headers.Contains("X-KC-SDKID"));
Assert.Contains($"nuget.org;{sdkPackageId};{sdkVersion}", headerContent);
}

[Fact]
public async Task SendAsync_SendsMessageOnSuccessReturnsResponse()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Reflection;

namespace KenticoCloud.ContentManagement.Modules.Extensions
{
internal static class HttpRequestHeadersExtensions
{
private const string SdkTrackingHeaderName = "X-KC-SDKID";

private const string PackageRepositoryHost = "nuget.org";

private static readonly Lazy<string> SdkVersion = new Lazy<string>(GetSdkVersion);
private static readonly Lazy<string> SdkPackageId = new Lazy<string>(GetSdkPackageId);


internal static void AddSdkTrackingHeader(this HttpRequestHeaders header)
{
header.Add(SdkTrackingHeaderName, $"{PackageRepositoryHost};{SdkPackageId.Value};{SdkVersion.Value}");
}

private static string GetSdkVersion()
{
var assembly = Assembly.GetExecutingAssembly();
var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
var sdkVersion = fileVersionInfo.ProductVersion;

return sdkVersion;
}

private static string GetSdkPackageId()
{
var assembly = Assembly.GetExecutingAssembly();
var sdkPackageId = assembly.GetName().Name;

return sdkPackageId;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;

using KenticoCloud.ContentManagement.Exceptions;
using KenticoCloud.ContentManagement.Modules.Extensions;

namespace KenticoCloud.ContentManagement.Modules.HttpClient
{
Expand All @@ -28,6 +29,7 @@ public ContentManagementHttpClient()

public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage message)
{
message.Headers.AddSdkTrackingHeader();
var response = await _httpClient.SendAsync(message);

while ((int)response.StatusCode == HTTP_CODE_TOO_MANY_REQUESTS)
Expand Down

0 comments on commit 546c062

Please sign in to comment.