Skip to content

Commit

Permalink
Merge pull request #441 from ArangoDB-Community/DE-428-440-header-for…
Browse files Browse the repository at this point in the history
…-driver-version-and-other-info

Added header for driver version and other info
  • Loading branch information
DiscoPYF authored Dec 9, 2022
2 parents 7b05d65 + 3e968e4 commit fd82aef
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
4 changes: 4 additions & 0 deletions arangodb-net-standard/CustomHttpHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ public static class CustomHttpHeaders
/// Introduced in ArangoDB 3.10.
/// </summary>
public const string ReadFromFollowersHeader = "x-arango-allow-dirty-read";
/// <summary>
/// The header string used for Driver Info Header
/// </summary>
public const string DriverInfoHeader = "x-arango-driver";
}
}
17 changes: 17 additions & 0 deletions arangodb-net-standard/DriverConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ArangoDBNetStandard
{
/// <summary>
/// Global constants to be used throughout the driver
/// </summary>
public static class DriverConstants
{
/// <summary>
/// Name of the driver
/// </summary>
public const string DriverName = "arangodb-net-standard";
}
}
31 changes: 23 additions & 8 deletions arangodb-net-standard/Transport/Http/HttpApiTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
Expand Down Expand Up @@ -31,6 +32,11 @@ public class HttpApiTransport : IApiClientTransport
[HttpContentType.VPack] = "application/x-velocypack"
};

/// <summary>
/// Flags containing specific driver information.
/// </summary>
public IEnumerable<string> DriverFlags { get; set; }

/// <summary>
/// Create <see cref="HttpApiTransport"/> from an existing <see cref="HttpClient"/> instance.
/// </summary>
Expand All @@ -48,7 +54,8 @@ public HttpApiTransport(HttpClient client, HttpContentType contentType)
/// </summary>
/// <param name="webHeaderCollection">Object containing a dictionary of Header keys and values.</param>
/// <param name="headers">The header to update.</param>
private static void ApplyHeaders(WebHeaderCollection webHeaderCollection, HttpHeaders headers)
/// <param name="driverFlags">Driver flags that are passed to the server.</param>
private static void ApplyHeaders(WebHeaderCollection webHeaderCollection, HttpHeaders headers, IEnumerable<string> driverFlags = null)
{
if (webHeaderCollection != null)
{
Expand All @@ -57,6 +64,14 @@ private static void ApplyHeaders(WebHeaderCollection webHeaderCollection, HttpHe
headers.Add(key, webHeaderCollection[key]);
}
}
//build and add driver info header
string flags = string.Empty;
if (driverFlags != null)
{
flags = string.Join(";", driverFlags);
}
var assemblyInfo = Assembly.GetCallingAssembly().GetName();
headers.Add(CustomHttpHeaders.DriverInfoHeader, $"{DriverConstants.DriverName}/{assemblyInfo.Version} ({flags})");
}

/// <summary>
Expand Down Expand Up @@ -284,7 +299,7 @@ public async Task<IApiClientResponse> DeleteAsync(
CancellationToken token = default)
{
var request = new HttpRequestMessage(HttpMethod.Delete, requestUri);
ApplyHeaders(webHeaderCollection, request.Headers);
ApplyHeaders(webHeaderCollection, request.Headers,DriverFlags);
var response = await _client.SendAsync(request, token).ConfigureAwait(false);
return new HttpApiClientResponse(response);
}
Expand All @@ -309,7 +324,7 @@ public async Task<IApiClientResponse> DeleteAsync(
};

request.Content.Headers.ContentType = new MediaTypeHeaderValue(_contentTypeMap[_contentType]);
ApplyHeaders(webHeaderCollection, request.Content.Headers);
ApplyHeaders(webHeaderCollection, request.Content.Headers, DriverFlags);
var response = await _client.SendAsync(request, token).ConfigureAwait(false);
return new HttpApiClientResponse(response);
}
Expand All @@ -330,7 +345,7 @@ public async Task<IApiClientResponse> PostAsync(
{
var httpContent = new ByteArrayContent(content);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(_contentTypeMap[_contentType]);
ApplyHeaders(webHeaderCollection, httpContent.Headers);
ApplyHeaders(webHeaderCollection, httpContent.Headers, DriverFlags);
var response = await _client.PostAsync(requestUri, httpContent, token).ConfigureAwait(false);
return new HttpApiClientResponse(response);
}
Expand All @@ -351,7 +366,7 @@ public async Task<IApiClientResponse> PutAsync(
{
var httpContent = new ByteArrayContent(content);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(_contentTypeMap[_contentType]);
ApplyHeaders(webHeaderCollection, httpContent.Headers);
ApplyHeaders(webHeaderCollection, httpContent.Headers, DriverFlags);
var response = await _client.PutAsync(requestUri, httpContent, token).ConfigureAwait(false);
return new HttpApiClientResponse(response);
}
Expand All @@ -369,7 +384,7 @@ public async Task<IApiClientResponse> GetAsync(
CancellationToken token = default)
{
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
ApplyHeaders(webHeaderCollection, request.Headers);
ApplyHeaders(webHeaderCollection, request.Headers, DriverFlags);
var response = await _client.SendAsync(request, token).ConfigureAwait(false);
return new HttpApiClientResponse(response);
}
Expand All @@ -395,7 +410,7 @@ public async Task<IApiClientResponse> PatchAsync(
};

request.Content.Headers.ContentType = new MediaTypeHeaderValue(_contentTypeMap[_contentType]);
ApplyHeaders(webHeaderCollection, request.Content.Headers);
ApplyHeaders(webHeaderCollection, request.Content.Headers, DriverFlags);
var response = await _client.SendAsync(request, token).ConfigureAwait(false);
return new HttpApiClientResponse(response);
}
Expand All @@ -413,7 +428,7 @@ public async Task<IApiClientResponse> HeadAsync(
CancellationToken token = default)
{
var request = new HttpRequestMessage(HttpMethod.Head, requestUri);
ApplyHeaders(webHeaderCollection, request.Headers);
ApplyHeaders(webHeaderCollection, request.Headers, DriverFlags);
var response = await _client.SendAsync(request, token);
return new HttpApiClientResponse(response);
}
Expand Down
6 changes: 6 additions & 0 deletions arangodb-net-standard/Transport/IApiClientTransport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -10,6 +11,11 @@ namespace ArangoDBNetStandard.Transport
/// </summary>
public interface IApiClientTransport : IDisposable
{
/// <summary>
/// Flags containing specific driver information.
/// </summary>
IEnumerable<string> DriverFlags { get; set; }

/// <summary>
/// Send a POST request.
/// </summary>
Expand Down

0 comments on commit fd82aef

Please sign in to comment.