Skip to content

Commit a5c58cf

Browse files
shestakovgGennadii Shestakov
andauthored
FE 1443: refactor default constructor (#163)
* create DefaultClientIO via Builder * Builder pattern implementation for DefaultClientIO class Co-authored-by: Gennadii Shestakov <shestakov,[email protected]>
1 parent ecffe02 commit a5c58cf

File tree

3 files changed

+123
-26
lines changed

3 files changed

+123
-26
lines changed

FaunaDB.Client/Client/Builder.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using System.Net.Http.Headers;
5+
6+
namespace FaunaDB.Client
7+
{
8+
/// <summary>
9+
/// Builder class for DefaultClientIO class
10+
/// </summary>
11+
internal class Builder
12+
{
13+
internal AuthenticationHeaderValue AuthHeader { get; private set; }
14+
15+
internal LastSeen LastSeen { get; private set; }
16+
17+
internal HttpClient Client { get; private set; }
18+
19+
internal string Secret { get; private set; }
20+
21+
internal Uri Endpoint { get; private set; }
22+
23+
internal TimeSpan? Timeout { get; private set; }
24+
25+
internal IReadOnlyDictionary<string, string> CustomHeaders { get; private set; }
26+
27+
internal Version HttpVersion { get; private set; }
28+
29+
internal Builder SetLastSeen(LastSeen lastSeen)
30+
{
31+
LastSeen = lastSeen;
32+
return this;
33+
}
34+
35+
internal Builder SetClient(HttpClient client)
36+
{
37+
Client = client;
38+
return this;
39+
}
40+
41+
internal Builder SetSecret(string secret)
42+
{
43+
Secret = secret;
44+
return this;
45+
}
46+
47+
internal Builder SetEndpoint(Uri endpoint)
48+
{
49+
Endpoint = endpoint;
50+
return this;
51+
}
52+
53+
internal Builder SetTimeout(TimeSpan? timeout)
54+
{
55+
Timeout = timeout;
56+
return this;
57+
}
58+
59+
internal Builder SetCustomHeaders(IReadOnlyDictionary<string, string> customHeaders)
60+
{
61+
CustomHeaders = customHeaders;
62+
return this;
63+
}
64+
65+
internal Builder SetHttpVersion(Version httpVersion)
66+
{
67+
HttpVersion = httpVersion;
68+
return this;
69+
}
70+
71+
internal Builder SetAuthHeader(AuthenticationHeaderValue authHeader)
72+
{
73+
AuthHeader = authHeader;
74+
return this;
75+
}
76+
77+
internal DefaultClientIO Build()
78+
{
79+
return new DefaultClientIO(this);
80+
}
81+
}
82+
}

FaunaDB.Client/Client/DefaultClientIO.cs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,46 @@ internal class DefaultClientIO : IClientIO
3232
public const string StreamingPath = "stream";
3333
public const HttpMethodKind StreamingHttpMethod = HttpMethodKind.Post;
3434

35-
internal DefaultClientIO(HttpClient client, AuthenticationHeaderValue authHeader, LastSeen lastSeen, Uri endpoint, TimeSpan? timeout, Version httpVersion, IReadOnlyDictionary<string, string> customHeaders)
35+
public static Builder Builder()
3636
{
37-
client.AssertNotNull(nameof(client));
38-
authHeader.AssertNotNull(nameof(authHeader));
39-
endpoint.AssertNotNull(nameof(endpoint));
40-
lastSeen.AssertNotNull(nameof(lastSeen));
41-
42-
this.client = client;
43-
this.authHeader = authHeader;
44-
this.lastSeen = lastSeen;
45-
this.endpoint = endpoint;
46-
this.clientTimeout = timeout;
47-
this.customHeaders = customHeaders;
37+
return new Builder();
38+
}
39+
40+
internal DefaultClientIO(Builder builder)
41+
{
42+
if (builder.AuthHeader == null)
43+
{
44+
builder.Secret.AssertNotNull(nameof(builder.Secret));
45+
}
46+
47+
builder.Endpoint.AssertNotNull(nameof(builder.Endpoint));
48+
49+
this.client = builder.Client ?? CreateClient();
50+
this.authHeader = builder.AuthHeader ?? AuthHeader(builder.Secret);
51+
this.lastSeen = builder.LastSeen ?? new LastSeen();
52+
this.endpoint = builder.Endpoint;
53+
this.clientTimeout = builder.Timeout;
54+
this.customHeaders = builder.CustomHeaders;
55+
4856
#if NETSTANDARD2_1
49-
this.httpVersion = httpVersion == null ? new Version(2, 0) : httpVersion;
57+
this.httpVersion = builder.HttpVersion == null ? new Version(2, 0) : builder.HttpVersion;
5058
#else
51-
this.httpVersion = httpVersion == null ? new Version(1, 1) : httpVersion;
59+
this.httpVersion = builder.HttpVersion == null ? new Version(1, 1) : builder.HttpVersion;
5260
#endif
5361
}
5462

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)
57-
{ }
58-
59-
public IClientIO NewSessionClient(string secret) =>
60-
new DefaultClientIO(client, AuthHeader(secret), lastSeen, endpoint, clientTimeout, httpVersion, customHeaders);
63+
public IClientIO NewSessionClient(string secret)
64+
{
65+
return Builder()
66+
.SetClient(client)
67+
.SetAuthHeader(AuthHeader(secret))
68+
.SetLastSeen(lastSeen)
69+
.SetEndpoint(endpoint)
70+
.SetTimeout(clientTimeout)
71+
.SetHttpVersion(httpVersion)
72+
.SetCustomHeaders(customHeaders)
73+
.Build();
74+
}
6175

6276
public Task<RequestResult> DoRequest(HttpMethodKind method, string path, string data, IReadOnlyDictionary<string, string> query = null, TimeSpan? queryTimeout = null) =>
6377
DoRequestAsync(method, path, data, query, queryTimeout);

FaunaDB.Client/Client/FaunaClient.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,13 @@ private static IClientIO CreateClient(
265265
Task.Run(() => CheckLatestVersion.GetVersionAsync());
266266
}
267267

268-
return new DefaultClientIO(
269-
secret: secret,
270-
endpoint: new Uri(endpoint),
271-
timeout: timeout,
272-
httpClient: httpClient,
273-
httpVersion: httpVersion);
268+
return DefaultClientIO.Builder()
269+
.SetSecret(secret)
270+
.SetEndpoint(new Uri(endpoint))
271+
.SetTimeout(timeout)
272+
.SetClient(httpClient)
273+
.SetHttpVersion(httpVersion)
274+
.Build();
274275
}
275276
}
276277
}

0 commit comments

Comments
 (0)