| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="WebApiService.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Diagnostics.CodeAnalysis; |
| | | 10 | | using System.Globalization; |
| | | 11 | | using System.IO; |
| | | 12 | | using System.Linq; |
| | | 13 | | using System.Net; |
| | | 14 | | using System.Net.Http; |
| | | 15 | | using System.Net.Http.Headers; |
| | | 16 | | using System.Text; |
| | | 17 | | using System.Text.Json; |
| | | 18 | | using System.Text.Json.Nodes; |
| | | 19 | | using System.Threading; |
| | | 20 | | using System.Threading.Tasks; |
| | | 21 | | using MyNet.Http.Exceptions; |
| | | 22 | | using MyNet.Text; |
| | | 23 | | |
| | | 24 | | namespace MyNet.Http; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Provides methods to interact with web APIs using HTTP requests. The <see cref="WebApiService"/> class allows sending |
| | | 28 | | /// </summary> |
| | | 29 | | public sealed class WebApiService : IWebApiService |
| | | 30 | | { |
| | 3 | 31 | | private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; |
| | | 32 | | |
| | | 33 | | private readonly HttpClient _client; |
| | | 34 | | private readonly bool _disposeClient; |
| | | 35 | | private readonly TimeSpan _timeout; |
| | | 36 | | private readonly Func<object?, Exception>? _toException; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the <see cref="WebApiService"/> class. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="serverUrl">Target server URL.</param> |
| | | 42 | | /// <param name="timeout">Maximum request timeout duration.</param> |
| | | 43 | | /// <param name="headers">Custom HTTP headers.</param> |
| | | 44 | | /// <param name="toException">Function to convert a response to an exception.</param> |
| | | 45 | | public WebApiService(Uri? serverUrl = null, TimeSpan timeout = default, Dictionary<string, string>? headers = null, |
| | 0 | 46 | | : this(CreateHttpClient(serverUrl, headers), timeout, toException, disposeClient: true) |
| | | 47 | | { |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="WebApiService"/> class with a custom <see cref="HttpClient"/>. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="client">Configured HTTP client.</param> |
| | | 54 | | /// <param name="timeout">Maximum request timeout duration.</param> |
| | | 55 | | /// <param name="toException">Function to convert a response to an exception.</param> |
| | | 56 | | /// <param name="disposeClient">Indicates whether this service should dispose the HTTP client.</param> |
| | | 57 | | public WebApiService(HttpClient client, TimeSpan timeout = default, Func<object?, Exception>? toException = null, bo |
| | | 58 | | { |
| | 15 | 59 | | ArgumentNullException.ThrowIfNull(client); |
| | | 60 | | |
| | 15 | 61 | | _client = client; |
| | 15 | 62 | | _disposeClient = disposeClient; |
| | 15 | 63 | | _toException = toException; |
| | 15 | 64 | | _timeout = timeout != TimeSpan.Zero ? timeout : TimeSpan.FromMilliseconds(Timeout.Infinite); |
| | | 65 | | |
| | | 66 | | // We enforce timeouts per request via CancellationTokenSource.CancelAfter. |
| | 15 | 67 | | _client.Timeout = Timeout.InfiniteTimeSpan; |
| | 15 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc/> |
| | 0 | 71 | | public Task<Stream> GetStreamAsync(string str) => GetStreamAsync(new Uri(str, UriKind.RelativeOrAbsolute), Cancellat |
| | | 72 | | |
| | | 73 | | /// <inheritdoc/> |
| | 12 | 74 | | public Task<T> GetDataAsync<T>(string str, CancellationToken cancellationToken, params ApiParameter[] parameters) => |
| | | 75 | | |
| | | 76 | | /// <inheritdoc/> |
| | 12 | 77 | | public Task<T> GetDataAsync<T>(Uri uri, CancellationToken cancellationToken = default) => SendRequestAsync<T>(HttpMe |
| | | 78 | | |
| | | 79 | | /// <inheritdoc/> |
| | 0 | 80 | | public Task<TReturn> PostDataAsync<TParam, TReturn>(string str, TParam value, CancellationToken cancellationToken, p |
| | | 81 | | |
| | | 82 | | /// <inheritdoc/> |
| | 0 | 83 | | public Task<T> PostDataAsync<TParam, T>(Uri uri, TParam value, CancellationToken cancellationToken = default) => Sen |
| | | 84 | | |
| | | 85 | | /// <inheritdoc/> |
| | 3 | 86 | | public Task PostDataAsync<TParam>(string str, TParam value, CancellationToken cancellationToken, params ApiParameter |
| | | 87 | | |
| | | 88 | | /// <inheritdoc/> |
| | 3 | 89 | | public Task PostDataAsync<TParam>(Uri uri, TParam value, CancellationToken cancellationToken = default) => SendReque |
| | | 90 | | |
| | | 91 | | /// <inheritdoc/> |
| | 0 | 92 | | public Task PostDataAsync(string str, CancellationToken cancellationToken, params ApiParameter[] parameters) => Post |
| | | 93 | | |
| | | 94 | | /// <inheritdoc/> |
| | 0 | 95 | | public Task PostDataAsync(Uri uri, CancellationToken cancellationToken = default) => SendRequestAsync(HttpMethod.Pos |
| | | 96 | | |
| | | 97 | | /// <inheritdoc/> |
| | 0 | 98 | | public Task<TReturn> PutDataAsync<TParam, TReturn>(string str, TParam value, CancellationToken cancellationToken, pa |
| | | 99 | | |
| | | 100 | | /// <inheritdoc/> |
| | 0 | 101 | | public Task<T> PutDataAsync<TParam, T>(Uri uri, TParam value, CancellationToken cancellationToken = default) => Send |
| | | 102 | | |
| | | 103 | | /// <inheritdoc/> |
| | 0 | 104 | | public Task<TReturn> PatchDataAsync<TParam, TReturn>(string str, TParam value, CancellationToken cancellationToken, |
| | | 105 | | |
| | | 106 | | /// <inheritdoc/> |
| | 0 | 107 | | public Task<T> PatchDataAsync<TParam, T>(Uri uri, TParam value, CancellationToken cancellationToken = default) => Se |
| | | 108 | | |
| | | 109 | | /// <inheritdoc/> |
| | 0 | 110 | | public Task DeleteDataAsync(string str, CancellationToken cancellationToken, params ApiParameter[] parameters) => De |
| | | 111 | | |
| | | 112 | | /// <inheritdoc/> |
| | 0 | 113 | | public Task DeleteDataAsync(Uri uri, CancellationToken cancellationToken = default) => SendRequestAsync(HttpMethod.D |
| | | 114 | | |
| | | 115 | | /// <inheritdoc/> |
| | 0 | 116 | | public Task<TReturn> DeleteDataAsync<TParam, TReturn>(string str, TParam value, CancellationToken cancellationToken, |
| | | 117 | | |
| | | 118 | | /// <inheritdoc/> |
| | 0 | 119 | | public Task<T> DeleteDataAsync<TParam, T>(Uri uri, TParam value, CancellationToken cancellationToken = default) => S |
| | | 120 | | |
| | | 121 | | /// <inheritdoc/> |
| | 0 | 122 | | public Task DeleteDataAsync<TParam>(string str, TParam value, CancellationToken cancellationToken, params ApiParamet |
| | | 123 | | |
| | | 124 | | /// <inheritdoc/> |
| | 0 | 125 | | public Task DeleteDataAsync<TParam>(Uri uri, TParam value, CancellationToken cancellationToken = default) => SendReq |
| | | 126 | | |
| | | 127 | | /// <inheritdoc/> |
| | | 128 | | public void Dispose() |
| | | 129 | | { |
| | 15 | 130 | | if (_disposeClient) |
| | | 131 | | { |
| | 0 | 132 | | _client.Dispose(); |
| | | 133 | | } |
| | 15 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc/> |
| | | 137 | | public async Task SendRequestAsync(HttpMethod method, Uri uri, HttpContent? content = null, CancellationToken cancel |
| | | 138 | | { |
| | 3 | 139 | | using var request = CreateRequest(method, uri, content); |
| | 3 | 140 | | using var linkedCancellationToken = CreateLinkedTimeoutTokenSource(request, cancellationToken, out var tokenSour |
| | | 141 | | |
| | 3 | 142 | | HttpResponseMessage? response = null; |
| | | 143 | | try |
| | | 144 | | { |
| | 3 | 145 | | response = await _client.SendAsync(request, HttpCompletionOption.ResponseContentRead, linkedCancellationToke |
| | | 146 | | |
| | 3 | 147 | | if (response.IsSuccessStatusCode) |
| | | 148 | | { |
| | 3 | 149 | | return; |
| | | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | throw await GetExceptionAsync(response, linkedCancellationToken.Token).ConfigureAwait(false); |
| | | 153 | | } |
| | 0 | 154 | | catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) |
| | | 155 | | { |
| | 0 | 156 | | throw new TimeoutException(); |
| | | 157 | | } |
| | | 158 | | finally |
| | | 159 | | { |
| | 3 | 160 | | response?.Dispose(); |
| | 3 | 161 | | tokenSource.Dispose(); |
| | | 162 | | } |
| | 3 | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <inheritdoc/> |
| | | 166 | | public async Task<T> SendRequestAsync<T>(HttpMethod method, Uri uri, HttpContent? content = null, CancellationToken |
| | | 167 | | { |
| | 12 | 168 | | using var request = CreateRequest(method, uri, content); |
| | 12 | 169 | | using var linkedCancellationToken = CreateLinkedTimeoutTokenSource(request, cancellationToken, out var tokenSour |
| | | 170 | | |
| | 12 | 171 | | HttpResponseMessage? response = null; |
| | | 172 | | try |
| | | 173 | | { |
| | 12 | 174 | | response = await _client.SendAsync(request, HttpCompletionOption.ResponseContentRead, linkedCancellationToke |
| | 9 | 175 | | return !response.IsSuccessStatusCode |
| | 9 | 176 | | ? throw await GetExceptionAsync(response, linkedCancellationToken.Token).ConfigureAwait(false) |
| | 9 | 177 | | : await ReadResponseAsync<T>(response, linkedCancellationToken.Token).ConfigureAwait(false); |
| | | 178 | | } |
| | 3 | 179 | | catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) |
| | | 180 | | { |
| | 3 | 181 | | throw new TimeoutException(); |
| | | 182 | | } |
| | | 183 | | finally |
| | | 184 | | { |
| | 12 | 185 | | response?.Dispose(); |
| | 12 | 186 | | tokenSource.Dispose(); |
| | | 187 | | } |
| | 3 | 188 | | } |
| | | 189 | | |
| | | 190 | | private static HttpClient CreateHttpClient(Uri? serverUrl, Dictionary<string, string>? headers) |
| | | 191 | | { |
| | 0 | 192 | | var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip, CheckCertificateRevoca |
| | | 193 | | |
| | 0 | 194 | | var client = new HttpClient(handler) { BaseAddress = serverUrl }; |
| | | 195 | | |
| | 0 | 196 | | client.DefaultRequestHeaders.Accept.Clear(); |
| | 0 | 197 | | client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| | 0 | 198 | | client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/problem+json")); |
| | 0 | 199 | | client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain")); |
| | 0 | 200 | | client.DefaultRequestHeaders.AcceptEncoding.Add(StringWithQualityHeaderValue.Parse("gzip")); |
| | 0 | 201 | | ApplyHeaders(client.DefaultRequestHeaders, headers); |
| | | 202 | | |
| | 0 | 203 | | return client; |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | private async Task<Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken) |
| | | 207 | | { |
| | 0 | 208 | | using var request = CreateRequest(HttpMethod.Get, uri); |
| | 0 | 209 | | using var linkedCancellationToken = CreateLinkedTimeoutTokenSource(request, cancellationToken, out var tokenSour |
| | | 210 | | |
| | 0 | 211 | | HttpResponseMessage? response = null; |
| | | 212 | | try |
| | | 213 | | { |
| | 0 | 214 | | response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, linkedCancellationToke |
| | | 215 | | |
| | 0 | 216 | | if (!response.IsSuccessStatusCode) |
| | 0 | 217 | | throw await GetExceptionAsync(response, linkedCancellationToken.Token).ConfigureAwait(false); |
| | 0 | 218 | | var stream = await response.Content.ReadAsStreamAsync(linkedCancellationToken.Token).ConfigureAwait(false); |
| | 0 | 219 | | var responseToReturn = response; |
| | 0 | 220 | | response = null; |
| | | 221 | | |
| | 0 | 222 | | return new ResponseStream(stream, responseToReturn); |
| | | 223 | | } |
| | 0 | 224 | | catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) |
| | | 225 | | { |
| | 0 | 226 | | throw new TimeoutException(); |
| | | 227 | | } |
| | | 228 | | finally |
| | | 229 | | { |
| | 0 | 230 | | response?.Dispose(); |
| | 0 | 231 | | tokenSource.Dispose(); |
| | | 232 | | } |
| | 0 | 233 | | } |
| | | 234 | | |
| | | 235 | | [SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1204:Static members should appear before non-static members", J |
| | | 236 | | private static void ApplyHeaders(HttpRequestHeaders headers, Dictionary<string, string>? customHeaders) |
| | | 237 | | { |
| | 0 | 238 | | if (customHeaders is null) |
| | | 239 | | { |
| | 0 | 240 | | return; |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | foreach (var (key, value) in customHeaders) |
| | | 244 | | { |
| | 0 | 245 | | if (string.Equals(key, "Authorization", StringComparison.OrdinalIgnoreCase)) |
| | | 246 | | { |
| | 0 | 247 | | headers.Authorization = AuthenticationHeaderValue.Parse(value); |
| | 0 | 248 | | continue; |
| | | 249 | | } |
| | | 250 | | |
| | 0 | 251 | | if (!headers.TryAddWithoutValidation(key, value)) |
| | | 252 | | { |
| | 0 | 253 | | throw new InvalidOperationException($"Unable to add HTTP header '{key}'."); |
| | | 254 | | } |
| | | 255 | | } |
| | 0 | 256 | | } |
| | | 257 | | |
| | | 258 | | private static HttpRequestMessage CreateRequest(HttpMethod method, Uri uri, HttpContent? content = null) |
| | | 259 | | { |
| | 15 | 260 | | var request = new HttpRequestMessage(method, uri) { Content = content }; |
| | | 261 | | |
| | 15 | 262 | | request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue(CultureInfo.CurrentCulture.Name)); |
| | 15 | 263 | | return request; |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | private CancellationTokenSource CreateLinkedTimeoutTokenSource(HttpRequestMessage request, CancellationToken cancell |
| | | 267 | | { |
| | 15 | 268 | | tokenSource = new CancellationTokenSource(); |
| | 15 | 269 | | var linked = CancellationTokenSource.CreateLinkedTokenSource(tokenSource.Token, cancellationToken); |
| | 15 | 270 | | var effectiveTimeout = request.GetTimeout() ?? _timeout; |
| | | 271 | | |
| | 15 | 272 | | if (effectiveTimeout != Timeout.InfiniteTimeSpan) |
| | | 273 | | { |
| | 3 | 274 | | tokenSource.CancelAfter(effectiveTimeout); |
| | | 275 | | } |
| | | 276 | | |
| | 15 | 277 | | return linked; |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | [SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1204:Static members should appear before non-static members", J |
| | 3 | 281 | | private static StringContent CreateContent<TParam>(TParam value) => new(JsonSerializer.Serialize(value, JsonOptions) |
| | | 282 | | |
| | | 283 | | private static Uri ToWebUri(string str, ApiParameter[] parameters) |
| | 15 | 284 | | => parameters.Length == 0 && Uri.TryCreate(str, UriKind.RelativeOrAbsolute, out var uri) |
| | 15 | 285 | | ? uri |
| | 15 | 286 | | : str.ToWebUri([.. parameters.Select(x => (x.Key, x.Value))]); |
| | | 287 | | |
| | | 288 | | [SuppressMessage("Reliability", "CA2007:Appeler ConfigureAwait sur la tâche attendue", Justification = "Library code |
| | | 289 | | private static async Task<T> ReadResponseAsync<T>(HttpResponseMessage response, CancellationToken cancellationToken) |
| | | 290 | | { |
| | 3 | 291 | | if (typeof(T) == typeof(string)) |
| | | 292 | | { |
| | 0 | 293 | | return (T)(object)await response.Content |
| | 0 | 294 | | .ReadAsStringAsync(cancellationToken) |
| | 0 | 295 | | .ConfigureAwait(false); |
| | | 296 | | } |
| | | 297 | | |
| | 3 | 298 | | await using var stream = await response.Content |
| | 3 | 299 | | .ReadAsStreamAsync(cancellationToken) |
| | 3 | 300 | | .ConfigureAwait(false); |
| | | 301 | | |
| | 3 | 302 | | if (stream.Length == 0) |
| | | 303 | | { |
| | 0 | 304 | | throw new JsonException($"Response body is empty and cannot be deserialized to '{typeof(T)}'."); |
| | | 305 | | } |
| | | 306 | | |
| | 3 | 307 | | var result = await JsonSerializer.DeserializeAsync<T>(stream, JsonOptions, cancellationToken).ConfigureAwait(fal |
| | 3 | 308 | | return result ?? throw new JsonException($"Unable to deserialize response body to '{typeof(T)}'."); |
| | 3 | 309 | | } |
| | | 310 | | |
| | 6 | 311 | | private Task<Exception> GetExceptionAsync(HttpResponseMessage response, CancellationToken cancellationToken) => GetE |
| | | 312 | | |
| | | 313 | | private async Task<Exception> GetExceptionAsync(HttpStatusCode statusCode, string? reasonPhrase, HttpContent content |
| | | 314 | | { |
| | 6 | 315 | | string? text = null; |
| | | 316 | | try |
| | | 317 | | { |
| | 6 | 318 | | text = await content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); |
| | 6 | 319 | | } |
| | 0 | 320 | | catch |
| | | 321 | | { |
| | | 322 | | // ignore read failures: we still want to throw a meaningful exception. |
| | 0 | 323 | | } |
| | | 324 | | |
| | 6 | 325 | | var problemException = ProblemDetailsParser.TryParseException(text); |
| | 6 | 326 | | if (problemException is not null) |
| | | 327 | | { |
| | 3 | 328 | | return problemException; |
| | | 329 | | } |
| | | 330 | | |
| | 3 | 331 | | object? payload = null; |
| | 3 | 332 | | if (string.IsNullOrWhiteSpace(text)) |
| | 0 | 333 | | return _toException?.Invoke(payload) ?? new WebApiException(statusCode, reasonPhrase, text, payload); |
| | | 334 | | try |
| | | 335 | | { |
| | 3 | 336 | | payload = JsonNode.Parse(text); |
| | 0 | 337 | | } |
| | 3 | 338 | | catch (JsonException) |
| | | 339 | | { |
| | 3 | 340 | | payload = text; |
| | 3 | 341 | | } |
| | | 342 | | |
| | 3 | 343 | | return _toException?.Invoke(payload) ?? new WebApiException(statusCode, reasonPhrase, text, payload); |
| | 6 | 344 | | } |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// A stream wrapper that ensures the underlying HTTP response is disposed when the stream is disposed. This is nece |
| | | 348 | | /// </summary> |
| | | 349 | | /// <param name="inner">The inner stream to wrap.</param> |
| | | 350 | | /// <param name="response">The HTTP response message associated with the stream.</param> |
| | 0 | 351 | | private sealed class ResponseStream(Stream inner, HttpResponseMessage response) : Stream |
| | | 352 | | { |
| | | 353 | | /// <inheritdoc/> |
| | 0 | 354 | | public override bool CanRead => inner.CanRead; |
| | | 355 | | |
| | | 356 | | /// <inheritdoc/> |
| | 0 | 357 | | public override bool CanSeek => inner.CanSeek; |
| | | 358 | | |
| | | 359 | | /// <inheritdoc/> |
| | 0 | 360 | | public override bool CanWrite => inner.CanWrite; |
| | | 361 | | |
| | | 362 | | /// <inheritdoc/> |
| | 0 | 363 | | public override long Length => inner.Length; |
| | | 364 | | |
| | | 365 | | /// <inheritdoc/> |
| | | 366 | | public override long Position |
| | | 367 | | { |
| | 0 | 368 | | get => inner.Position; |
| | 0 | 369 | | set => inner.Position = value; |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | /// <inheritdoc/> |
| | 0 | 373 | | public override void Flush() => inner.Flush(); |
| | | 374 | | |
| | | 375 | | /// <inheritdoc/> |
| | 0 | 376 | | public override int Read(byte[] buffer, int offset, int count) => inner.Read(buffer, offset, count); |
| | | 377 | | |
| | | 378 | | /// <inheritdoc/> |
| | 0 | 379 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) = |
| | | 380 | | |
| | | 381 | | /// <inheritdoc/> |
| | 0 | 382 | | public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) => |
| | | 383 | | |
| | | 384 | | /// <inheritdoc/> |
| | 0 | 385 | | public override long Seek(long offset, SeekOrigin origin) => inner.Seek(offset, origin); |
| | | 386 | | |
| | | 387 | | /// <inheritdoc/> |
| | 0 | 388 | | public override void SetLength(long value) => inner.SetLength(value); |
| | | 389 | | |
| | | 390 | | /// <inheritdoc/> |
| | 0 | 391 | | public override void Write(byte[] buffer, int offset, int count) => inner.Write(buffer, offset, count); |
| | | 392 | | |
| | | 393 | | /// <inheritdoc/> |
| | 0 | 394 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => in |
| | | 395 | | |
| | | 396 | | /// <inheritdoc/> |
| | 0 | 397 | | public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) |
| | | 398 | | |
| | | 399 | | /// <inheritdoc/> |
| | | 400 | | protected override void Dispose(bool disposing) |
| | | 401 | | { |
| | 0 | 402 | | if (disposing) |
| | | 403 | | { |
| | 0 | 404 | | inner.Dispose(); |
| | 0 | 405 | | response.Dispose(); |
| | | 406 | | } |
| | | 407 | | |
| | 0 | 408 | | base.Dispose(disposing); |
| | 0 | 409 | | } |
| | | 410 | | |
| | | 411 | | /// <inheritdoc/> |
| | | 412 | | public override async ValueTask DisposeAsync() |
| | | 413 | | { |
| | | 414 | | try |
| | | 415 | | { |
| | 0 | 416 | | await inner.DisposeAsync().ConfigureAwait(false); |
| | 0 | 417 | | } |
| | | 418 | | finally |
| | | 419 | | { |
| | 0 | 420 | | response.Dispose(); |
| | | 421 | | } |
| | | 422 | | |
| | 0 | 423 | | await base.DisposeAsync().ConfigureAwait(false); |
| | 0 | 424 | | } |
| | | 425 | | } |
| | | 426 | | } |
| | | 427 | | |
| | | 428 | | /// <summary> |
| | | 429 | | /// Represents a key-value pair for API parameters, such as query parameters or form data. This struct provides implicit |
| | | 430 | | /// </summary> |
| | | 431 | | /// <param name="Key">The key of the API parameter.</param> |
| | | 432 | | /// <param name="Value">The value of the API parameter.</param> |
| | | 433 | | public record struct ApiParameter(string Key, string Value) |
| | | 434 | | { |
| | | 435 | | /// <summary> |
| | | 436 | | /// Defines an implicit conversion from an <see cref="ApiParameter"/> to a tuple of (string Key, string Value). This |
| | | 437 | | /// </summary> |
| | | 438 | | /// <param name="value">The <see cref="ApiParameter"/> instance to convert.</param> |
| | | 439 | | public static implicit operator (string Key, string Value)(ApiParameter value) => ToValueTuple(value); |
| | | 440 | | |
| | | 441 | | /// <summary> |
| | | 442 | | /// Defines an implicit conversion from a tuple of (string Key, string Value) to an <see cref="ApiParameter"/>. This |
| | | 443 | | /// </summary> |
| | | 444 | | /// <param name="value">The tuple representing the API parameter.</param> |
| | | 445 | | /// <returns>A new <see cref="ApiParameter"/> instance with the specified Key and Value.</returns> |
| | | 446 | | public static implicit operator ApiParameter((string Key, string Value) value) => ToApiParameter(value); |
| | | 447 | | |
| | | 448 | | /// <summary> |
| | | 449 | | /// Converts a tuple of (string Key, string Value) to an <see cref="ApiParameter"/>. This method is used by the impl |
| | | 450 | | /// </summary> |
| | | 451 | | /// <param name="value">The tuple representing the API parameter.</param> |
| | | 452 | | /// <returns>A new <see cref="ApiParameter"/> instance with the specified Key and Value.</returns> |
| | | 453 | | public static ApiParameter ToApiParameter((string Key, string Value) value) => new(value.Key, value.Value); |
| | | 454 | | |
| | | 455 | | /// <summary> |
| | | 456 | | /// Converts an <see cref="ApiParameter"/> to a tuple of (string Key, string Value). This method is used by the impl |
| | | 457 | | /// </summary> |
| | | 458 | | /// <param name="value">The <see cref="ApiParameter"/> instance to convert.</param> |
| | | 459 | | /// <returns>A tuple containing the Key and Value of the <see cref="ApiParameter"/> instance.</returns> |
| | | 460 | | public static (string Key, string Value) ToValueTuple(ApiParameter value) => (value.Key, value.Value); |
| | | 461 | | } |
| | | 462 | | |