Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
Connection.cs
Go to the documentation of this file.
1using System.Net.Http.Headers;
2using Polly;
3
4namespace Fauna;
5
9internal class Connection : IConnection
10{
11 private readonly Configuration _cfg;
12 private bool _disposed;
13
18 public Connection(Configuration configuration)
19 {
20 _cfg = configuration;
21 }
22
23 public async Task<HttpResponseMessage> DoPostAsync(
24 string path,
25 Stream body,
26 Dictionary<string, string> headers,
27 CancellationToken cancel = default)
28 {
29 HttpResponseMessage response;
30 {
31
32 var policyResult = await _cfg.RetryConfiguration.RetryPolicy
33 .ExecuteAndCaptureAsync(async () => await _cfg.HttpClient.SendAsync(CreateHttpRequest(path, body, headers), cancel))
34 .ConfigureAwait(false);
35
36 if (policyResult.Outcome == OutcomeType.Successful)
37 {
38 response = policyResult.Result;
39 }
40 else
41 {
42 throw policyResult.FinalException;
43 }
44 }
45
46 return response;
47 }
48
49 private HttpRequestMessage CreateHttpRequest(string path, Stream body, Dictionary<string, string> headers)
50 {
51 body.Position = 0;
52 var request = new HttpRequestMessage
53 {
54 Content = new StreamContent(body),
55 Method = HttpMethod.Post,
56 RequestUri = new Uri(_cfg.Endpoint, path)
57 };
58
59 request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
60 request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
61
62 foreach (var header in headers)
63 {
64 request.Headers.Add(header.Key, header.Value);
65 }
66
67 return request;
68 }
69
70 private void Dispose(bool disposing)
71 {
72 if (_disposed) return;
73
74 if (disposing && _cfg.DisposeHttpClient)
75 {
76 _cfg.HttpClient.Dispose();
77 GC.SuppressFinalize(this);
78 }
79 _disposed = true;
80 }
81
85 public void Dispose()
86 {
87 Dispose(true);
88 }
89
90 // A finalizer: https://stackoverflow.com/questions/151051/when-should-i-use-gc-suppressfinalize
91 ~Connection()
92 {
93 Dispose(false);
94 }
95}
Fauna.Types.Stream Stream
Definition Serializer.cs:4
Definition Client.cs:8