|
| 1 | +using System.Net.Http.Headers; |
| 2 | +using System.Text; |
| 3 | +using System.Text.Json; |
| 4 | + |
| 5 | +namespace Termii; |
| 6 | + |
| 7 | +internal sealed class TermiiJsonHttpPipeline |
| 8 | +{ |
| 9 | + private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web) |
| 10 | + { |
| 11 | + DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull, |
| 12 | + }; |
| 13 | + |
| 14 | + private readonly HttpClient _httpClient; |
| 15 | + private readonly TermiiOptions _options; |
| 16 | + |
| 17 | + public TermiiJsonHttpPipeline(HttpClient httpClient, TermiiOptions options) |
| 18 | + { |
| 19 | + _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); |
| 20 | + _options = options ?? throw new ArgumentNullException(nameof(options)); |
| 21 | + } |
| 22 | + |
| 23 | + public Task<HttpResponseMessage> SendAsync( |
| 24 | + HttpMethod method, |
| 25 | + string path, |
| 26 | + object? body, |
| 27 | + TermiiAuthenticationLocation authenticationLocation, |
| 28 | + CancellationToken cancellationToken) |
| 29 | + { |
| 30 | + if (method is null) |
| 31 | + { |
| 32 | + throw new ArgumentNullException(nameof(method)); |
| 33 | + } |
| 34 | + |
| 35 | + if (string.IsNullOrWhiteSpace(path)) |
| 36 | + { |
| 37 | + throw new ArgumentException("A request path is required.", nameof(path)); |
| 38 | + } |
| 39 | + |
| 40 | + var requestUri = authenticationLocation == TermiiAuthenticationLocation.Query |
| 41 | + ? AppendApiKey(path) |
| 42 | + : path; |
| 43 | + |
| 44 | + var request = new HttpRequestMessage(method, requestUri); |
| 45 | + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 46 | + |
| 47 | + if (body is not null || authenticationLocation == TermiiAuthenticationLocation.Body) |
| 48 | + { |
| 49 | + request.Content = CreateJsonContent(body, authenticationLocation); |
| 50 | + } |
| 51 | + |
| 52 | + return _httpClient.SendAsync(request, cancellationToken); |
| 53 | + } |
| 54 | + |
| 55 | + private string AppendApiKey(string path) |
| 56 | + { |
| 57 | + var separator = path.IndexOf("?", StringComparison.Ordinal) >= 0 ? "&" : "?"; |
| 58 | + |
| 59 | + return $"{path}{separator}api_key={Uri.EscapeDataString(_options.ApiKey)}"; |
| 60 | + } |
| 61 | + |
| 62 | + private HttpContent CreateJsonContent(object? body, TermiiAuthenticationLocation authenticationLocation) |
| 63 | + { |
| 64 | + var json = authenticationLocation == TermiiAuthenticationLocation.Body |
| 65 | + ? SerializeBodyWithApiKey(body) |
| 66 | + : JsonSerializer.Serialize(body, JsonSerializerOptions); |
| 67 | + |
| 68 | + return new StringContent(json, Encoding.UTF8, "application/json"); |
| 69 | + } |
| 70 | + |
| 71 | + private string SerializeBodyWithApiKey(object? body) |
| 72 | + { |
| 73 | + using var stream = new MemoryStream(); |
| 74 | + |
| 75 | + using (var writer = new Utf8JsonWriter(stream)) |
| 76 | + { |
| 77 | + writer.WriteStartObject(); |
| 78 | + writer.WriteString("api_key", _options.ApiKey); |
| 79 | + |
| 80 | + if (body is not null) |
| 81 | + { |
| 82 | + using var document = JsonDocument.Parse(JsonSerializer.Serialize(body, JsonSerializerOptions)); |
| 83 | + |
| 84 | + if (document.RootElement.ValueKind != JsonValueKind.Object) |
| 85 | + { |
| 86 | + throw new InvalidOperationException("Termii JSON request bodies must serialize to an object."); |
| 87 | + } |
| 88 | + |
| 89 | + foreach (var property in document.RootElement.EnumerateObject()) |
| 90 | + { |
| 91 | + if (string.Equals(property.Name, "api_key", StringComparison.OrdinalIgnoreCase)) |
| 92 | + { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + property.WriteTo(writer); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + writer.WriteEndObject(); |
| 101 | + } |
| 102 | + |
| 103 | + return Encoding.UTF8.GetString(stream.ToArray()); |
| 104 | + } |
| 105 | +} |
0 commit comments