| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using Kestrun.Models; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Callback; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Factory for creating <see cref="CallbackRuntimeContext"/> instances from HTTP context. |
| | | 8 | | /// </summary> |
| | | 9 | | public static partial class CallbackRuntimeContextFactory |
| | | 10 | | { |
| | | 11 | | // Matches {id} and {id:int} etc; ignores {$request.body#/...} because of / and # |
| | 1 | 12 | | private static readonly Regex TemplateParamRegex = |
| | 1 | 13 | | TemplateParameterRegex(); |
| | | 14 | | |
| | | 15 | | private static HashSet<string> ExtractTemplateParams(string urlTemplate) |
| | | 16 | | { |
| | 2 | 17 | | var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 2 | 18 | | if (string.IsNullOrWhiteSpace(urlTemplate)) |
| | | 19 | | { |
| | 0 | 20 | | return set; |
| | | 21 | | } |
| | | 22 | | |
| | 8 | 23 | | foreach (Match m in TemplateParamRegex.Matches(urlTemplate)) |
| | | 24 | | { |
| | 2 | 25 | | var name = m.Groups["name"].Value.Trim(); |
| | 2 | 26 | | if (!string.IsNullOrEmpty(name)) |
| | | 27 | | { |
| | 2 | 28 | | _ = set.Add(name); |
| | | 29 | | } |
| | | 30 | | } |
| | | 31 | | |
| | 2 | 32 | | return set; |
| | | 33 | | } |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates a <see cref="CallbackRuntimeContext"/> from the given <see cref="HttpContext"/>. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="ctx">The HTTP context from which to create the callback runtime context.</param> |
| | | 38 | | /// <param name="urlTemplate">An optional URL template to extract template parameters for idempotency key generation |
| | | 39 | | /// <returns>A new instance of <see cref="CallbackRuntimeContext"/> populated with data from the HTTP context.</retu |
| | | 40 | | public static CallbackRuntimeContext FromHttpContext(KestrunContext ctx, string? urlTemplate = null) |
| | | 41 | | { |
| | 5 | 42 | | ArgumentNullException.ThrowIfNull(ctx); |
| | | 43 | | |
| | 5 | 44 | | var correlationId = ctx.TraceIdentifier; |
| | | 45 | | |
| | | 46 | | // Vars come from resolved OpenAPI parameters (this is the key fix) |
| | 5 | 47 | | var vars = new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase); |
| | | 48 | | |
| | 16 | 49 | | foreach (var kv in ctx.Parameters.Parameters) |
| | | 50 | | { |
| | 3 | 51 | | vars[kv.Key] = kv.Value.Value; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // Typed body is already resolved |
| | 5 | 55 | | var requestBody = ctx.Parameters.Body?.Value; |
| | | 56 | | |
| | | 57 | | // Idempotency seed: derived from placeholders in the callback URL template (if provided) |
| | | 58 | | string idempotencySeed; |
| | 5 | 59 | | if (!string.IsNullOrWhiteSpace(urlTemplate)) |
| | | 60 | | { |
| | 2 | 61 | | var names = ExtractTemplateParams(urlTemplate); |
| | | 62 | | |
| | 2 | 63 | | var seedParts = names |
| | 2 | 64 | | .OrderBy(n => n, StringComparer.OrdinalIgnoreCase) |
| | 2 | 65 | | .Select(n => |
| | 2 | 66 | | { |
| | 2 | 67 | | if (!vars.TryGetValue(n, out var v) || v is null) |
| | 2 | 68 | | { |
| | 1 | 69 | | return null; |
| | 2 | 70 | | } |
| | 2 | 71 | | |
| | 1 | 72 | | var s = v.ToString(); |
| | 1 | 73 | | return string.IsNullOrWhiteSpace(s) ? null : $"{n}={s}"; |
| | 2 | 74 | | }) |
| | 2 | 75 | | .Where(x => x is not null) |
| | 2 | 76 | | .ToArray(); |
| | | 77 | | |
| | 2 | 78 | | idempotencySeed = seedParts.Length > 0 |
| | 2 | 79 | | ? string.Join("&", seedParts) |
| | 2 | 80 | | : correlationId; |
| | | 81 | | } |
| | | 82 | | else |
| | | 83 | | { |
| | | 84 | | // No template => don't guess which param matters |
| | 3 | 85 | | idempotencySeed = correlationId; |
| | | 86 | | } |
| | | 87 | | |
| | 5 | 88 | | return new CallbackRuntimeContext( |
| | 5 | 89 | | CorrelationId: correlationId, |
| | 5 | 90 | | IdempotencyKeySeed: idempotencySeed, |
| | 5 | 91 | | DefaultBaseUri: null, |
| | 5 | 92 | | Vars: vars, |
| | 5 | 93 | | CallbackPayload: requestBody // <-- typed body goes here |
| | 5 | 94 | | ); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | [GeneratedRegex(@"\{(?<name>[^{}:/\?]+)(?:[:][^{}]+)?\}", RegexOptions.Compiled)] |
| | | 98 | | private static partial Regex TemplateParameterRegex(); |
| | | 99 | | } |
| | | 100 | | |