-
Notifications
You must be signed in to change notification settings - Fork 12
/
api.mustache
243 lines (205 loc) · 11.7 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package {{package}};
import {{invokerPackage}}.ApiClient;
import com.google.gson.reflect.TypeToken;
import okhttp3.Call;
import okhttp3.Request;
import com.algolia.utils.*;
import {{modelPackage}}.*;
import com.algolia.exceptions.*;
import com.algolia.utils.retry.CallType;
import com.algolia.utils.retry.StatefulHost;
import com.algolia.utils.RequestOptions;
import java.util.function.IntUnaryOperator;
import java.util.EnumSet;
import java.util.Random;
import java.util.Collections;
import java.lang.reflect.Type;
{{^fullJavaUtil}}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
{{/fullJavaUtil}}
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.concurrent.CompletableFuture;
{{#operations}}
public class {{classname}} extends ApiClient {
{{#hasRegionalHost}}
{{#fallbackToAliasHost}}
public {{classname}}(String appId, String apiKey) {
this(appId, apiKey, new HttpRequester(getDefaultHosts(null)), null);
}
public {{classname}}(String appId, String apiKey, AlgoliaAgent.Segment[] algoliaAgentSegments) {
this(appId, apiKey, new HttpRequester(getDefaultHosts(null)), algoliaAgentSegments);
}
{{/fallbackToAliasHost}}
public {{classname}}(String appId, String apiKey, String region) {
this(appId, apiKey, new HttpRequester(getDefaultHosts(region)), null);
}
public {{classname}}(String appId, String apiKey, String region, AlgoliaAgent.Segment[] algoliaAgentSegments) {
this(appId, apiKey, new HttpRequester(getDefaultHosts(region)), algoliaAgentSegments);
}
{{/hasRegionalHost}}
{{^hasRegionalHost}}
public {{classname}}(String appId, String apiKey) {
this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), null);
}
public {{classname}}(String appId, String apiKey, AlgoliaAgent.Segment[] algoliaAgentSegments) {
this(appId, apiKey, new HttpRequester(getDefaultHosts(appId)), algoliaAgentSegments);
}
{{/hasRegionalHost}}
public {{classname}}(String appId, String apiKey, Requester requester) {
this(appId, apiKey, requester, null);
}
public {{classname}}(String appId, String apiKey, Requester requester, AlgoliaAgent.Segment[] algoliaAgentSegments) {
super(appId, apiKey, requester, "{{{baseName}}}", algoliaAgentSegments);
}
{{^hasRegionalHost}}
private static List<StatefulHost> getDefaultHosts(String appId) {
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
hosts.add(new StatefulHost(appId + "-dsn.algolia.net", "https", EnumSet.of(CallType.READ)));
hosts.add(new StatefulHost(appId + ".algolia.net", "https", EnumSet.of(CallType.WRITE)));
List<StatefulHost> commonHosts = new ArrayList<StatefulHost>();
hosts.add(new StatefulHost(appId + "-1.algolianet.net", "https", EnumSet.of(CallType.READ, CallType.WRITE)));
hosts.add(new StatefulHost(appId + "-2.algolianet.net", "https", EnumSet.of(CallType.READ, CallType.WRITE)));
hosts.add(new StatefulHost(appId + "-3.algolianet.net", "https", EnumSet.of(CallType.READ, CallType.WRITE)));
Collections.shuffle(commonHosts, new Random());
return Stream.concat(hosts.stream(), commonHosts.stream()).collect(Collectors.toList());
}
{{/hasRegionalHost}}
{{#hasRegionalHost}}
private static List<StatefulHost> getDefaultHosts(String region) {
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
String url = {{#fallbackToAliasHost}}region == null ? "{{{hostWithFallback}}}" : {{/fallbackToAliasHost}} "{{{host}}}".replace("{region}", region);
hosts.add(new StatefulHost(url, "https", EnumSet.of(CallType.READ, CallType.WRITE)));
return hosts;
}
{{/hasRegionalHost}}
{{#operation}}
/**
* {{¬es}}{{#allParams}}
* @param {{paramName}} {{&description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}{{#returnType}}
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
* @return {{.}}{{/returnType}}
* @throws AlgoliaRuntimeException If fail to call the API, e.g. server error or cannot deserialize the response body
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{&description}}
* @see <a href="{{url}}">{{&summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public {{#returnType}}{{{.}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}RequestOptions requestOptions) throws AlgoliaRuntimeException {
return LaunderThrowable.await({{operationId}}Async({{#allParams}}{{paramName}}, {{/allParams}}requestOptions));
}
{{! This case only sets `requestOptions` as optional }}
public {{#returnType}}{{{.}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws AlgoliaRuntimeException {
{{#returnType}}return {{/returnType}}this.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}null);
}
{{! This case sets `requiredParams` + `requestOptions` }}
{{#optionalParams.0}}
public {{#returnType}}{{{.}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#requiredParams}}{{{dataType}}} {{paramName}}, {{/requiredParams}}RequestOptions requestOptions) throws AlgoliaRuntimeException {
{{#returnType}}return {{/returnType}}this.{{operationId}}({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, requestOptions);
}
{{/optionalParams.0}}
{{! This case only sets `requiredParams` }}
{{#optionalParams.0}}
public {{#returnType}}{{{.}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#requiredParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}) throws AlgoliaRuntimeException {
{{#returnType}}return {{/returnType}}this.{{operationId}}({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, null);
}
{{/optionalParams.0}}
/**
* (asynchronously)
* {{notes}}{{#allParams}}
* @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
* @return The awaitable future
* @throws AlgoliaRuntimeException If fail to process the API call, e.g. serializing the request body object
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{&description}}
* @see <a href="{{url}}">{{&summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public CompletableFuture<{{{returnType}}}> {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}RequestOptions requestOptions) throws AlgoliaRuntimeException {
{{#allParams}}{{#required}}
if ({{paramName}} == null) {
throw new AlgoliaRuntimeException("Missing the required parameter '{{paramName}}' when calling {{operationId}}(Async)");
}
{{/required}}{{/allParams}}
Object bodyObj = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
// create path and map variables
String requestPath = "{{{path}}}"{{#vendorExtensions}}{{#pathParams}}.replaceAll(
{{=<% %>=}}"\\{<%baseName%>\\}"<%={{ }}=%>,
{{#x-is-custom-request}}{{{paramName}}}.toString(){{/x-is-custom-request}}{{^x-is-custom-request}}this.escapeString({{{paramName}}}.toString()){{/x-is-custom-request}}
){{/pathParams}}{{/vendorExtensions}};
{{javaUtilPrefix}}Map<String, Object> queryParameters = new {{javaUtilPrefix}}HashMap<String, Object>();
{{javaUtilPrefix}}Map<String, String> headers = new {{javaUtilPrefix}}HashMap<String, String>();
{{#vendorExtensions}}{{#queryParams}}
if ({{paramName}} != null) {
{{^x-is-custom-request}}
queryParameters.put("{{baseName}}", parameterToString({{paramName}}));
{{/x-is-custom-request}}
{{#x-is-custom-request}}
for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
queryParameters.put(parameter.getKey().toString(), parameterToString(parameter.getValue()));
}
{{/x-is-custom-request}}
}
{{/queryParams}}{{/vendorExtensions}}
{{#headerParams}}
if ({{paramName}} != null) {
headers.put("{{baseName}}", this.parameterToString({{paramName}}));
}
{{/headerParams}}
Call call = this.buildCall(requestPath, "{{httpMethod}}", queryParameters, bodyObj, headers, requestOptions, {{#vendorExtensions.x-use-read-transporter}}true{{/vendorExtensions.x-use-read-transporter}}{{^vendorExtensions.x-use-read-transporter}}false{{/vendorExtensions.x-use-read-transporter}});
Type returnType = new TypeToken<{{{returnType}}}>() {}.getType();
return this.executeAsync(call, returnType);
}
{{! This case only sets `requestOptions` as optional }}
public CompletableFuture<{{{returnType}}}> {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws AlgoliaRuntimeException {
{{#returnType}}return {{/returnType}}this.{{operationId}}Async({{#allParams}}{{paramName}}, {{/allParams}}null);
}
{{! This case sets `requiredParams` + `requestOptions` }}
{{#optionalParams.0}}
public CompletableFuture<{{{returnType}}}> {{operationId}}Async({{#requiredParams}}{{{dataType}}} {{paramName}}, {{/requiredParams}}RequestOptions requestOptions) throws AlgoliaRuntimeException {
{{#returnType}}return {{/returnType}}this.{{operationId}}Async({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, requestOptions);
}
{{/optionalParams.0}}
{{! This case only sets `requiredParams` }}
{{#optionalParams.0}}
public CompletableFuture<{{{returnType}}}> {{operationId}}Async({{#requiredParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}) throws AlgoliaRuntimeException {
{{#returnType}}return {{/returnType}}this.{{operationId}}Async({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, null);
}
{{/optionalParams.0}}
{{/operation}}
{{#isSearchClient}}
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions, int maxTrial, IntUnaryOperator timeout) {
TaskUtils.retryUntil(() -> {
return this.getTaskAsync(indexName, taskID, requestOptions);
}, (GetTaskResponse task) -> {
return task.getStatus() == TaskStatus.PUBLISHED;
}, maxTrial, timeout);
}
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) {
this.waitForTask(indexName, taskID, requestOptions, TaskUtils.DEFAULT_MAX_TRIAL, TaskUtils.DEFAULT_TIMEOUT);
}
public void waitForTask(String indexName, Long taskID, int maxTrial, IntUnaryOperator timeout) {
this.waitForTask(indexName, taskID, null, maxTrial, timeout);
}
public void waitForTask(String indexName, Long taskID) {
this.waitForTask(indexName, taskID, null, TaskUtils.DEFAULT_MAX_TRIAL, TaskUtils.DEFAULT_TIMEOUT);
}
{{/isSearchClient}}
}
{{/operations}}