5 using System.Net.Http.Headers;
6 using System.Threading;
7 using System.Threading.Tasks;
17 private readonly HttpClient _client;
18 private readonly
bool _ownsClient;
28 TimeSpan? pooledConnectionLifetime =
null,
29 TimeSpan? pooledConnectionIdleTimeout =
null)
31 var handler =
new SocketsHttpHandler
34 PooledConnectionLifetime = pooledConnectionLifetime ?? TimeSpan.FromMinutes(2),
35 PooledConnectionIdleTimeout = pooledConnectionIdleTimeout ?? TimeSpan.FromMinutes(2),
38 AutomaticDecompression = DecompressionMethods.None,
40 AllowAutoRedirect =
false,
43 _client =
new HttpClient(handler, disposeHandler:
true)
67 string? authorization,
68 CancellationToken cancellationToken)
70 using var request = BuildRequest(url, body, contentType, authorization);
71 using var response = _client.Send(request, cancellationToken);
72 return ReadOrThrow(response, cancellationToken);
82 string? authorization,
83 CancellationToken cancellationToken)
85 using var request = BuildRequest(url, body, contentType, authorization);
86 using var response = await _client
87 .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
88 .ConfigureAwait(
false);
90 var bytes = await response.Content
91 .ReadAsByteArrayAsync(cancellationToken)
92 .ConfigureAwait(
false);
94 if (response.IsSuccessStatusCode)
100 private static HttpRequestMessage BuildRequest(
104 string? authorization)
106 var request =
new HttpRequestMessage(HttpMethod.Post, url)
108 Content =
new ByteArrayContent(body),
112 Version = HttpVersion.Version11,
114 request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
115 request.Content.Headers.ContentLength = body.Length;
117 if (!
string.IsNullOrEmpty(authorization))
119 var space = authorization.IndexOf(
' ');
122 request.Headers.Authorization =
new AuthenticationHeaderValue(
123 authorization[..space],
124 authorization[(space + 1)..]);
129 request.Headers.Add(
"Authorization", authorization);
136 private static byte[] ReadOrThrow(
137 HttpResponseMessage response,
138 CancellationToken cancellationToken)
140 using var stream = response.Content.ReadAsStream(cancellationToken);
141 using var buffer =
new MemoryStream();
142 stream.CopyTo(buffer);
143 var bytes = buffer.ToArray();
145 if (response.IsSuccessStatusCode)
180 : base($
"Kinetica server returned HTTP {statusCode}.")
int StatusCode
HTTP status code from the server response.
byte [] Body
Raw response body bytes (may contain Avro-encoded error message).
byte [] Post(string url, byte[] body, string contentType, string? authorization, CancellationToken cancellationToken)
Synchronous POST request.
Thrown by HttpClientTransport when the server responds with a non-2xx status code.
Abstraction over the raw HTTP POST layer.
async Task< byte[]> PostAsync(string url, byte[] body, string contentType, string? authorization, CancellationToken cancellationToken)
Asynchronous POST request.
IHttpTransport implementation backed by HttpClient
HttpClientTransport(TimeSpan timeout, TimeSpan? pooledConnectionLifetime=null, TimeSpan? pooledConnectionIdleTimeout=null)
Creates a new HttpClientTransport with configurable timeout and connection pooling.
KineticaTransportException(int statusCode, byte[] body)