16 private const string QueryUriPath =
"/query/1";
22 private readonly Dictionary<Type, DataContext> _dbCtxs =
new();
24 private bool _disposed;
26 internal override MappingContext MappingCtx {
get => _defaultCtx; }
59 var dbCtxType = typeof(DB);
63 if (!_dbCtxs.TryGetValue(dbCtxType, out ctx))
65 var builder =
new DataContextBuilder<DB>();
66 ctx = builder.Build(
this);
67 _dbCtxs[dbCtxType] = ctx;
74 internal override async Task<QuerySuccess<T>> QueryAsyncInternal<T>(
79 CancellationToken cancel)
83 throw new ArgumentNullException(nameof(query));
86 var finalOptions =
QueryOptions.GetFinalQueryOptions(_config.DefaultQueryOptions, queryOptions);
87 var headers = GetRequestHeaders(finalOptions);
89 using var stream =
new MemoryStream();
90 Serialize(stream, query, ctx);
92 using var httpResponse = await _connection.DoPostAsync(QueryUriPath, stream, headers, cancel);
93 var body = await httpResponse.Content.ReadAsStringAsync(cancel);
94 var res =
QueryResponse.GetFromResponseBody<T>(ctx, deserializer, httpResponse.StatusCode, body);
101 throw ExceptionFactory.FromQueryFailure(ctx, failure);
103 throw ExceptionFactory.FromRawResponse(body, httpResponse);
110 writer.WriteStartObject();
111 writer.WriteFieldName(
"query");
113 writer.WriteEndObject();
117 private Dictionary<string, string> GetRequestHeaders(
QueryOptions? queryOptions)
119 var headers =
new Dictionary<string, string>
122 { Headers.Authorization, $
"Bearer {_config.Secret}"},
123 { Headers.Format,
"tagged" },
124 { Headers.Driver,
"C#" }
129 headers.Add(Headers.LastTxnTs,
LastSeenTxn.ToString());
132 if (queryOptions !=
null)
137 Headers.QueryTimeoutMs,
138 queryOptions.
QueryTimeout.Value.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
143 headers.Add(Headers.QueryTags, EncodeQueryTags(queryOptions.
QueryTags));
146 if (!
string.IsNullOrEmpty(queryOptions.
TraceParent))
148 headers.Add(Headers.TraceParent, queryOptions.
TraceParent);
153 headers.Add(Headers.Linearized, queryOptions.
Linearized.ToString()!);
158 headers.Add(Headers.TypeCheck, queryOptions.
TypeCheck.ToString()!);
165 private static string EncodeQueryTags(Dictionary<string, string> tags)
167 return string.Join(
",", tags.Select(entry => entry.Key +
"=" + entry.Value));
170 private void Dispose(
bool disposing)
172 if (_disposed)
return;
176 _connection.Dispose();
177 GC.SuppressFinalize(
this);
188 GC.SuppressFinalize(
this);