Skip to content

Commit

Permalink
FE 1443: refactor default constructor (#163)
Browse files Browse the repository at this point in the history
* create DefaultClientIO via Builder
* Builder pattern implementation for DefaultClientIO class

Co-authored-by: Gennadii Shestakov <shestakov,[email protected]>
  • Loading branch information
shestakovg and Gennadii Shestakov authored Jul 29, 2021
1 parent ecffe02 commit a5c58cf
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 26 deletions.
82 changes: 82 additions & 0 deletions FaunaDB.Client/Client/Builder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;

namespace FaunaDB.Client
{
/// <summary>
/// Builder class for DefaultClientIO class
/// </summary>
internal class Builder
{
internal AuthenticationHeaderValue AuthHeader { get; private set; }

internal LastSeen LastSeen { get; private set; }

internal HttpClient Client { get; private set; }

internal string Secret { get; private set; }

internal Uri Endpoint { get; private set; }

internal TimeSpan? Timeout { get; private set; }

internal IReadOnlyDictionary<string, string> CustomHeaders { get; private set; }

internal Version HttpVersion { get; private set; }

internal Builder SetLastSeen(LastSeen lastSeen)
{
LastSeen = lastSeen;
return this;
}

internal Builder SetClient(HttpClient client)
{
Client = client;
return this;
}

internal Builder SetSecret(string secret)
{
Secret = secret;
return this;
}

internal Builder SetEndpoint(Uri endpoint)
{
Endpoint = endpoint;
return this;
}

internal Builder SetTimeout(TimeSpan? timeout)
{
Timeout = timeout;
return this;
}

internal Builder SetCustomHeaders(IReadOnlyDictionary<string, string> customHeaders)
{
CustomHeaders = customHeaders;
return this;
}

internal Builder SetHttpVersion(Version httpVersion)
{
HttpVersion = httpVersion;
return this;
}

internal Builder SetAuthHeader(AuthenticationHeaderValue authHeader)
{
AuthHeader = authHeader;
return this;
}

internal DefaultClientIO Build()
{
return new DefaultClientIO(this);
}
}
}
54 changes: 34 additions & 20 deletions FaunaDB.Client/Client/DefaultClientIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,46 @@ internal class DefaultClientIO : IClientIO
public const string StreamingPath = "stream";
public const HttpMethodKind StreamingHttpMethod = HttpMethodKind.Post;

internal DefaultClientIO(HttpClient client, AuthenticationHeaderValue authHeader, LastSeen lastSeen, Uri endpoint, TimeSpan? timeout, Version httpVersion, IReadOnlyDictionary<string, string> customHeaders)
public static Builder Builder()
{
client.AssertNotNull(nameof(client));
authHeader.AssertNotNull(nameof(authHeader));
endpoint.AssertNotNull(nameof(endpoint));
lastSeen.AssertNotNull(nameof(lastSeen));

this.client = client;
this.authHeader = authHeader;
this.lastSeen = lastSeen;
this.endpoint = endpoint;
this.clientTimeout = timeout;
this.customHeaders = customHeaders;
return new Builder();
}

internal DefaultClientIO(Builder builder)
{
if (builder.AuthHeader == null)
{
builder.Secret.AssertNotNull(nameof(builder.Secret));
}

builder.Endpoint.AssertNotNull(nameof(builder.Endpoint));

this.client = builder.Client ?? CreateClient();
this.authHeader = builder.AuthHeader ?? AuthHeader(builder.Secret);
this.lastSeen = builder.LastSeen ?? new LastSeen();
this.endpoint = builder.Endpoint;
this.clientTimeout = builder.Timeout;
this.customHeaders = builder.CustomHeaders;

#if NETSTANDARD2_1
this.httpVersion = httpVersion == null ? new Version(2, 0) : httpVersion;
this.httpVersion = builder.HttpVersion == null ? new Version(2, 0) : builder.HttpVersion;
#else
this.httpVersion = httpVersion == null ? new Version(1, 1) : httpVersion;
this.httpVersion = builder.HttpVersion == null ? new Version(1, 1) : builder.HttpVersion;
#endif
}

public DefaultClientIO(string secret, Uri endpoint, TimeSpan? timeout = null, HttpClient httpClient = null, Version httpVersion = null, IReadOnlyDictionary<string, string> customHeaders = null)
: this(httpClient ?? CreateClient(), AuthHeader(secret), new LastSeen(), endpoint, timeout, httpVersion, customHeaders)
{ }

public IClientIO NewSessionClient(string secret) =>
new DefaultClientIO(client, AuthHeader(secret), lastSeen, endpoint, clientTimeout, httpVersion, customHeaders);
public IClientIO NewSessionClient(string secret)
{
return Builder()
.SetClient(client)
.SetAuthHeader(AuthHeader(secret))
.SetLastSeen(lastSeen)
.SetEndpoint(endpoint)
.SetTimeout(clientTimeout)
.SetHttpVersion(httpVersion)
.SetCustomHeaders(customHeaders)
.Build();
}

public Task<RequestResult> DoRequest(HttpMethodKind method, string path, string data, IReadOnlyDictionary<string, string> query = null, TimeSpan? queryTimeout = null) =>
DoRequestAsync(method, path, data, query, queryTimeout);
Expand Down
13 changes: 7 additions & 6 deletions FaunaDB.Client/Client/FaunaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,13 @@ private static IClientIO CreateClient(
Task.Run(() => CheckLatestVersion.GetVersionAsync());
}

return new DefaultClientIO(
secret: secret,
endpoint: new Uri(endpoint),
timeout: timeout,
httpClient: httpClient,
httpVersion: httpVersion);
return DefaultClientIO.Builder()
.SetSecret(secret)
.SetEndpoint(new Uri(endpoint))
.SetTimeout(timeout)
.SetClient(httpClient)
.SetHttpVersion(httpVersion)
.Build();
}
}
}

0 comments on commit a5c58cf

Please sign in to comment.