-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.mustache
More file actions
175 lines (165 loc) · 8.76 KB
/
Copy pathapi.mustache
File metadata and controls
175 lines (165 loc) · 8.76 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
{{>partial_header}}
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using {{packageName}}.Interfaces;
using {{packageName}}.Internal;
using {{packageName}}.Internal.RequestHandlers;
using {{packageName}}.Model;
namespace {{packageName}}.Api
{
{{#operations}}
/// <summary>
/// {{classname}}
/// </summary>
public class {{classname}} : I{{classname}}
{
private readonly ApiInvoker _apiInvoker;
private readonly Configuration _configuration;
/// <summary>
/// Initializes a new instance of the <see cref="{{classname}}" /> class.
/// </summary>
/// <param name="configuration">Configuration settings</param>
public {{classname}}(Configuration configuration)
{
_configuration = configuration;
List<IRequestHandler> requestHandlers = new List<IRequestHandler>();
switch (_configuration.AuthType)
{
case AuthType.JWT:
requestHandlers.Add(new JwtRequestHandler(_configuration));
break;
case AuthType.ExternalAuth:
requestHandlers.Add(new ExternalAuthorizationRequestHandler(_configuration));
break;
default:
throw new System.ArgumentOutOfRangeException($"Unknown AuthType={_configuration.AuthType}.");
}
requestHandlers.Add(new DebugLogRequestHandler(_configuration));
requestHandlers.Add(new ApiExceptionRequestHandler());
_apiInvoker = new ApiInvoker(configuration, requestHandlers);
}
/// <summary>
/// Initializes a new instance of the <see cref="{{classname}}" /> class.
/// </summary>
/// <param name="clientSecret">
/// The Client Secret.
/// </param>
/// <param name="clientId">
/// The Client Id.
/// </param>
public {{classname}}(string clientSecret, string clientId)
: this(new Configuration { ClientSecret = clientSecret, ClientId = clientId })
{
}
{{#operation}}
/// <summary>
/// {{summary}} {{notes}}
/// </summary>
{{#allParams}}
{{^vendorExtensions.x-param-group-camel}}
/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}{{#isDeprecated}} (deprecated){{/isDeprecated}}</param>
{{/vendorExtensions.x-param-group-camel}}
{{/allParams}}
{{#vendorExtensions.x-param-groups}}
/// <param name="{{camel}}">Grouped parameters of type {{type}} (optional)</param>
{{/vendorExtensions.x-param-groups}}
/// <param name="cancellationToken"></param>
/// <returns>
/// A task that represents the asynchronous operation. {{#returnType}}Task result type is <see cref="{{returnType}}" /> {{/returnType}}
/// </returns>
{{#isDeprecated}}
[Obsolete]
{{/isDeprecated}}
public async {{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}} {{nickname}}Async({{#allParams}}{{^vendorExtensions.x-param-group-camel}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default{{/optionalMethodArgument}}{{/required}}, {{/vendorExtensions.x-param-group-camel}}{{/allParams}}{{#vendorExtensions.x-param-groups}}{{type}} {{camel}} = default, {{/vendorExtensions.x-param-groups}}System.Threading.CancellationToken cancellationToken = default)
{
{{#allParams}}{{#required}}{{^isEnum}} // verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null)
{
throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{nickname}}");
}
{{/isEnum}}{{/required}}{{/allParams}} // create path and map variables
string resourcePath = _configuration.GetApiRootUrl() + "{{path}}";
resourcePath = Regex
.Replace(resourcePath, "\\*", string.Empty)
.Replace("&", "&")
.Replace("/?", "?");
{{#headerParams}}
{{#-first}}
Dictionary<string, string> headerParams = new Dictionary<string, string>();
{{/-first}}
{{/headerParams}}
{{#formParams}}
{{#-first}}
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
{{/-first}}
{{^vendorExtensions.x-param-group-camel}}
{{^isEnum}}
if ({{paramName}} != null)
{ {{/isEnum}} {{#isEnum}}{{^required}}if ({{paramName}} != null) { {{/required}}{{/isEnum}}
{{#isFile}}
{{^required}}
if ({{paramName}} != null)
{ {{/required}}
multipartContent.Add(new StreamContent({{paramName}}), "{{paramName}}", "{{paramName}}.png");
{{^required}} } {{/required}}
{{/isFile}}
{{^isFile}}
{{#isCollectionFormatMulti}}
foreach (var oneParam in {{paramName}})
{
multipartContent.Add(new StringContent(oneParam.ToString()), "{{paramName}}");
}
{{/isCollectionFormatMulti}}
{{#isPrimitiveType}}
multipartContent.Add(new StringContent($"{ {{paramName}} }"), "{{paramName}}");
{{/isPrimitiveType}}
{{/isFile}}
{{^isEnum}} } {{/isEnum}} {{#isEnum}}{{^required}} } {{/required}}{{/isEnum}}
{{/vendorExtensions.x-param-group-camel}}
{{#vendorExtensions.x-param-group-camel}}
if ({{vendorExtensions.x-param-group-camel}}?.{{nameInPascalCase}} != null)
{
multipartContent.Add(new StringContent($"{ {{vendorExtensions.x-param-group-camel}}?.{{nameInPascalCase}} }"), "{{paramName}}");
}
{{/vendorExtensions.x-param-group-camel}}
{{/formParams}}
{{#pathParams}}
resourcePath = UrlHelper.AddPathParameter(resourcePath, "{{paramName}}", {{paramName}});
{{/pathParams}}
{{#queryParams}}
{{#-first}}
#pragma warning disable CS0618 // Type or member is obsolete
{{/-first}}
{{^vendorExtensions.x-param-group-camel}}resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "{{paramName}}", {{paramName}});{{/vendorExtensions.x-param-group-camel}}{{#vendorExtensions.x-param-group-camel}}resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "{{paramName}}", {{vendorExtensions.x-param-group-camel}}?.{{nameInPascalCase}});{{/vendorExtensions.x-param-group-camel}}
{{#-last}}
#pragma warning restore CS0618 // Type or member is obsolete
{{/-last}}
{{/queryParams}}
{{#hasBodyParam}}string postBody = SerializationHelper.Serialize({{bodyParam.paramName}}); // http body (model) parameter{{/hasBodyParam}}
{{#vendorExtensions}}{{#x-binary-result}} return await _apiInvoker.InvokeBinaryApiAsync(
resourcePath,
"{{httpMethod}}",
{{#hasBodyParam}}postBody{{/hasBodyParam}}{{^hasBodyParam}}null{{/hasBodyParam}},
{{#hasHeaderParams}}headerParams{{/hasHeaderParams}}{{^hasHeaderParams}}null{{/hasHeaderParams}},
{{#hasFormParams}}multipartContent{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});{{/x-binary-result}}{{/vendorExtensions}}{{#vendorExtensions}}{{^x-binary-result}}{{#returnType}} string response = await _apiInvoker.InvokeApiAsync(
resourcePath,
"{{httpMethod}}",
{{#hasBodyParam}}postBody{{/hasBodyParam}}{{^hasBodyParam}}null{{/hasBodyParam}},
null,
{{#hasFormParams}}multipartContent{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});
return response != null ? ({{returnType}}) SerializationHelper.Deserialize(response, typeof({{returnType}})) : null;
{{/returnType}}
{{^returnType}}
await _apiInvoker.InvokeApiAsync(
resourcePath,
"{{httpMethod}}",
{{#hasBodyParam}}postBody{{/hasBodyParam}}{{^hasBodyParam}}null{{/hasBodyParam}},
{{#hasHeaderParams}}headerParams{{/hasHeaderParams}}{{^hasHeaderParams}}null{{/hasHeaderParams}},
{{#hasFormParams}}multipartContent{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}});{{/returnType}}{{/x-binary-result}}{{/vendorExtensions}}
}
{{/operation}}
}
{{/operations}}
}