1
- import http from 'http' ;
2
1
import { shuffle } from '../utils/helpers' ;
3
2
import { Transporter } from '../utils/Transporter' ;
4
3
import { Headers , Host , Request , RequestOptions } from '../utils/types' ;
4
+ import { Requester } from '../utils/Requester' ;
5
5
6
6
import { BatchObject } from '../model/batchObject' ;
7
7
import { BatchResponse } from '../model/batchResponse' ;
@@ -12,9 +12,7 @@ import { SaveObjectResponse } from '../model/saveObjectResponse';
12
12
import { SearchParams } from '../model/searchParams' ;
13
13
import { SearchParamsString } from '../model/searchParamsString' ;
14
14
import { SearchResponse } from '../model/searchResponse' ;
15
-
16
- import { ObjectSerializer , Authentication , VoidAuth , Interceptor } from '../model/models' ;
17
- import { HttpBearerAuth , ApiKeyAuth , OAuth } from '../model/models' ;
15
+ import { ApiKeyAuth } from '../model/models' ;
18
16
19
17
export enum SearchApiApiKeys {
20
18
apiKey ,
@@ -25,14 +23,11 @@ export class SearchApi {
25
23
private transporter : Transporter ;
26
24
27
25
protected authentications = {
28
- default : < Authentication > new VoidAuth ( ) ,
29
26
apiKey : new ApiKeyAuth ( 'header' , 'X-Algolia-API-Key' ) ,
30
27
appId : new ApiKeyAuth ( 'header' , 'X-Algolia-Application-Id' ) ,
31
28
} ;
32
29
33
- protected interceptors : Interceptor [ ] = [ ] ;
34
-
35
- constructor ( appId : string , apiKey : string ) {
30
+ constructor ( appId : string , apiKey : string , requester ?: Requester ) {
36
31
this . setApiKey ( SearchApiApiKeys . appId , appId ) ;
37
32
this . setApiKey ( SearchApiApiKeys . apiKey , apiKey ) ;
38
33
this . transporter = new Transporter ( {
@@ -57,19 +52,25 @@ export class SearchApi {
57
52
read : 5 ,
58
53
write : 30 ,
59
54
} ,
55
+ requester,
60
56
} ) ;
61
57
}
62
58
63
- public setDefaultAuthentication ( auth : Authentication ) {
64
- this . authentications . default = auth ;
65
- }
66
-
67
59
public setApiKey ( key : SearchApiApiKeys , value : string ) {
68
- ( this . authentications as any ) [ SearchApiApiKeys [ key ] ] . apiKey = value ;
60
+ this . authentications [ SearchApiApiKeys [ key ] ] . apiKey = value ;
69
61
}
70
62
71
- public addInterceptor ( interceptor : Interceptor ) {
72
- this . interceptors . push ( interceptor ) ;
63
+ private async sendRequest < TResponse > (
64
+ request : Request ,
65
+ requestOptions : RequestOptions
66
+ ) : Promise < TResponse > {
67
+ if ( this . authentications . apiKey . apiKey ) {
68
+ this . authentications . apiKey . applyToRequest ( requestOptions ) ;
69
+ }
70
+ if ( this . authentications . appId . apiKey ) {
71
+ this . authentications . appId . applyToRequest ( requestOptions ) ;
72
+ }
73
+ return this . transporter . request ( request , requestOptions ) ;
73
74
}
74
75
75
76
/**
@@ -78,137 +79,65 @@ export class SearchApi {
78
79
* @param indexName The index in which to perform the request
79
80
* @param batchObject
80
81
*/
81
- public async batch (
82
- indexName : string ,
83
- batchObject : BatchObject ,
84
- options : { headers : { [ name : string ] : string } } = { headers : { } }
85
- ) : Promise < BatchResponse > {
82
+ public async batch ( indexName : string , batchObject : BatchObject ) : Promise < BatchResponse > {
86
83
const path = '/1/indexes/{indexName}/batch' . replace (
87
84
'{' + 'indexName' + '}' ,
88
85
encodeURIComponent ( String ( indexName ) )
89
86
) ;
90
- let headers : Headers = { } ;
87
+ let headers : Headers = { Accept : 'application/json' } ;
91
88
let queryParameters : Record < string , string > = { } ;
92
- const produces = [ 'application/json' ] ;
93
- // give precedence to 'application/json'
94
- if ( produces . indexOf ( 'application/json' ) >= 0 ) {
95
- headers . Accept = 'application/json' ;
96
- } else {
97
- headers . Accept = produces . join ( ',' ) ;
98
- }
99
- let formParams : Record < string , string > = { } ;
100
89
101
- // verify required parameter 'indexName' is not null or undefined
102
90
if ( indexName === null || indexName === undefined ) {
103
91
throw new Error ( 'Required parameter indexName was null or undefined when calling batch.' ) ;
104
92
}
105
93
106
- // verify required parameter 'batchObject' is not null or undefined
107
94
if ( batchObject === null || batchObject === undefined ) {
108
95
throw new Error ( 'Required parameter batchObject was null or undefined when calling batch.' ) ;
109
96
}
110
97
111
- headers = { ...headers , ...options . headers } ;
112
-
113
98
const request : Request = {
114
99
method : 'POST' ,
115
100
path,
116
- data : ObjectSerializer . serialize ( batchObject , 'BatchObject' ) ,
101
+ data : batchObject ,
117
102
} ;
118
103
119
104
const requestOptions : RequestOptions = {
120
105
headers,
121
106
queryParameters,
122
107
} ;
123
108
124
- let authenticationPromise = Promise . resolve ( ) ;
125
- if ( this . authentications . apiKey . apiKey ) {
126
- authenticationPromise = authenticationPromise . then ( ( ) =>
127
- this . authentications . apiKey . applyToRequest ( requestOptions )
128
- ) ;
129
- }
130
- if ( this . authentications . appId . apiKey ) {
131
- authenticationPromise = authenticationPromise . then ( ( ) =>
132
- this . authentications . appId . applyToRequest ( requestOptions )
133
- ) ;
134
- }
135
- authenticationPromise = authenticationPromise . then ( ( ) =>
136
- this . authentications . default . applyToRequest ( requestOptions )
137
- ) ;
138
-
139
- let interceptorPromise = authenticationPromise ;
140
- for ( const interceptor of this . interceptors ) {
141
- interceptorPromise = interceptorPromise . then ( ( ) => interceptor ( requestOptions ) ) ;
142
- }
143
-
144
- await interceptorPromise ;
145
-
146
- return this . transporter . retryableRequest ( request , requestOptions ) ;
109
+ return this . sendRequest ( request , requestOptions ) ;
147
110
}
148
111
/**
149
112
*
150
113
* @summary Get search results for the given requests.
151
114
* @param multipleQueriesObject
152
115
*/
153
116
public async multipleQueries (
154
- multipleQueriesObject : MultipleQueriesObject ,
155
- options : { headers : { [ name : string ] : string } } = { headers : { } }
117
+ multipleQueriesObject : MultipleQueriesObject
156
118
) : Promise < MultipleQueriesResponse > {
157
119
const path = '/1/indexes/*/queries' ;
158
- let headers : Headers = { } ;
120
+ let headers : Headers = { Accept : 'application/json' } ;
159
121
let queryParameters : Record < string , string > = { } ;
160
- const produces = [ 'application/json' ] ;
161
- // give precedence to 'application/json'
162
- if ( produces . indexOf ( 'application/json' ) >= 0 ) {
163
- headers . Accept = 'application/json' ;
164
- } else {
165
- headers . Accept = produces . join ( ',' ) ;
166
- }
167
- let formParams : Record < string , string > = { } ;
168
122
169
- // verify required parameter 'multipleQueriesObject' is not null or undefined
170
123
if ( multipleQueriesObject === null || multipleQueriesObject === undefined ) {
171
124
throw new Error (
172
125
'Required parameter multipleQueriesObject was null or undefined when calling multipleQueries.'
173
126
) ;
174
127
}
175
128
176
- headers = { ...headers , ...options . headers } ;
177
-
178
129
const request : Request = {
179
130
method : 'POST' ,
180
131
path,
181
- data : ObjectSerializer . serialize ( multipleQueriesObject , 'MultipleQueriesObject' ) ,
132
+ data : multipleQueriesObject ,
182
133
} ;
183
134
184
135
const requestOptions : RequestOptions = {
185
136
headers,
186
137
queryParameters,
187
138
} ;
188
139
189
- let authenticationPromise = Promise . resolve ( ) ;
190
- if ( this . authentications . apiKey . apiKey ) {
191
- authenticationPromise = authenticationPromise . then ( ( ) =>
192
- this . authentications . apiKey . applyToRequest ( requestOptions )
193
- ) ;
194
- }
195
- if ( this . authentications . appId . apiKey ) {
196
- authenticationPromise = authenticationPromise . then ( ( ) =>
197
- this . authentications . appId . applyToRequest ( requestOptions )
198
- ) ;
199
- }
200
- authenticationPromise = authenticationPromise . then ( ( ) =>
201
- this . authentications . default . applyToRequest ( requestOptions )
202
- ) ;
203
-
204
- let interceptorPromise = authenticationPromise ;
205
- for ( const interceptor of this . interceptors ) {
206
- interceptorPromise = interceptorPromise . then ( ( ) => interceptor ( requestOptions ) ) ;
207
- }
208
-
209
- await interceptorPromise ;
210
-
211
- return this . transporter . retryableRequest ( request , requestOptions ) ;
140
+ return this . sendRequest ( request , requestOptions ) ;
212
141
}
213
142
/**
214
143
* Add an object to the index, automatically assigning it an object ID
@@ -218,74 +147,39 @@ export class SearchApi {
218
147
*/
219
148
public async saveObject (
220
149
indexName : string ,
221
- requestBody : { [ key : string ] : object } ,
222
- options : { headers : { [ name : string ] : string } } = { headers : { } }
150
+ requestBody : { [ key : string ] : object }
223
151
) : Promise < SaveObjectResponse > {
224
152
const path = '/1/indexes/{indexName}' . replace (
225
153
'{' + 'indexName' + '}' ,
226
154
encodeURIComponent ( String ( indexName ) )
227
155
) ;
228
- let headers : Headers = { } ;
156
+ let headers : Headers = { Accept : 'application/json' } ;
229
157
let queryParameters : Record < string , string > = { } ;
230
- const produces = [ 'application/json' ] ;
231
- // give precedence to 'application/json'
232
- if ( produces . indexOf ( 'application/json' ) >= 0 ) {
233
- headers . Accept = 'application/json' ;
234
- } else {
235
- headers . Accept = produces . join ( ',' ) ;
236
- }
237
- let formParams : Record < string , string > = { } ;
238
158
239
- // verify required parameter 'indexName' is not null or undefined
240
159
if ( indexName === null || indexName === undefined ) {
241
160
throw new Error (
242
161
'Required parameter indexName was null or undefined when calling saveObject.'
243
162
) ;
244
163
}
245
164
246
- // verify required parameter 'requestBody' is not null or undefined
247
165
if ( requestBody === null || requestBody === undefined ) {
248
166
throw new Error (
249
167
'Required parameter requestBody was null or undefined when calling saveObject.'
250
168
) ;
251
169
}
252
170
253
- headers = { ...headers , ...options . headers } ;
254
-
255
171
const request : Request = {
256
172
method : 'POST' ,
257
173
path,
258
- data : ObjectSerializer . serialize ( requestBody , '{ [key: string]: object; }' ) ,
174
+ data : requestBody ,
259
175
} ;
260
176
261
177
const requestOptions : RequestOptions = {
262
178
headers,
263
179
queryParameters,
264
180
} ;
265
181
266
- let authenticationPromise = Promise . resolve ( ) ;
267
- if ( this . authentications . apiKey . apiKey ) {
268
- authenticationPromise = authenticationPromise . then ( ( ) =>
269
- this . authentications . apiKey . applyToRequest ( requestOptions )
270
- ) ;
271
- }
272
- if ( this . authentications . appId . apiKey ) {
273
- authenticationPromise = authenticationPromise . then ( ( ) =>
274
- this . authentications . appId . applyToRequest ( requestOptions )
275
- ) ;
276
- }
277
- authenticationPromise = authenticationPromise . then ( ( ) =>
278
- this . authentications . default . applyToRequest ( requestOptions )
279
- ) ;
280
-
281
- let interceptorPromise = authenticationPromise ;
282
- for ( const interceptor of this . interceptors ) {
283
- interceptorPromise = interceptorPromise . then ( ( ) => interceptor ( requestOptions ) ) ;
284
- }
285
-
286
- await interceptorPromise ;
287
-
288
- return this . transporter . retryableRequest ( request , requestOptions ) ;
182
+ return this . sendRequest ( request , requestOptions ) ;
289
183
}
290
184
/**
291
185
*
@@ -295,74 +189,36 @@ export class SearchApi {
295
189
*/
296
190
public async search (
297
191
indexName : string ,
298
- searchParamsSearchParamsString : SearchParams | SearchParamsString ,
299
- options : { headers : { [ name : string ] : string } } = { headers : { } }
192
+ searchParamsSearchParamsString : SearchParams | SearchParamsString
300
193
) : Promise < SearchResponse > {
301
194
const path = '/1/indexes/{indexName}/query' . replace (
302
195
'{' + 'indexName' + '}' ,
303
196
encodeURIComponent ( String ( indexName ) )
304
197
) ;
305
- let headers : Headers = { } ;
198
+ let headers : Headers = { Accept : 'application/json' } ;
306
199
let queryParameters : Record < string , string > = { } ;
307
- const produces = [ 'application/json' ] ;
308
- // give precedence to 'application/json'
309
- if ( produces . indexOf ( 'application/json' ) >= 0 ) {
310
- headers . Accept = 'application/json' ;
311
- } else {
312
- headers . Accept = produces . join ( ',' ) ;
313
- }
314
- let formParams : Record < string , string > = { } ;
315
200
316
- // verify required parameter 'indexName' is not null or undefined
317
201
if ( indexName === null || indexName === undefined ) {
318
202
throw new Error ( 'Required parameter indexName was null or undefined when calling search.' ) ;
319
203
}
320
204
321
- // verify required parameter 'searchParamsSearchParamsString' is not null or undefined
322
205
if ( searchParamsSearchParamsString === null || searchParamsSearchParamsString === undefined ) {
323
206
throw new Error (
324
207
'Required parameter searchParamsSearchParamsString was null or undefined when calling search.'
325
208
) ;
326
209
}
327
210
328
- headers = { ...headers , ...options . headers } ;
329
-
330
211
const request : Request = {
331
212
method : 'POST' ,
332
213
path,
333
- data : ObjectSerializer . serialize (
334
- searchParamsSearchParamsString ,
335
- 'SearchParams | SearchParamsString'
336
- ) ,
214
+ data : searchParamsSearchParamsString ,
337
215
} ;
338
216
339
217
const requestOptions : RequestOptions = {
340
218
headers,
341
219
queryParameters,
342
220
} ;
343
221
344
- let authenticationPromise = Promise . resolve ( ) ;
345
- if ( this . authentications . apiKey . apiKey ) {
346
- authenticationPromise = authenticationPromise . then ( ( ) =>
347
- this . authentications . apiKey . applyToRequest ( requestOptions )
348
- ) ;
349
- }
350
- if ( this . authentications . appId . apiKey ) {
351
- authenticationPromise = authenticationPromise . then ( ( ) =>
352
- this . authentications . appId . applyToRequest ( requestOptions )
353
- ) ;
354
- }
355
- authenticationPromise = authenticationPromise . then ( ( ) =>
356
- this . authentications . default . applyToRequest ( requestOptions )
357
- ) ;
358
-
359
- let interceptorPromise = authenticationPromise ;
360
- for ( const interceptor of this . interceptors ) {
361
- interceptorPromise = interceptorPromise . then ( ( ) => interceptor ( requestOptions ) ) ;
362
- }
363
-
364
- await interceptorPromise ;
365
-
366
- return this . transporter . retryableRequest ( request , requestOptions ) ;
222
+ return this . sendRequest ( request , requestOptions ) ;
367
223
}
368
224
}
0 commit comments