| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | using NexusLabs.Needlr.Generators.Models; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.Generators.CodeGen; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Emits the body of the <c>RegisterHttpClients</c> method for discovered |
| | | 13 | | /// <c>[HttpClientOptions]</c> types. Each discovered type produces: |
| | | 14 | | /// <list type="number"> |
| | | 15 | | /// <item><description>An <c>AddOptions<T>().BindConfiguration(section)</c> call so <c>IOptions<T></c> and f |
| | | 16 | | /// <item><description>A <c>services.AddHttpClient(clientName, (sp, client) => { ... })</c> call whose body condition |
| | | 17 | | /// </list> |
| | | 18 | | /// The conditional emission is the load-bearing design choice: future capabilities |
| | | 19 | | /// (resilience, handler chain, handler lifetime, etc.) are added by introducing a |
| | | 20 | | /// new capability flag + a new emission block here, with no change to the attribute |
| | | 21 | | /// or existing consumers. |
| | | 22 | | /// </summary> |
| | | 23 | | internal static class HttpClientCodeGenerator |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Emits HttpClient registration statements into the supplied <see cref="StringBuilder"/>. |
| | | 27 | | /// The caller is responsible for opening/closing the containing method. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="builder">The builder to append to (already positioned inside a method body).</param> |
| | | 30 | | /// <param name="httpClients">The discovered HttpClient options types to emit registrations for.</param> |
| | | 31 | | public static void EmitHttpClientRegistrations( |
| | | 32 | | StringBuilder builder, |
| | | 33 | | IReadOnlyList<DiscoveredHttpClient> httpClients) |
| | | 34 | | { |
| | 0 | 35 | | foreach (var http in httpClients) |
| | | 36 | | { |
| | 0 | 37 | | builder.AppendLine(); |
| | 0 | 38 | | builder.AppendLine($" // Named HttpClient: \"{http.ClientName}\" (bound to \"{http.SectionName}\")"); |
| | | 39 | | |
| | | 40 | | // Step 1: IOptions<T> binding so consumers can inject IOptions<WebFetchHttpClientOptions> |
| | | 41 | | // if they need runtime access to the record alongside the HttpClient. |
| | 0 | 42 | | builder.AppendLine($" services.AddOptions<{http.TypeName}>().BindConfiguration(\"{http.SectionName}\" |
| | | 43 | | |
| | | 44 | | // Step 2: the named AddHttpClient call with a capability-driven configuration callback. |
| | 0 | 45 | | builder.AppendLine($" services.AddHttpClient(\"{EscapeStringLiteral(http.ClientName)}\", (sp, client) |
| | 0 | 46 | | builder.AppendLine(" {"); |
| | 0 | 47 | | builder.AppendLine($" var options = sp.GetRequiredService<global::Microsoft.Extensions.Options.IO |
| | | 48 | | |
| | 0 | 49 | | if ((http.Capabilities & HttpClientCapabilities.Timeout) != 0) |
| | | 50 | | { |
| | 0 | 51 | | builder.AppendLine(" client.Timeout = options.Timeout;"); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | if ((http.Capabilities & HttpClientCapabilities.BaseAddress) != 0) |
| | | 55 | | { |
| | 0 | 56 | | builder.AppendLine(" if (options.BaseAddress is not null)"); |
| | 0 | 57 | | builder.AppendLine(" {"); |
| | 0 | 58 | | builder.AppendLine(" client.BaseAddress = options.BaseAddress;"); |
| | 0 | 59 | | builder.AppendLine(" }"); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | if ((http.Capabilities & HttpClientCapabilities.UserAgent) != 0) |
| | | 63 | | { |
| | 0 | 64 | | builder.AppendLine(" if (!string.IsNullOrEmpty(options.UserAgent))"); |
| | 0 | 65 | | builder.AppendLine(" {"); |
| | 0 | 66 | | builder.AppendLine(" client.DefaultRequestHeaders.UserAgent.ParseAdd(options.UserAgent);" |
| | 0 | 67 | | builder.AppendLine(" }"); |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | if ((http.Capabilities & HttpClientCapabilities.Headers) != 0) |
| | | 71 | | { |
| | 0 | 72 | | builder.AppendLine(" if (options.DefaultHeaders is not null)"); |
| | 0 | 73 | | builder.AppendLine(" {"); |
| | 0 | 74 | | builder.AppendLine(" foreach (var kvp in options.DefaultHeaders)"); |
| | 0 | 75 | | builder.AppendLine(" {"); |
| | 0 | 76 | | builder.AppendLine(" client.DefaultRequestHeaders.Add(kvp.Key, kvp.Value);"); |
| | 0 | 77 | | builder.AppendLine(" }"); |
| | 0 | 78 | | builder.AppendLine(" }"); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | builder.AppendLine(" });"); |
| | | 82 | | } |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | private static string EscapeStringLiteral(string value) |
| | | 86 | | { |
| | 0 | 87 | | return value.Replace("\\", "\\\\").Replace("\"", "\\\""); |
| | | 88 | | } |
| | | 89 | | } |