-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNpgsqlRestOptions.cs
More file actions
262 lines (256 loc) · 12.4 KB
/
Copy pathNpgsqlRestOptions.cs
File metadata and controls
262 lines (256 loc) · 12.4 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System.Threading;
using Npgsql;
namespace NpgsqlRest;
public enum Method { GET, PUT, POST, DELETE, HEAD, OPTIONS, TRACE, PATCH, CONNECT }
public enum RequestParamType { QueryString, BodyJson }
public enum CommentsMode
{
/// <summary>
/// Routine comments are ignored.
/// </summary>
Ignore,
/// <summary>
/// Creates all endpoints and parses comments for to configure endpoint meta data.
/// </summary>
ParseAll,
/// <summary>
/// Creates only endpoints from routines containing a comment with HTTP tag and and configures endpoint meta data.
/// </summary>
OnlyWithHttpTag
}
public enum RequestHeadersMode
{
/// <summary>
/// Ignore request headers, don't send them to PostgreSQL (default).
/// </summary>
Ignore,
/// <summary>
/// Send all request headers as json object to PostgreSQL by executing set_config('context.headers', headers, false) before routine call.
/// </summary>
Context,
/// <summary>
/// Send all request headers as json object to PostgreSQL as default routine parameter with name set by RequestHeadersParameterName option.
/// This parameter has to have the default value (null) in the routine and have to be text or json type.
/// </summary>
Parameter
}
/// <summary>
/// Options for the NpgsqlRest middleware.
/// </summary>
public class NpgsqlRestOptions(
string? connectionString,
string? customRoutineCommand = null,
string? schemaSimilarTo = null,
string? schemaNotSimilarTo = null,
string[]? includeSchemas = null,
string[]? excludeSchemas = null,
string? nameSimilarTo = null,
string? nameNotSimilarTo = null,
string[]? includeNames = null,
string[]? excludeNames = null,
string? urlPathPrefix = "/api",
Func<Routine, NpgsqlRestOptions, string>? urlPathBuilder = null,
bool connectionFromServiceProvider = false,
NpgsqlRestHttpFileOptions? httpFileOptions = null,
Func<Routine, RoutineEndpoint, RoutineEndpoint?>? endpointCreated = null,
Func<string?, string?>? nameConverter = null,
bool requiresAuthorization = false,
LogLevel logLevel = LogLevel.Information,
ILogger? logger = null,
string? loggerName = null,
bool logEndpointCreatedInfo = true,
bool logAnnotationSetInfo = true,
bool logConnectionNoticeEvents = true,
bool logCommands = false,
int? commandTimeout = null,
bool logParameterMismatchWarnings = true,
Method? defaultHttpMethod = null,
RequestParamType? defaultRequestParamType = null,
Action<ParameterValidationValues>? validateParameters = null,
Func<ParameterValidationValues, Task>? validateParametersAsync = null,
CommentsMode commentsMode = CommentsMode.ParseAll,
RequestHeadersMode requestHeadersMode = RequestHeadersMode.Ignore,
string requestHeadersParameterName = "headers",
Action<(Routine routine, RoutineEndpoint endpoint)[]>? endpointsCreated = null,
Func<(Routine routine, NpgsqlCommand command, HttpContext context), Task>? commandCallbackAsync = null)
{
/// <summary>
/// Options for the NpgsqlRest middleware.
/// </summary>
/// <param name="connectionString">PostgreSQL connection string</param>
public NpgsqlRestOptions(string? connectionString) : this(connectionString, null)
{
}
/// <summary>
/// Options for the NpgsqlRest middleware.
/// Connection string is set to null:
/// It either has to be set trough ConnectionString property or ConnectionFromServiceProvider has to be set to true.
/// </summary>
public NpgsqlRestOptions() : this(null)
{
}
/// <summary>
/// The connection string to the database.
/// Note: must run as superuser or have select permissions on information_schema.routines, information_schema.parameters, pg_catalog.pg_proc, pg_catalog.pg_description, pg_catalog.pg_namespace
/// </summary>
public string? ConnectionString { get; set; } = connectionString;
/// <summary>
/// When not null, this is a command to use to get the routines.
/// Note: If you need to replace a default query from RoutineQuery.cs module, for example with security definer function, set this property to a custom command.
/// </summary>
public string? CustomRoutineCommand { get; set; } = customRoutineCommand;
/// <summary>
/// Filter schema names similar to this parameters or null for all schemas.
/// </summary>
public string? SchemaSimilarTo { get; set; } = schemaSimilarTo;
/// <summary>
/// Filter schema names not similar to this parameters or null for all schemas.
/// </summary>
public string? SchemaNotSimilarTo { get; set; } = schemaNotSimilarTo;
/// <summary>
/// List of schema names to be included.
/// </summary>
public string[]? IncludeSchemas { get; set; } = includeSchemas;
/// <summary>
/// List of schema names to be excluded.
/// </summary>
public string[]? ExcludeSchemas { get; set; } = excludeSchemas;
/// <summary>
/// Filter routine names similar to this parameters or null for all routines.
/// </summary>
public string? NameSimilarTo { get; set; } = nameSimilarTo;
/// <summary>
/// Filter routine names not similar to this parameters or null for all routines.
/// </summary>
public string? NameNotSimilarTo { get; set; } = nameNotSimilarTo;
/// <summary>
/// List of routine names to be included.
/// </summary>
public string[]? IncludeNames { get; set; } = includeNames;
/// <summary>
/// List of routine names to be excluded.
/// </summary>
public string[]? ExcludeNames { get; set; } = excludeNames;
/// <summary>
/// Url prefix for every url created by the default url builder.
/// </summary>
public string? UrlPathPrefix { get; set; } = urlPathPrefix;
/// <summary>
/// A custom function delegate that returns a string that will be used as the url path for routine from the first parameter.
/// </summary>
public Func<Routine, NpgsqlRestOptions, string> UrlPathBuilder { get; set; } = urlPathBuilder ?? DefaultUrlBuilder.CreateUrl;
/// <summary>
/// Set to true to get the PostgreSQL connection from the service provider. Otherwise, it will be created from the connection string property.
/// </summary>
public bool ConnectionFromServiceProvider { get; set; } = connectionFromServiceProvider;
/// <summary>
/// Configure creation of the .http file on service build.
/// </summary>
public NpgsqlRestHttpFileOptions HttpFileOptions { get; set; } = httpFileOptions ?? new NpgsqlRestHttpFileOptions(HttpFileOption.Disabled);
/// <summary>
/// Callback, if not null, will be called after endpoint meta data is created.
/// Use this to do custom configuration over routine endpoints.
/// Return null to disable this endpoint.
/// </summary>
public Func<Routine, RoutineEndpoint, RoutineEndpoint?>? EndpointCreated { get; set; } = endpointCreated;
/// <summary>
/// Method that converts names for parameters and return fields.
/// By default it is a lower camel case.
/// Use NameConverter = name => name to preserve original names.
/// </summary>
public Func<string?, string?> NameConverter { get; set; } = nameConverter ?? DefaultNameConverter.ConvertToCamelCase;
/// <summary>
/// Set to true to require authorization for all endpoints.
/// </summary>
public bool RequiresAuthorization { get; set; } = requiresAuthorization;
/// <summary>
/// Set the minimal level of log messages or LogLevel.None to disable logging.
/// </summary>
public LogLevel LogLevel { get; set; } = logLevel;
/// <summary>
/// Use this logger instead of the default logger.
/// </summary>
public ILogger? Logger { get; set; } = logger;
/// <summary>
/// Default logger name. Set to null to use default logger name which is NpgsqlRest (default namespace).
/// </summary>
public string? LoggerName { get; set; } = loggerName;
/// <summary>
/// Log endpoint created events.
/// </summary>
public bool LogEndpointCreatedInfo { get; set; } = logEndpointCreatedInfo;
/// <summary>
/// Log annotation set events. When endpoint properties are set from comment annotations.
/// </summary>
public bool LogAnnotationSetInfo { get; set; } = logAnnotationSetInfo;
/// <summary>
/// Set to true to log connection notice events.
/// </summary>
public bool LogConnectionNoticeEvents { get; set; } = logConnectionNoticeEvents;
/// <summary>
/// Log commands executed on PostgreSQL.
/// </summary>
public bool LogCommands { get; set; } = logCommands;
/// <summary>
/// Sets the wait time (in seconds) before terminating the attempt to execute a command and generating an error.
/// Default value is 30 seconds.
/// </summary>
public int? CommandTimeout { get; set; } = commandTimeout;
/// <summary>
/// Set to true to log parameter mismatch warnings. These mismatches occur regularly when using functions with parameter overloads with different types.
/// </summary>
public bool LogParameterMismatchWarnings { get; set; } = logParameterMismatchWarnings;
/// <summary>
/// Default HTTP method for all endpoints. NULL is default behavior:
///
/// The endpoint is always GET if volatility option is STABLE or IMMUTABLE or the routine name either:
/// - Starts with `get_` (case insensitive).
/// - Ends with `_get` (case insensitive).
/// - Contains `_get_` (case insensitive).
///
/// Otherwise, the endpoint is POST (VOLATILE and doesn't contain `get`).
///
/// </summary>
public Method? DefaultHttpMethod { get; set; } = defaultHttpMethod;
/// <summary>
/// Default parameter position - Query String or JSON Body.
/// NULL is default behavior: if endpoint is not POST, use Query String, otherwise JSON Body.
/// </summary>
public RequestParamType? DefaultRequestParamType { get; set; } = defaultRequestParamType;
/// <summary>
/// Parameters validation function callback. Set the HttpContext response status or start writing response body to cancel the request.
/// </summary>
public Action<ParameterValidationValues>? ValidateParameters { get; set; } = validateParameters;
/// <summary>
/// Parameters validation function async callback. Set the HttpContext response status or start writing response body to cancel the request.
/// </summary>
public Func<ParameterValidationValues, Task>? ValidateParametersAsync { get; set; } = validateParametersAsync;
/// <summary>
/// Configure how to parse comments:
/// Ignore: Routine comments are ignored.
/// ParseAll: Creates all endpoints and parses comments for to configure endpoint meta data (default).
/// OnlyWithHttpTag: Creates only endpoints from routines containing a comment with HTTP tag and and configures endpoint meta data.
/// </summary>
public CommentsMode CommentsMode { get; set; } = commentsMode;
/// <summary>
/// Configure how to send request headers to PostgreSQL:
/// Ignore: Ignore request headers, don't send them to PostgreSQL (default).
/// AsContextConfig: Send all request headers as json object to PostgreSQL by executing set_config('context.headers', headers, false) before routine call.
/// AsDefaultParameter: Send all request headers as json object to PostgreSQL as default routine parameter with name set by RequestHeadersParameterName option.
/// </summary>
public RequestHeadersMode RequestHeadersMode { get; set; } = requestHeadersMode;
/// <summary>
/// The name of the default routine parameter (text or json) to send request headers to PostgreSQL (parsed or unparsed).
/// This is only used when RequestHeadersMode is set to AsDefaultParameter.
/// </summary>
public string RequestHeadersParameterName { get; set; } = requestHeadersParameterName;
/// <summary>
/// Callback, if not null, will be called after all endpoints are created.
/// </summary>
public Action<(Routine routine, RoutineEndpoint endpoint)[]>? EndpointsCreated { get; set; } = endpointsCreated;
/// <summary>
/// Command callback, if not null, will be called after every command is created and before it is executed.
/// Setting the the HttpContext response status or start writing response body will the default command execution.
/// </summary>
public Func<(Routine routine, NpgsqlCommand command, HttpContext context), Task>? CommandCallbackAsync { get; set; } = commandCallbackAsync;
}