-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDefaultEndpoint.cs
More file actions
87 lines (81 loc) · 3.05 KB
/
Copy pathDefaultEndpoint.cs
File metadata and controls
87 lines (81 loc) · 3.05 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
namespace NpgsqlRest.Defaults;
internal static class DefaultEndpoint
{
internal static RoutineEndpoint? Create(
Routine routine,
NpgsqlRestOptions options,
ILogger? logger)
{
var url = options.UrlPathBuilder(routine, options);
if (routine.FormatUrlPattern is not null)
{
url = string.Format(routine.FormatUrlPattern, url);
}
var method = routine.CrudType switch
{
CrudType.Select => Method.GET,
CrudType.Update => Method.POST,
CrudType.Insert => Method.PUT,
CrudType.Delete => Method.DELETE,
_ => Method.POST
};
var requestParamType = method == Method.GET || method == Method.DELETE ?
RequestParamType.QueryString :
RequestParamType.BodyJson;
string[] returnRecordNames = routine.ColumnNames.Select(s => options.NameConverter(s) ?? "").ToArray();
string[] paramNames = routine
.ParamNames
.Select((s, i) =>
{
var result = options.NameConverter(s) ?? "";
if (string.IsNullOrEmpty(result))
{
result = $"${i + 1}";
}
return result;
})
.ToArray();
RoutineEndpoint routineEndpoint = new(
url: url,
method: method,
requestParamType: requestParamType,
requiresAuthorization: options.RequiresAuthorization,
columnNames: returnRecordNames,
paramNames: paramNames,
commandTimeout: options.CommandTimeout,
responseContentType: null,
responseHeaders: [],
requestHeadersMode: options.RequestHeadersMode,
requestHeadersParameterName: options.RequestHeadersParameterName,
bodyParameterName: null,
textResponseNullHandling: options.TextResponseNullHandling,
queryStringNullHandling: options.QueryStringNullHandling);
if (options.LogCommands && logger != null)
{
routineEndpoint.LogCallback = LoggerMessage.Define<string, string>(LogLevel.Information,
new EventId(5, nameof(routineEndpoint.LogCallback)),
"{parameters}{command}",
Logger.LogDefineOptions);
}
else
{
routineEndpoint.LogCallback = null;
}
if (routine.EndpointHandler is not null)
{
var parsed = DefaultCommentParser.Parse(
ref routine,
ref options,
ref logger,
ref routineEndpoint);
#pragma warning disable CS8602 // Dereference of a possibly null reference.
return routine.EndpointHandler(parsed);
#pragma warning restore CS8602 // Dereference of a possibly null reference.
}
return DefaultCommentParser.Parse(
ref routine,
ref options,
ref logger,
ref routineEndpoint);
}
}