-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathEsqlQueryRequest.cs
71 lines (57 loc) · 2 KB
/
EsqlQueryRequest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Elastic.Transport;
#if ELASTICSEARCH_SERVERLESS
namespace Elastic.Clients.Elasticsearch.Serverless.Esql;
#else
namespace Elastic.Clients.Elasticsearch.Esql;
#endif
internal sealed class EsqlResponseBuilder : CustomResponseBuilder
{
public override object DeserializeResponse(Serializer serializer, ApiCallDetails response, Stream stream)
{
var bytes = stream switch
{
MemoryStream ms => ms.ToArray(),
_ => BytesFromStream(stream)
};
return new EsqlQueryResponse { Data = bytes };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static byte[] BytesFromStream(Stream stream)
{
using var binaryReader = new BinaryReader(stream);
return binaryReader.ReadBytes((int)stream.Length);
}
}
public override async Task<object> DeserializeResponseAsync(Serializer serializer, ApiCallDetails response, Stream stream,
CancellationToken ctx = new CancellationToken())
{
var bytes = stream switch
{
MemoryStream ms => ms.ToArray(),
_ => await BytesFromStreamAsync(stream, ctx).ConfigureAwait(false)
};
return new EsqlQueryResponse { Data = bytes };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static async Task<byte[]> BytesFromStreamAsync(Stream stream, CancellationToken cancellationToken)
{
using var ms = new MemoryStream();
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
await stream.CopyToAsync(ms, cancellationToken).ConfigureAwait(false);
#else
await stream.CopyToAsync(ms).ConfigureAwait(false);
#endif
return ms.ToArray();
}
}
}
public sealed partial class EsqlQueryRequestParameters
{
private static readonly EsqlResponseBuilder ResponseBuilder = new();
public EsqlQueryRequestParameters() => CustomResponseBuilder = ResponseBuilder;
}