|
| 1 | +using System.Security.Cryptography.X509Certificates; |
| 2 | +using Npgsql; |
| 3 | +using NpgsqlRest.Defaults; |
| 4 | + |
| 5 | +namespace NpgsqlRest; |
| 6 | + |
| 7 | +public class NpgsqlRestMetadataEntry |
| 8 | +{ |
| 9 | + internal NpgsqlRestMetadataEntry( |
| 10 | + Routine routine, |
| 11 | + RoutineEndpoint endpoint, |
| 12 | + IRoutineSourceParameterFormatter formatter, |
| 13 | + string key) |
| 14 | + { |
| 15 | + Routine = routine; |
| 16 | + Endpoint = endpoint; |
| 17 | + Formatter = formatter; |
| 18 | + Key = key; |
| 19 | + } |
| 20 | + |
| 21 | + public Routine Routine { get; } |
| 22 | + public RoutineEndpoint Endpoint { get; } |
| 23 | + public IRoutineSourceParameterFormatter Formatter { get; } |
| 24 | + public string Key { get; } |
| 25 | +} |
| 26 | + |
| 27 | +public class NpgsqlRestMetadata |
| 28 | +{ |
| 29 | + internal NpgsqlRestMetadata( |
| 30 | + Dictionary<string, NpgsqlRestMetadataEntry> entries, |
| 31 | + Dictionary<string, NpgsqlRestMetadataEntry> overloads) |
| 32 | + { |
| 33 | + Entries = entries; |
| 34 | + Overloads = overloads; |
| 35 | + } |
| 36 | + |
| 37 | + public Dictionary<string, NpgsqlRestMetadataEntry> Entries { get; } |
| 38 | + public Dictionary<string, NpgsqlRestMetadataEntry> Overloads { get; } |
| 39 | +} |
| 40 | + |
| 41 | +public static class NpgsqlRestMetadataBuilder |
| 42 | +{ |
| 43 | + public const int MaxKeyLength = 2056; |
| 44 | + public const int MaxPathLength = 2048; |
| 45 | + public static NpgsqlRestMetadata Build(NpgsqlRestOptions options, ILogger? logger, IApplicationBuilder? builder) |
| 46 | + { |
| 47 | + Dictionary<string, NpgsqlRestMetadataEntry> lookup = []; |
| 48 | + Dictionary<string, NpgsqlRestMetadataEntry> overloads = []; |
| 49 | + |
| 50 | + var hasLogin = false; |
| 51 | + if (builder is not null) |
| 52 | + { |
| 53 | + foreach (var handler in options.EndpointCreateHandlers) |
| 54 | + { |
| 55 | + handler.Setup(builder, logger, options); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + options.SourcesCreated(options.RoutineSources); |
| 60 | + |
| 61 | + CommentsMode optionsCommentsMode = options.CommentsMode; |
| 62 | + foreach (var source in options.RoutineSources) |
| 63 | + { |
| 64 | + if (source.CommentsMode.HasValue) |
| 65 | + { |
| 66 | + options.CommentsMode = source.CommentsMode.Value; |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + options.CommentsMode = optionsCommentsMode; |
| 71 | + } |
| 72 | + foreach (var (routine, formatter) in source.Read(options, builder?.ApplicationServices)) |
| 73 | + { |
| 74 | + RoutineEndpoint endpoint = DefaultEndpoint.Create(routine, options, logger)!; |
| 75 | + |
| 76 | + if (endpoint is null) |
| 77 | + { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + if (options.EndpointCreated is not null) |
| 82 | + { |
| 83 | + endpoint = options.EndpointCreated(routine, endpoint)!; |
| 84 | + } |
| 85 | + |
| 86 | + if (endpoint is null) |
| 87 | + { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + if (endpoint.Url.Length > MaxPathLength) |
| 92 | + { |
| 93 | + throw new ArgumentException($"URL path for URL {endpoint.Url}, routine {routine.Name} length exceeds {MaxPathLength} characters."); |
| 94 | + } |
| 95 | + |
| 96 | + var method = endpoint.Method.ToString(); |
| 97 | + if (endpoint.HasBodyParameter is true && endpoint.RequestParamType == RequestParamType.BodyJson) |
| 98 | + { |
| 99 | + endpoint.RequestParamType = RequestParamType.QueryString; |
| 100 | + logger?.EndpointTypeChanged(method, endpoint.Url, endpoint!.BodyParameterName ?? ""); |
| 101 | + |
| 102 | + } |
| 103 | + var key = string.Concat(method, endpoint?.Url); |
| 104 | + var value = new NpgsqlRestMetadataEntry(routine, endpoint!, formatter, key); |
| 105 | + if (lookup.TryGetValue(key, out var existing)) |
| 106 | + { |
| 107 | + overloads[string.Concat(key, existing.Routine.ParamCount)] = existing; |
| 108 | + } |
| 109 | + lookup[key] = value; |
| 110 | + |
| 111 | + if (builder is not null) |
| 112 | + { |
| 113 | + foreach (var handler in options.EndpointCreateHandlers) |
| 114 | + { |
| 115 | + handler.Handle(routine, endpoint!); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (options.LogEndpointCreatedInfo) |
| 120 | + { |
| 121 | + logger?.EndpointCreated(method, endpoint!.Url); |
| 122 | + } |
| 123 | + |
| 124 | + if (endpoint?.Login is true) |
| 125 | + { |
| 126 | + if (hasLogin is false) |
| 127 | + { |
| 128 | + hasLogin = true; |
| 129 | + } |
| 130 | + if (routine.IsVoid is true || routine.ReturnsUnnamedSet is true) |
| 131 | + { |
| 132 | + throw new ArgumentException($"{routine.Type.ToString().ToLowerInvariant()} {routine.Schema}.{routine.Name} is marked as login and it can't be void or returning unnamed data sets."); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (hasLogin is true) |
| 139 | + { |
| 140 | + if (options.AuthenticationOptions.DefaultAuthenticationType is null) |
| 141 | + { |
| 142 | + string db = new NpgsqlConnectionStringBuilder(options.ConnectionString).Database ?? "NpgsqlRest"; |
| 143 | + options.AuthenticationOptions.DefaultAuthenticationType = db; |
| 144 | + logger?.SetDefaultAuthenticationType(db); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (options.EndpointsCreated is not null) |
| 149 | + { |
| 150 | + options.EndpointsCreated(lookup.Values.Select(x => (x.Routine, x.Endpoint)).ToArray()); |
| 151 | + } |
| 152 | + |
| 153 | + if (builder is not null) |
| 154 | + { |
| 155 | + (Routine routine, RoutineEndpoint endpoint)[]? array = null; |
| 156 | + foreach (var handler in options.EndpointCreateHandlers) |
| 157 | + { |
| 158 | + array ??= lookup.Values.Select(x => (x.Routine, x.Endpoint)).ToArray(); |
| 159 | + handler.Cleanup(array); |
| 160 | + handler.Cleanup(); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return new NpgsqlRestMetadata(lookup, overloads); |
| 165 | + } |
| 166 | +} |
0 commit comments