Skip to content

Commit 74dee6f

Browse files
authored
feat(OSS-846): Added an optional customHeaders parameter for FaunaCli… (#158)
1 parent 433639f commit 74dee6f

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

FaunaDB.Client.Test/ClientTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,37 @@ public async Task TestGetAnInstance()
231231
Assert.AreEqual("Magic Missile", document.Get(NAME_FIELD));
232232
}
233233

234+
[Test]
235+
public async Task TestGetAnInstanceWithCustomHeaders()
236+
{
237+
var clientWithCustomHeaders = new FaunaClient(
238+
secret: faunaSecret,
239+
endpoint: faunaEndpoint,
240+
customHeaders: new Dictionary<string, string>
241+
{
242+
{ "test-header-1", "test-value-1" },
243+
{ "test-header-2", "test-value-2" },
244+
}
245+
);
246+
247+
await clientWithCustomHeaders.Query(
248+
CreateCollection(
249+
Obj("name", "magic_spells"))
250+
);
251+
252+
var magicFireball = GetRef(await clientWithCustomHeaders.Query(
253+
Create(Collection("magic_spells"),
254+
Obj("data",
255+
Obj(
256+
"name", "Fire Ball",
257+
"element", "arcane",
258+
"cost", 10)))
259+
));
260+
261+
Value document = await clientWithCustomHeaders.Query(Get(magicFireball));
262+
Assert.AreEqual("Fire Ball", document.Get(NAME_FIELD));
263+
}
264+
234265
[Test]
235266
public async Task TestIssueABatchedQueryWithVarargs()
236267
{

FaunaDB.Client.Test/TestCase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using System.Net;
45
using System.Net.Http;
56
using System.Text;

FaunaDB.Client/Client/DefaultClientIO.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ internal class DefaultClientIO : IClientIO
2424
private readonly HttpClient client;
2525
private readonly AuthenticationHeaderValue authHeader;
2626

27+
private readonly IReadOnlyDictionary<string, string> customHeaders;
28+
2729
private LastSeen lastSeen;
2830
private Version httpVersion;
2931

3032
public const string StreamingPath = "stream";
3133
public const HttpMethodKind StreamingHttpMethod = HttpMethodKind.Post;
3234

33-
internal DefaultClientIO(HttpClient client, AuthenticationHeaderValue authHeader, LastSeen lastSeen, Uri endpoint, TimeSpan? timeout, Version httpVersion)
35+
internal DefaultClientIO(HttpClient client, AuthenticationHeaderValue authHeader, LastSeen lastSeen, Uri endpoint, TimeSpan? timeout, Version httpVersion, IReadOnlyDictionary<string, string> customHeaders)
3436
{
3537
client.AssertNotNull(nameof(client));
3638
authHeader.AssertNotNull(nameof(authHeader));
@@ -42,19 +44,20 @@ internal DefaultClientIO(HttpClient client, AuthenticationHeaderValue authHeader
4244
this.lastSeen = lastSeen;
4345
this.endpoint = endpoint;
4446
this.clientTimeout = timeout;
47+
this.customHeaders = customHeaders;
4548
#if NETSTANDARD2_1
4649
this.httpVersion = httpVersion == null ? new Version(2, 0) : httpVersion;
4750
#else
4851
this.httpVersion = httpVersion == null ? new Version(1, 1) : httpVersion;
4952
#endif
5053
}
5154

52-
public DefaultClientIO(string secret, Uri endpoint, TimeSpan? timeout = null, HttpClient httpClient = null, Version httpVersion = null)
53-
: this(httpClient ?? CreateClient(), AuthHeader(secret), new LastSeen(), endpoint, timeout, httpVersion)
55+
public DefaultClientIO(string secret, Uri endpoint, TimeSpan? timeout = null, HttpClient httpClient = null, Version httpVersion = null, IReadOnlyDictionary<string, string> customHeaders = null)
56+
: this(httpClient ?? CreateClient(), AuthHeader(secret), new LastSeen(), endpoint, timeout, httpVersion, customHeaders)
5457
{ }
5558

5659
public IClientIO NewSessionClient(string secret) =>
57-
new DefaultClientIO(client, AuthHeader(secret), lastSeen, endpoint, clientTimeout, httpVersion);
60+
new DefaultClientIO(client, AuthHeader(secret), lastSeen, endpoint, clientTimeout, httpVersion, customHeaders);
5861

5962
public Task<RequestResult> DoRequest(HttpMethodKind method, string path, string data, IReadOnlyDictionary<string, string> query = null, TimeSpan? queryTimeout = null) =>
6063
DoRequestAsync(method, path, data, query, queryTimeout);
@@ -82,6 +85,15 @@ private async Task<RequestResult> DoRequestAsync(HttpMethodKind method, string p
8285
message.Headers.Add("X-Driver-Env", RuntimeEnvironmentHeader.Construct(EnvironmentEditor.Create()));
8386
message.Version = httpVersion;
8487

88+
// adding custom headers provided during the client creation
89+
if (customHeaders != null)
90+
{
91+
foreach (KeyValuePair<string, string> header in customHeaders)
92+
{
93+
message.Headers.Add(header.Key, header.Value);
94+
}
95+
}
96+
8597
var last = lastSeen.Txn;
8698
if (last.HasValue)
8799
{
@@ -141,6 +153,15 @@ private async Task<StreamingRequestResult> DoStreamingRequestAsync(string data,
141153
message.Version = httpVersion;
142154
message.SetTimeout(Timeout.InfiniteTimeSpan);
143155

156+
// adding custom headers provided during the client creation
157+
if (customHeaders != null)
158+
{
159+
foreach (KeyValuePair<string, string> header in customHeaders)
160+
{
161+
message.Headers.Add(header.Key, header.Value);
162+
}
163+
}
164+
144165
var last = lastSeen.Txn;
145166
if (last.HasValue)
146167
{

FaunaDB.Client/Client/FaunaClient.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public FaunaClient(
4040
TimeSpan? timeout = null,
4141
HttpClient httpClient = null,
4242
Version httpVersion = null,
43-
bool checkNewVersion = true)
44-
: this(CreateClient(secret, endpoint, timeout, httpClient, httpVersion, checkNewVersion))
43+
bool checkNewVersion = true,
44+
IReadOnlyDictionary<string, string> customHeaders = null)
45+
: this(CreateClient(secret, endpoint, timeout, httpClient, httpVersion, checkNewVersion, customHeaders))
4546
{ }
4647

4748
/// <summary>
@@ -253,7 +254,8 @@ private static IClientIO CreateClient(
253254
TimeSpan? timeout = null,
254255
HttpClient httpClient = null,
255256
Version httpVersion = null,
256-
bool checkNewVersion = true)
257+
bool checkNewVersion = true,
258+
IReadOnlyDictionary<string, string> customHeaders = null)
257259
{
258260
secret.AssertNotNull(nameof(secret));
259261
endpoint.AssertNotNull(nameof(endpoint));

0 commit comments

Comments
 (0)