/
api.mustache
219 lines (203 loc) · 7.96 KB
/
api.mustache
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
{{>licenseInfo}}
package {{package}};
import {{invokerPackage}}.ApiClient;
import {{invokerPackage}}.ApiException;
import {{invokerPackage}}.Pair;
{{#imports}}
import {{import}};
{{/imports}}
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.function.Consumer;
{{^fullJavaUtil}}
import java.util.ArrayList;
import java.util.StringJoiner;
import java.util.List;
import java.util.Map;
{{/fullJavaUtil}}
{{#asyncNative}}
import java.util.concurrent.CompletableFuture;
{{/asyncNative}}
{{>generatedAnnotation}}
{{#operations}}
public class {{classname}} {
private final HttpClient memberVarHttpClient;
private final ObjectMapper memberVarObjectMapper;
private final String memberVarBaseUri;
private final Consumer<HttpRequest.Builder> memberVarInterceptor;
private final Duration memberVarReadTimeout;
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
public {{classname}}() {
this(new ApiClient());
}
public {{classname}}(ApiClient apiClient) {
memberVarHttpClient = apiClient.getHttpClient();
memberVarObjectMapper = apiClient.getObjectMapper();
memberVarBaseUri = apiClient.getBaseUri();
memberVarInterceptor = apiClient.getRequestInterceptor();
memberVarReadTimeout = apiClient.getReadTimeout();
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}
{{#operation}}
/**
* {{summary}}
* {{notes}}
{{#allParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/isContainer}}{{/required}}
{{/allParams}}
{{#returnType}}
* @return {{returnType}}
{{/returnType}}
* @throws ApiException if fails to make API call
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
{{#allParams}}
{{#required}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null) {
{{#asyncNative}}
return CompletableFuture.failedFuture(new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}"));
{{/asyncNative}}
{{^asyncNative}}
throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}");
{{/asyncNative}}
}
{{/required}}
{{/allParams}}
HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();
{{! Switch delimiters for baseName so we can write constants like "{query}" }}
String localVarPath = "{{{path}}}"{{#pathParams}}
.replace({{=<% %>=}}"{<%baseName%>}"<%={{ }}=%>, ApiClient.urlEncode({{{paramName}}}.toString())){{/pathParams}};
{{#hasQueryParams}}
{{javaUtilPrefix}}List<Pair> localVarQueryParams = new {{javaUtilPrefix}}ArrayList<>();
{{#queryParams}}
{{#collectionFormat}}
localVarQueryParams.addAll(ApiClient.parameterToPairs("{{{collectionFormat}}}", "{{baseName}}", {{paramName}}));
{{/collectionFormat}}
{{^collectionFormat}}
localVarQueryParams.addAll(ApiClient.parameterToPairs("{{baseName}}", {{paramName}}));
{{/collectionFormat}}
{{/queryParams}}
if (!localVarQueryParams.isEmpty()) {
{{javaUtilPrefix}}StringJoiner queryJoiner = new StringJoiner("&");
localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue()));
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + queryJoiner.toString()));
} else {
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
}
{{/hasQueryParams}}
{{^hasQueryParams}}
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
{{/hasQueryParams}}
{{#headerParams}}
if ({{paramName}} != null) {
localVarRequestBuilder.header("{{baseName}}", {{paramName}}.toString());
}
{{/headerParams}}
{{#bodyParam}}
localVarRequestBuilder.header("Content-Type", "application/json");
{{/bodyParam}}
localVarRequestBuilder.header("Accept", "application/json");
{{^asyncNative}}
try {
{{/asyncNative}}
{{#asyncNative}}
{{#bodyParam}}
try {
{{/bodyParam}}
{{/asyncNative}}
{{#bodyParam}}
byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes({{paramName}});
localVarRequestBuilder.method("{{httpMethod}}", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
{{/bodyParam}}
{{^bodyParam}}
localVarRequestBuilder.method("{{httpMethod}}", HttpRequest.BodyPublishers.noBody());
{{/bodyParam}}
if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
if (memberVarInterceptor != null) {
memberVarInterceptor.accept(localVarRequestBuilder);
}
{{^asyncNative}}
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofInputStream());
if (memberVarResponseInterceptor != null) {
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"{{operationId}} call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
}
{{#returnType}}
return memberVarObjectMapper.readValue(localVarResponse.body(), new TypeReference<{{{returnType}}}>() {});
{{/returnType}}
{{/asyncNative}}
{{#asyncNative}}
return memberVarHttpClient.sendAsync(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
if (localVarResponse.statusCode()/ 100 != 2) {
return CompletableFuture.failedFuture(new ApiException(localVarResponse.statusCode(),
"{{operationId}} call received non-success response",
localVarResponse.headers(),
localVarResponse.body())
);
} else {
try {
return CompletableFuture.completedFuture(
{{#returnType}}
memberVarObjectMapper.readValue(localVarResponse.body(), new TypeReference<{{{returnType}}}>() {})
{{/returnType}}
{{^returnType}}
null
{{/returnType}}
);
} catch (IOException e) {
return CompletableFuture.failedFuture(new ApiException(e));
}
}
});
{{/asyncNative}}
{{#asyncNative}}
{{#bodyParam}}
} catch (IOException e) {
return CompletableFuture.failedFuture(new ApiException(e));
}
{{/bodyParam}}
{{/asyncNative}}
{{^asyncNative}}
} catch (IOException e) {
throw new ApiException(e);
}
{{/asyncNative}}
{{^asyncNative}}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ApiException(e);
}
{{/asyncNative}}
}
{{/operation}}
}
{{/operations}}