Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
QueryResponse.cs
Go to the documentation of this file.
1using System.Net;
2using System.Text.Json;
3using Fauna.Mapping;
6
7namespace Fauna;
8
12public abstract class QueryResponse
13{
14 public JsonElement RawJson { get; init; }
15
19 public long LastSeenTxn { get; init; }
20
24 public long SchemaVersion { get; init; }
25
29 public string Summary { get; init; } = "";
30
34 public Dictionary<string, string> QueryTags { get; init; } = new();
35
39 public QueryStats Stats { get; init; }
40
41 internal QueryResponse(JsonElement json)
42 {
43 RawJson = json;
44
45 if (json.TryGetProperty(LastSeenTxnFieldName, out var elem))
46 {
47 if (elem.TryGetInt64(out var i)) LastSeenTxn = i;
48 }
49
50 if (json.TryGetProperty(SchemaVersionFieldName, out elem))
51 {
52 if (elem.TryGetInt64(out var i)) LastSeenTxn = i;
53 }
54
55 if (json.TryGetProperty(SummaryFieldName, out elem))
56 {
57 Summary = elem.GetString() ?? "";
58 }
59
60
61 if (json.TryGetProperty(QueryTagsFieldName, out elem))
62 {
63 var queryTagsString = elem.GetString();
64
65 if (!string.IsNullOrEmpty(queryTagsString))
66 {
67 var tagPairs = queryTagsString.Split(',').Select(tag =>
68 {
69 var tokens = tag.Split('=');
70 return KeyValuePair.Create(tokens[0], tokens[1]);
71 });
72
73 QueryTags = new Dictionary<string, string>(tagPairs);
74 }
75 }
76
77 if (json.TryGetProperty(StatsFieldName, out elem))
78 {
79 Stats = elem.Deserialize<QueryStats>();
80 }
81 }
82
94 ISerializer<T> serializer,
95 HttpStatusCode statusCode,
96 string body)
97 {
98 try
99 {
100 var json = JsonSerializer.Deserialize<JsonElement>(body);
101
102 if (statusCode is >= HttpStatusCode.OK and <= (HttpStatusCode)299)
103 {
104 return new QuerySuccess<T>(ctx, serializer, json);
105 }
106
107 return new QueryFailure(statusCode, json);
108 }
109 catch (JsonException)
110 {
111 return null;
112 }
113 }
114}
115
120public sealed class QuerySuccess<T> : QueryResponse
121{
125 public T Data { get; init; }
126
130 public string? StaticType { get; init; }
131
139 MappingContext ctx,
140 ISerializer<T> serializer,
141 JsonElement json)
142 : base(json)
143 {
144 var dataText = json.GetProperty(DataFieldName).GetRawText();
145 var reader = new Utf8FaunaReader(dataText);
146 reader.Read();
147 Data = serializer.Deserialize(ctx, ref reader);
148
149 if (json.TryGetProperty(StaticTypeFieldName, out var elem))
150 {
151 StaticType = elem.GetString();
152 }
153 }
154}
155
159public sealed class QueryFailure : QueryResponse
160{
161 public HttpStatusCode StatusCode { get; init; }
162 public string ErrorCode { get; init; } = "";
163 public string Message { get; init; } = "";
164 public object? ConstraintFailures { get; init; }
165 public object? Abort { get; init; }
166
172 public QueryFailure(HttpStatusCode statusCode, JsonElement json) : base(json)
173 {
174 StatusCode = statusCode;
175 if (!json.TryGetProperty(ErrorFieldName, out var elem)) return;
176
177 var info = elem.Deserialize<ErrorInfo>();
178 ErrorCode = info.Code ?? "";
179 Message = info.Message ?? "";
180 ConstraintFailures = info.ConstraintFailures;
181 Abort = info.Abort;
182 }
183}
A class representing the mapping context to be used during serialization and deserialization.
Represents a failed query response.
QueryFailure(HttpStatusCode statusCode, JsonElement json)
Initializes a new instance of the QueryFailure class, parsing the provided raw response text to extra...
HttpStatusCode StatusCode
Represents the response from a query executed.
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.
long LastSeenTxn
Gets the last transaction seen by this query.
long SchemaVersion
Gets the schema version.
static ? QueryResponse GetFromResponseBody< T >(MappingContext ctx, ISerializer< T > serializer, HttpStatusCode statusCode, string body)
Asynchronously parses the HTTP response message to create a QueryResponse instance.
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...
T Data
Gets the deserialized data from the query response.
string? StaticType
Gets the static type information from the query response, if available.
new T Deserialize(MappingContext context, ref Utf8FaunaReader reader)
Definition Client.cs:8
Contains detailed information about an error in a query response.
Definition ErrorInfo.cs:10
string? Code
The error code when a query fails.
Definition ErrorInfo.cs:15
Contains statistics related to the execution of a query in the Fauna database.
Definition QueryStats.cs:10
Represents a reader that provides fast, non-cached, forward-only access to serialized data.