1
- import localVarRequest from 'request';
2
1
import http from 'http';
2
+ import { shuffle } from '../complement/helpers';
3
+ import { Transporter } from '../complement/Transporter';
4
+ import { Headers, Host, Request, RequestOptions } from '../complement/types';
3
5
4
6
{ {#imports} }
5
7
import { {{classname} } } from '{ {filename} }';
6
8
{ {/imports} }
7
9
8
10
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
9
11
{ {#hasAuthMethods} }
10
- import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
12
+ import { HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
11
13
{ {/hasAuthMethods} }
12
14
13
15
import { HttpError, RequestFile } from './apis';
14
16
15
- let defaultBasePath = '{ {{basePath} }}';
16
-
17
- // ===============================================
18
- // This file is autogenerated - Please do not edit
19
- // ===============================================
20
-
21
17
{ {#operations} }
22
18
{ {#description} }
23
19
/**
@@ -33,9 +29,7 @@ export enum {{classname}}ApiKeys {
33
29
}
34
30
35
31
export class { {classname} } {
36
- protected _basePath = defaultBasePath;
37
- protected _defaultHeaders : any = {} ;
38
- protected _useQuerystring : boolean = false;
32
+ private transporter: Transporter;
39
33
40
34
protected authentications = {
41
35
' default' : < Authentication> new VoidAuth(),
@@ -59,35 +53,30 @@ export class {{classname}} {
59
53
60
54
protected interceptors: Interceptor[] = [];
61
55
62
- constructor(appId: string, apiKey: string, basePath?: string) {
63
-
56
+ constructor(appId: string, apiKey: string) {
64
57
this.setApiKey(SearchApiApiKeys.appId, appId);
65
58
this.setApiKey(SearchApiApiKeys.apiKey, apiKey);
66
- this.basePath = ' https://' + appId + ' -1.algolianet.com' ;
67
-
68
- if (basePath) {
69
- this.basePath = basePath;
70
- }
71
- }
72
-
73
- set useQuerystring(value: boolean) {
74
- this._useQuerystring = value;
75
- }
76
-
77
- set basePath(basePath: string) {
78
- this._basePath = basePath;
79
- }
80
-
81
- set defaultHeaders(defaultHeaders: any) {
82
- this._defaultHeaders = defaultHeaders;
83
- }
84
-
85
- get defaultHeaders() {
86
- return this._defaultHeaders;
87
- }
88
-
89
- get basePath() {
90
- return this._basePath;
59
+ this.transporter = new Transporter({
60
+ hosts: ([
61
+ { url: `${appId} -dsn.algolia.net`, accept: 'read', protocol: 'https' },
62
+ { url: `${appId} .algolia.net`, accept: 'write', protocol: 'https' },
63
+ ] as Host[]).concat(
64
+ shuffle([
65
+ { url: `${appId} -1.algolianet.com`, accept: 'readWrite', protocol: 'https' },
66
+ { url: `${appId} -2.algolianet.com`, accept: 'readWrite', protocol: 'https' },
67
+ { url: `${appId} -3.algolianet.com`, accept: 'readWrite', protocol: 'https' },
68
+ ])
69
+ ),
70
+ baseHeaders: {
71
+ ' content-type' : ' application/x-www-form-urlencoded'
72
+ } ,
73
+ userAgent: 'Algolia for Javascript',
74
+ timeouts: {
75
+ connect: 2,
76
+ read: 5,
77
+ write: 30,
78
+ } ,
79
+ });
91
80
}
92
81
93
82
public setDefaultAuthentication(auth: Authentication) {
@@ -139,20 +128,20 @@ export class {{classname}} {
139
128
{ {/allParams} }
140
129
*/
141
130
public async { {nickname} } ({ {#allParams} }{ {paramName} }{ {^required} }?{ {/required} }: { {{dataType} }}, { {/allParams} }options: { headers: {[name: string]: string} } = { headers: {} }) : Promise<{ response: http.IncomingMessage; {{#returnType} }body: { {{returnType} }}; { {/returnType} }{ {^returnType} }body?: any; { {/returnType} } }> {
142
- const localVarPath = this.basePath + ' {{{path}}}' {{#pathParams} }
131
+ const path = ' {{{path}}}' {{#pathParams} }
143
132
.replace('{ ' + ' {{baseName} }' + '}', encodeURIComponent(String({ {paramName} }))){ {/pathParams} };
144
- let localVarQueryParameters: any = { } ;
145
- let localVarHeaderParams: any = (< any >Object).assign( { } , this._defaultHeaders) ;
133
+ let headers: Headers = { } ;
134
+ let queryParameters: Record< string , string > = { } ;
146
135
{ {#hasProduces} }
147
136
const produces = [{ {#produces} }'{ {{mediaType} }}'{ {^-last} }, { {/-last} }{ {/produces} }];
148
137
// give precedence to 'application/json'
149
138
if (produces.indexOf('application/json') >= 0) {
150
- localVarHeaderParams .Accept = ' application/json' ;
139
+ headers .Accept = ' application/json' ;
151
140
} else {
152
- localVarHeaderParams .Accept = produces.join(' ,' );
141
+ headers .Accept = produces.join(' ,' );
153
142
}
154
143
{ {/hasProduces} }
155
- let localVarFormParams: any = { } ;
144
+ let formParams: Record< string , string > = { } ;
156
145
157
146
{ {#allParams} }
158
147
{ {#required} }
@@ -165,103 +154,61 @@ export class {{classname}} {
165
154
{ {/allParams} }
166
155
{ {#queryParams} }
167
156
if ({ {paramName} } !== undefined) {
168
- localVarQueryParameters [' {{baseName}}' ] = ObjectSerializer.serialize({{paramName} }, "{ {{dataType} }}");
157
+ queryParameters [' {{baseName}}' ] = ObjectSerializer.serialize({{paramName} }, "{ {{dataType} }}");
169
158
}
170
159
171
160
{ {/queryParams} }
172
161
{ {#headerParams} }
173
- localVarHeaderParams ['{ {baseName} }'] = ObjectSerializer.serialize({ {paramName} }, "{ {{dataType} }}");
162
+ headers ['{ {baseName} }'] = ObjectSerializer.serialize({ {paramName} }, "{ {{dataType} }}");
174
163
{ {/headerParams} }
175
- (<any >Object).assign(localVarHeaderParams, options.headers);
176
-
177
- let localVarUseFormData = false;
178
-
179
- { {#formParams} }
180
- if ({ {paramName} } !== undefined) {
181
- {{#isFile} }
182
- localVarFormParams['{ {baseName} }'] = { {paramName} };
183
- { {/isFile} }
184
- { {^isFile} }
185
- localVarFormParams['{ {baseName} }'] = ObjectSerializer.serialize({ {paramName} }, "{ {{dataType} }}");
186
- { {/isFile} }
187
- }
188
- { {#isFile} }
189
- localVarUseFormData = true;
190
- { {/isFile} }
164
+ headers = { ...headers, ...options.headers} ;
191
165
192
- { {/formParams} }
193
- let localVarRequestOptions: localVarRequest.Options = {
166
+ const request: Request = {
194
167
method: ' {{httpMethod}}' ,
195
- qs: localVarQueryParameters,
196
- headers: localVarHeaderParams,
197
- uri: localVarPath,
198
- useQuerystring: this._useQuerystring,
199
- {{^isResponseFile} }
200
- json: true,
201
- { {/isResponseFile} }
202
- { {#isResponseFile} }
203
- encoding: null,
204
- { {/isResponseFile} }
168
+ path,
205
169
{{#bodyParam} }
206
- body : ObjectSerializer.serialize({ {paramName} }, "{ {{dataType} }}")
170
+ data : ObjectSerializer.serialize({ {paramName} }, "{ {{dataType} }}")
207
171
{ {/bodyParam} }
208
172
};
209
173
174
+ const requestOptions: RequestOptions = {
175
+ headers,
176
+ queryParameters
177
+ } ;
178
+
210
179
let authenticationPromise = Promise.resolve();
211
180
{ {#authMethods} }
212
181
{ {#isApiKey} }
213
182
if (this.authentications.{ {name} }.apiKey) {
214
- authenticationPromise = authenticationPromise.then(() => this.authentications.{{name} }.applyToRequest(localVarRequestOptions ));
183
+ authenticationPromise = authenticationPromise.then(() => this.authentications.{{name} }.applyToRequest(requestOptions ));
215
184
}
216
185
{ {/isApiKey} }
217
186
{ {#isBasicBasic} }
218
187
if (this.authentications.{ {name} }.username && this.authentications.{ {name} }.password) {
219
- authenticationPromise = authenticationPromise.then(() => this.authentications.{{name} }.applyToRequest(localVarRequestOptions ));
188
+ authenticationPromise = authenticationPromise.then(() => this.authentications.{{name} }.applyToRequest(requestOptions ));
220
189
}
221
190
{ {/isBasicBasic} }
222
191
{ {#isBasicBearer} }
223
192
if (this.authentications.{ {name} }.accessToken) {
224
- authenticationPromise = authenticationPromise.then(() => this.authentications.{{name} }.applyToRequest(localVarRequestOptions ));
193
+ authenticationPromise = authenticationPromise.then(() => this.authentications.{{name} }.applyToRequest(requestOptions ));
225
194
}
226
195
{ {/isBasicBearer} }
227
196
{ {#isOAuth} }
228
197
if (this.authentications.{ {name} }.accessToken) {
229
- authenticationPromise = authenticationPromise.then(() => this.authentications.{{name} }.applyToRequest(localVarRequestOptions ));
198
+ authenticationPromise = authenticationPromise.then(() => this.authentications.{{name} }.applyToRequest(requestOptions ));
230
199
}
231
200
{ {/isOAuth} }
232
201
{ {/authMethods} }
233
- authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions ));
202
+ authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(requestOptions ));
234
203
235
204
let interceptorPromise = authenticationPromise;
236
205
for (const interceptor of this.interceptors) {
237
- interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions ));
206
+ interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions ));
238
207
}
239
208
240
- return interceptorPromise.then(() => {
241
- if (Object.keys(localVarFormParams).length) {
242
- if (localVarUseFormData) {
243
- (< any> localVarRequestOptions).formData = localVarFormParams;
244
- } else {
245
- localVarRequestOptions.form = localVarFormParams;
246
- }
247
- }
248
- return new Promise<{ response: http.IncomingMessage; {{#returnType} }body: { {{returnType} }}; { {/returnType} }{ {^returnType} }body?: any; { {/returnType} } }>((resolve, reject) => {
249
- localVarRequest(localVarRequestOptions, (error, response, body) => {
250
- if (error) {
251
- reject(error);
252
- } else {
253
- {{#returnType} }
254
- body = ObjectSerializer.deserialize(body, "{ {{returnType} }}");
255
- { {/returnType} }
256
- if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
257
- resolve({ response: response, body: body } );
258
- } else {
259
- reject(new HttpError(response, body, response.statusCode));
260
- }
261
- }
262
- });
263
- });
264
- });
209
+ await interceptorPromise;
210
+
211
+ return this.transporter.retryableRequest(request, requestOptions);
265
212
}
266
213
{ {/operation} }
267
214
}
0 commit comments