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

Added header for driver version and other info #441

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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";
}
}
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 List<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, List<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, $"{assemblyInfo.Name}/{assemblyInfo.Version} ({flags})");
tjoubert marked this conversation as resolved.
Show resolved Hide resolved
}

/// <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>
List<string> DriverFlags { get; set; }
tjoubert marked this conversation as resolved.
Show resolved Hide resolved

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