forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlRestMetadataBuilder.cs
More file actions
236 lines (205 loc) · 9.5 KB
/
Copy pathNpgsqlRestMetadataBuilder.cs
File metadata and controls
236 lines (205 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System.Threading.Channels;
using Npgsql;
using NpgsqlRest.Defaults;
using NpgsqlRest.UploadHandlers;
namespace NpgsqlRest;
public static class NpgsqlRestMetadataBuilder
{
public const int MaxKeyLength = 2056;
public const int MaxPathLength = 2048;
public static NpgsqlRestMetadata Build(NpgsqlRestOptions options, ILogger? logger, IApplicationBuilder? builder)
{
Dictionary<string, NpgsqlRestMetadataEntry> lookup = [];
Dictionary<string, NpgsqlRestMetadataEntry> overloads = [];
// Create default upload handlers from upload handler options
options.UploadOptions.UploadHandlers ??= options.UploadOptions.CreateUploadHandlers();
var hasLogin = false;
if (builder is not null)
{
foreach (var handler in options.EndpointCreateHandlers)
{
handler.Setup(builder, logger, options);
}
}
options.SourcesCreated(options.RoutineSources);
var hasCachedRoutine = false;
CommentsMode optionsCommentsMode = options.CommentsMode;
bool hasStreamingEvents = false;
foreach (var source in options.RoutineSources)
{
if (source.CommentsMode.HasValue)
{
options.CommentsMode = source.CommentsMode.Value;
}
else
{
options.CommentsMode = optionsCommentsMode;
}
RetryStrategy? defaultStrategy = null;
if (options.CommandRetryOptions.Enabled is true && string.IsNullOrEmpty(options.CommandRetryOptions.DefaultStrategy) is false)
{
if (options.CommandRetryOptions.Strategies
.TryGetValue(options.CommandRetryOptions.DefaultStrategy, out defaultStrategy) is false)
{
logger?.LogWarning("Default retry strategy {defaultStrategy} not found in the list of strategies, command retry strategy will be ignored.",
options.CommandRetryOptions.DefaultStrategy);
}
}
foreach (var (routine, formatter) in source.Read(options, builder?.ApplicationServices, defaultStrategy, logger))
{
RoutineEndpoint endpoint = DefaultEndpoint.Create(routine, options, logger)!;
if (endpoint is null)
{
continue;
}
if (options.EndpointCreated is not null)
{
options.EndpointCreated(endpoint);
}
if (endpoint is null)
{
continue;
}
if (defaultStrategy is not null && endpoint.RetryStrategy is null)
{
endpoint.RetryStrategy = defaultStrategy;
}
if (endpoint.Url.Length == 0)
{
throw new ArgumentException($"URL path for URL {endpoint.Url}, routine {routine.Name} is empty.");
}
if (endpoint.Url.Length > MaxPathLength)
{
throw new ArgumentException($"URL path for URL {endpoint.Url}, routine {routine.Name} length exceeds {MaxPathLength} characters.");
}
var method = endpoint.Method.ToString();
if (endpoint.HasBodyParameter is true && endpoint.RequestParamType == RequestParamType.BodyJson)
{
endpoint.RequestParamType = RequestParamType.QueryString;
logger?.EndpointTypeChangedBodyParam(method, endpoint.Url, endpoint!.BodyParameterName ?? "");
}
if (endpoint.Upload is true)
{
if (endpoint.Method != Method.POST)
{
logger?.EndpointMethodChangedUpload(method, endpoint.Url, Method.POST.ToString());
endpoint.Method = Method.POST;
}
if (endpoint.RequestParamType == RequestParamType.BodyJson)
{
endpoint.RequestParamType = RequestParamType.QueryString;
logger?.EndpointTypeChangedUpload(method, endpoint.Url);
}
}
var key = string.Concat(method, endpoint?.Url);
var value = new NpgsqlRestMetadataEntry(endpoint!, formatter, key);
if (lookup.TryGetValue(key, out var existing))
{
overloads[string.Concat(key, existing.Endpoint.Routine.ParamCount)] = existing;
}
lookup[key] = value;
if (routine.ColumnsTypeDescriptor is not null && routine.ColumnsTypeDescriptor.Length == 1)
{
bool[] unknownResultTypeList = new bool[routine.ColumnsTypeDescriptor.Length];
bool hasKnownType = false;
for (var i = 0; i < routine.ColumnsTypeDescriptor.Length; i++)
{
unknownResultTypeList[i] = routine.ColumnsTypeDescriptor[i].ShouldRenderAsUnknownType;
if (routine.ColumnsTypeDescriptor[i].ShouldRenderAsUnknownType is false)
{
hasKnownType = true;
}
}
if (hasKnownType)
{
routine.UnknownResultTypeList = unknownResultTypeList;
}
}
if (builder is not null)
{
foreach (var handler in options.EndpointCreateHandlers)
{
handler.Handle(endpoint!);
}
}
if (endpoint?.InfoEventsStreamingPath is not null)
{
if (endpoint.InfoEventsStreamingPath.StartsWith(endpoint.Url) is false)
{
endpoint.InfoEventsStreamingPath = string.Concat(
endpoint.Url.EndsWith('/') ? endpoint.Url[..^1] : endpoint.Url , "/",
endpoint.InfoEventsStreamingPath.StartsWith('/') ? endpoint.InfoEventsStreamingPath[1..] : endpoint.InfoEventsStreamingPath);
}
NpgsqlRestNoticeEventSource.Paths.Add(endpoint.InfoEventsStreamingPath);
if (hasStreamingEvents is false)
{
hasStreamingEvents = true;
}
}
if (options.LogEndpointCreatedInfo)
{
var urlInfo = string.Concat(method, " ", endpoint!.Url);
logger?.EndpointCreated(urlInfo);
if (endpoint?.InfoEventsStreamingPath is not null)
{
logger?.EndpointInfoStreamingPath(urlInfo, endpoint.InfoEventsStreamingPath);
}
}
if (endpoint?.Login is true)
{
if (hasLogin is false)
{
hasLogin = true;
}
if (routine.IsVoid is true || routine.ReturnsUnnamedSet is true)
{
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.");
}
}
if (endpoint?.Cached is true && hasCachedRoutine is false)
{
hasCachedRoutine = true;
}
}
}
if (hasLogin is true)
{
if (options.AuthenticationOptions.DefaultAuthenticationType is null)
{
string db = new NpgsqlConnectionStringBuilder(options.ConnectionString).Database ?? "NpgsqlRest";
options.AuthenticationOptions.DefaultAuthenticationType = db;
logger?.SetDefaultAuthenticationType(db);
}
}
if (hasCachedRoutine is true && options.CacheOptions.DefaultRoutineCache is RoutineCache)
{
RoutineCache.Start(options);
if (builder is WebApplication app)
{
app.Lifetime.ApplicationStopping.Register(() =>
{
RoutineCache.Shutdown();
});
}
}
if (options.UploadOptions.UploadHandlers is not null && options.UploadOptions.UploadHandlers.ContainsKey(options.UploadOptions.DefaultUploadHandler) is false)
{
logger?.LogError("Default upload handler {defaultUploadHandler} not found in the list of upload handlers. Using upload endpoint with default handler may cause an error.", options.UploadOptions.DefaultUploadHandler);
}
if (options.EndpointsCreated is not null)
{
options.EndpointsCreated([.. lookup.Values.Select(x => x.Endpoint)]);
}
if (builder is not null)
{
RoutineEndpoint[]? array = null;
foreach (var handler in options.EndpointCreateHandlers)
{
array ??= [.. lookup.Values.Select(x => x.Endpoint)];
handler.Cleanup(array);
handler.Cleanup();
}
}
return new NpgsqlRestMetadata(lookup, overloads) { HasStreamingEvents = hasStreamingEvents };
}
}