Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
Client.cs
Go to the documentation of this file.
1using System.Globalization;
4using Fauna.Linq;
5using Fauna.Mapping;
7
8namespace Fauna;
9
13public class Client : BaseClient, IDisposable
14{
15 private const string QueryUriPath = "/query/1";
16
17 private readonly Configuration _config;
18 private readonly IConnection _connection;
19
20 private readonly MappingContext _defaultCtx = new();
21 private readonly Dictionary<Type, DataContext> _dbCtxs = new();
22
23 private bool _disposed;
24
25 internal override MappingContext MappingCtx { get => _defaultCtx; }
26
31
35 public long LastSeenTxn { get; private set; }
36
41 public Client(string secret) :
42 this(new Configuration(secret))
43 {
44 }
45
50 public Client(Configuration config)
51 {
52 _config = config;
54 _connection = new Connection(config);
55 }
56
62 public DB DataContext<DB>() where DB : DataContext
63 {
64 var dbCtxType = typeof(DB);
65 DataContext? ctx;
66 lock (_dbCtxs)
67 {
68 if (!_dbCtxs.TryGetValue(dbCtxType, out ctx))
69 {
70 var builder = new DataContextBuilder<DB>();
71 ctx = builder.Build(this);
72 _dbCtxs[dbCtxType] = ctx;
73 }
74 }
75
76 return (DB)ctx;
77 }
78
82 public void Dispose()
83 {
84 Dispose(true);
85 GC.SuppressFinalize(this);
86 }
87
88 // A finalizer: https://stackoverflow.com/questions/151051/when-should-i-use-gc-suppressfinalize
89 ~Client()
90 {
91 Dispose(false);
92 }
93
94 internal override async Task<QuerySuccess<T>> QueryAsyncInternal<T>(
95 Query query,
96 ISerializer<T> serializer,
98 QueryOptions? queryOptions,
99 CancellationToken cancel)
100 {
101 if (query == null)
102 {
103 throw new ArgumentNullException(nameof(query));
104 }
105
106 var finalOptions = QueryOptions.GetFinalQueryOptions(_config.DefaultQueryOptions, queryOptions);
107 var headers = GetRequestHeaders(finalOptions);
108
109 using var stream = new MemoryStream();
110 Serialize(stream, query, ctx);
111
112 using var httpResponse = await _connection.DoPostAsync(QueryUriPath, stream, headers, cancel);
113 var body = await httpResponse.Content.ReadAsStringAsync(cancel);
114 var res = QueryResponse.GetFromResponseBody<T>(ctx, serializer, httpResponse.StatusCode, body);
115 switch (res)
116 {
117 case QuerySuccess<T> success:
119 StatsCollector?.Add(res.Stats);
120 return success;
121 case QueryFailure failure:
122 StatsCollector?.Add(res.Stats);
123 throw ExceptionFactory.FromQueryFailure(ctx, failure);
124 default:
125 throw ExceptionFactory.FromRawResponse(body, httpResponse);
126 }
127 }
128
129 private void Serialize(Stream stream, Query query, MappingContext ctx)
130 {
131 using var writer = new Utf8FaunaWriter(stream);
132 writer.WriteStartObject();
133 writer.WriteFieldName("query");
134 query.Serialize(ctx, writer);
135 writer.WriteEndObject();
136 writer.Flush();
137 }
138
139 private Dictionary<string, string> GetRequestHeaders(QueryOptions? queryOptions)
140 {
141 var headers = new Dictionary<string, string>
142 {
143
144 { Headers.Authorization, $"Bearer {_config.Secret}"},
145 { Headers.Format, "tagged" },
146 { Headers.Driver, "C#" }
147 };
148
149 if (LastSeenTxn > long.MinValue)
150 {
151 headers.Add(Headers.LastTxnTs, LastSeenTxn.ToString());
152 }
153
154 if (queryOptions != null)
155 {
156 if (queryOptions.QueryTimeout.HasValue)
157 {
158 headers.Add(
159 Headers.QueryTimeoutMs,
160 queryOptions.QueryTimeout.Value.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
161 }
162
163 if (queryOptions.QueryTags != null)
164 {
165 headers.Add(Headers.QueryTags, EncodeQueryTags(queryOptions.QueryTags));
166 }
167
168 if (!string.IsNullOrEmpty(queryOptions.TraceParent))
169 {
170 headers.Add(Headers.TraceParent, queryOptions.TraceParent);
171 }
172
173 if (queryOptions.Linearized != null)
174 {
175 headers.Add(Headers.Linearized, queryOptions.Linearized.ToString()!);
176 }
177
178 if (queryOptions.TypeCheck != null)
179 {
180 headers.Add(Headers.TypeCheck, queryOptions.TypeCheck.ToString()!);
181 }
182 }
183
184 return headers;
185 }
186
187 private static string EncodeQueryTags(Dictionary<string, string> tags)
188 {
189 return string.Join(",", tags.Select(entry => entry.Key + "=" + entry.Value));
190 }
191
192 private void Dispose(bool disposing)
193 {
194 if (_disposed) return;
195
196 if (disposing)
197 {
198 _connection.Dispose();
199 GC.SuppressFinalize(this);
200 }
201 _disposed = true;
202 }
203}
Fauna.Types.Stream Stream
Definition Serializer.cs:4
The base class for Client and DataContext.
Definition IClient.cs:345
Represents a client for interacting with a Fauna.
Definition Client.cs:14
readonly? IStatsCollector StatsCollector
Provides collection and aggregation of query statistics. Can be set to null in the Configuration.
Definition Client.cs:30
Client(Configuration config)
Initializes a new instance of the Client with a custom Configuration.
Definition Client.cs:50
Client(string secret)
Initializes a new instance of a Client with a secret.
Definition Client.cs:41
long LastSeenTxn
Gets the timestamp of the last transaction seen by this client.
Definition Client.cs:35
DB DataContext< DB >()
Create and return a new database context which uses the Client instance.
Definition Client.cs:62
void Dispose()
Disposes the resources used by the Client class.
Definition Client.cs:82
Configuration is a class used to configure a Fauna Client. It encapsulates various settings such as t...
IStatsCollector? StatsCollector
StatsCollector for the client.
A class representing the mapping context to be used during serialization and deserialization.
Represents a failed query response.
Represents the abstract base class for constructing FQL queries.
Definition Query.cs:11
void Serialize(MappingContext ctx, Utf8FaunaWriter writer)
Serializes the query into the provided stream.
Represents the options for customizing Fauna queries.
string? TraceParent
Gets or sets the trace parent identifier for distributed tracing systems.
TimeSpan? QueryTimeout
Gets or sets the query timeout. It defines how long the client waits for a query to complete.
bool? TypeCheck
Gets or sets a value indicating whether type checking of the query is enabled or disabled before eval...
bool? Linearized
Gets or sets a value indicating whether the query runs as strictly serialized, affecting read-only tr...
Dictionary< string, string >? QueryTags
Gets or sets a string-encoded set of caller-defined tags for identifying the request in logs and resp...
Represents the response from a query executed.
long LastSeenTxn
Gets the last transaction seen by this query.
Represents a successful query response.
Provides functionality for writing data in a streaming manner to a buffer or a stream.
void Add(QueryStats stats)
Add the QueryStats to the current counts.
Definition Client.cs:8