15 private const string QueryUriPath =
"/query/1";
18 private readonly IConnection _connection;
21 private readonly Dictionary<Type, DataContext> _dbCtxs =
new();
23 private bool _disposed;
25 internal override MappingContext MappingCtx {
get => _defaultCtx; }
54 _connection =
new Connection(config);
64 var dbCtxType = typeof(DB);
68 if (!_dbCtxs.TryGetValue(dbCtxType, out ctx))
70 var builder =
new DataContextBuilder<DB>();
71 ctx = builder.Build(
this);
72 _dbCtxs[dbCtxType] = ctx;
85 GC.SuppressFinalize(
this);
94 internal override async Task<QuerySuccess<T>> QueryAsyncInternal<T>(
99 CancellationToken cancel)
103 throw new ArgumentNullException(nameof(query));
106 var finalOptions =
QueryOptions.GetFinalQueryOptions(_config.DefaultQueryOptions, queryOptions);
107 var headers = GetRequestHeaders(finalOptions);
109 using var stream =
new MemoryStream();
110 Serialize(stream, query, ctx);
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);
123 throw ExceptionFactory.FromQueryFailure(ctx, failure);
125 throw ExceptionFactory.FromRawResponse(body, httpResponse);
132 writer.WriteStartObject();
133 writer.WriteFieldName(
"query");
135 writer.WriteEndObject();
139 private Dictionary<string, string> GetRequestHeaders(
QueryOptions? queryOptions)
141 var headers =
new Dictionary<string, string>
144 { Headers.Authorization, $
"Bearer {_config.Secret}"},
145 { Headers.Format,
"tagged" },
146 { Headers.Driver,
"C#" }
151 headers.Add(Headers.LastTxnTs,
LastSeenTxn.ToString());
154 if (queryOptions !=
null)
159 Headers.QueryTimeoutMs,
160 queryOptions.
QueryTimeout.Value.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
165 headers.Add(Headers.QueryTags, EncodeQueryTags(queryOptions.
QueryTags));
168 if (!
string.IsNullOrEmpty(queryOptions.
TraceParent))
170 headers.Add(Headers.TraceParent, queryOptions.
TraceParent);
175 headers.Add(Headers.Linearized, queryOptions.
Linearized.ToString()!);
180 headers.Add(Headers.TypeCheck, queryOptions.
TypeCheck.ToString()!);
187 private static string EncodeQueryTags(Dictionary<string, string> tags)
189 return string.Join(
",", tags.Select(entry => entry.Key +
"=" + entry.Value));
192 private void Dispose(
bool disposing)
194 if (_disposed)
return;
198 _connection.Dispose();
199 GC.SuppressFinalize(
this);