Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
QueryResponse.cs
Go to the documentation of this file.
1using System.Net;
2using System.Text.Json;
4using Fauna.Mapping;
6using static Fauna.Core.ResponseFields;
7
8namespace Fauna.Core;
9
13public abstract class QueryResponse
14{
18 public JsonElement RawJson { get; init; }
19
23 public long LastSeenTxn { get; init; }
24
28 public long SchemaVersion { get; init; }
29
33 public string Summary { get; init; } = "";
34
38 public Dictionary<string, string> QueryTags { get; init; } = new();
39
43 public QueryStats Stats { get; init; }
44
45 internal QueryResponse(JsonElement json)
46 {
47 RawJson = json;
48
49 if (json.TryGetProperty(LastSeenTxnFieldName, out var elem))
50 {
51 if (elem.TryGetInt64(out var i)) LastSeenTxn = i;
52 }
53
54 if (json.TryGetProperty(SchemaVersionFieldName, out elem))
55 {
56 if (elem.TryGetInt64(out var i)) LastSeenTxn = i;
57 }
58
59 if (json.TryGetProperty(SummaryFieldName, out elem))
60 {
61 Summary = elem.GetString() ?? "";
62 }
63
64
65 if (json.TryGetProperty(QueryTagsFieldName, out elem))
66 {
67 var queryTagsString = elem.GetString();
68
69 if (!string.IsNullOrEmpty(queryTagsString))
70 {
71 var tagPairs = queryTagsString.Split(',').Select(tag =>
72 {
73 var tokens = tag.Split('=');
74 return KeyValuePair.Create(tokens[0], tokens[1]);
75 });
76
77 QueryTags = new Dictionary<string, string>(tagPairs);
78 }
79 }
80
81 if (json.TryGetProperty(StatsFieldName, out elem))
82 {
83 Stats = elem.Deserialize<QueryStats>();
84 }
85 }
86
98 ISerializer<T> serializer,
99 HttpStatusCode statusCode,
100 string body)
101 {
102 try
103 {
104 var json = JsonSerializer.Deserialize<JsonElement>(body);
105
106 if (statusCode is >= HttpStatusCode.OK and <= (HttpStatusCode)299)
107 {
108 return new QuerySuccess<T>(ctx, serializer, json);
109 }
110
111 return new QueryFailure(statusCode, json);
112 }
113 catch (JsonException)
114 {
115 return null;
116 }
117 }
118}
119
124public sealed class QuerySuccess<T> : QueryResponse
125{
129 public T Data { get; init; }
130
134 public string? StaticType { get; init; }
135
143 MappingContext ctx,
144 ISerializer<T> serializer,
145 JsonElement json)
146 : base(json)
147 {
148 var dataText = json.GetProperty(DataFieldName).GetRawText();
149 var reader = new Utf8FaunaReader(dataText);
150 reader.Read();
151 Data = serializer.Deserialize(ctx, ref reader);
152
153 if (json.TryGetProperty(StaticTypeFieldName, out var elem))
154 {
155 StaticType = elem.GetString();
156 }
157 }
158}
159
163public sealed class QueryFailure : QueryResponse
164{
168 public HttpStatusCode StatusCode { get; init; }
172 public string ErrorCode { get; init; } = "";
176 public string Message { get; init; } = "";
180 public ConstraintFailure[]? ConstraintFailures { get; init; }
184 public object? Abort { get; init; }
185
191 public QueryFailure(HttpStatusCode statusCode, JsonElement json) : base(json)
192 {
193 StatusCode = statusCode;
194 if (!json.TryGetProperty(ErrorFieldName, out var elem)) return;
195
196 var info = elem.Deserialize<ErrorInfo>();
197 ErrorCode = info.Code ?? "";
198 Message = info.Message ?? "";
199
200 ConstraintFailures = info.ConstraintFailures;
201 Abort = info.Abort;
202 }
203}
Represents a failed query response.
string Message
The query failure message.
ConstraintFailure?[] ConstraintFailures
The constraint failures, if any. Only present for the constraint_failure error code.
string ErrorCode
The Fauna error code.
HttpStatusCode StatusCode
The HTTP status code.
object? Abort
The abort object, if any. Only present for the abort error code.
QueryFailure(HttpStatusCode statusCode, JsonElement json)
Initializes a new instance of the QueryFailure class, parsing the provided raw response text to extra...
Represents the response from a query executed.
JsonElement RawJson
The raw JSON of the query response.
QueryStats Stats
Gets the statistics related to the query execution.
long LastSeenTxn
Gets the last transaction seen by this query.
Dictionary< string, string > QueryTags
Gets a dictionary of query tags, providing additional context about the query.
string Summary
Gets a summary of the query execution.
static ? QueryResponse GetFromResponseBody< T >(MappingContext ctx, ISerializer< T > serializer, HttpStatusCode statusCode, string body)
Asynchronously parses the HTTP response message to create a QueryResponse instance.
long SchemaVersion
Gets the schema version.
Represents a successful query response.
QuerySuccess(MappingContext ctx, ISerializer< T > serializer, JsonElement json)
Initializes a new instance of the QuerySuccess<T> class, deserializing the query response into the sp...
string? StaticType
Gets the static type information from the query response, if available.
T Data
Gets the deserialized data from the query response.
A class representing a constraint failure from Fauna.
A class representing the mapping context to be used during serialization and deserialization.
A generic interface that defines serialize and deserialize behavior for a specific type,...
new T Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
Consumes all or some of a Utf8FaunaReader and returns an instance of T .
Contains detailed information about an error in a query response.
Definition ErrorInfo.cs:11
A struct representing stats aggregated across queries.
Represents a reader that provides fast, non-cached, forward-only access to serialized data.