Skip to content

Commit d3af41e

Browse files
authored
fix: add echo requester and fix timeouts (#14)
* fix: simplify model with types and simplify api (#15)
1 parent a70ab87 commit d3af41e

39 files changed

+711
-2270
lines changed

clients/algoliasearch-client-javascript/client-search/apis.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import { SearchApi } from './searchApi';
44
export class searchClient extends SearchApi {}
55

66
export * from '../utils/errors';
7+
export { EchoRequester } from '../utils/EchoRequester';
78

89
export const APIS = [SearchApi];
Lines changed: 32 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import http from 'http';
21
import { shuffle } from '../utils/helpers';
32
import { Transporter } from '../utils/Transporter';
43
import { Headers, Host, Request, RequestOptions } from '../utils/types';
4+
import { Requester } from '../utils/Requester';
55

66
import { BatchObject } from '../model/batchObject';
77
import { BatchResponse } from '../model/batchResponse';
@@ -12,9 +12,7 @@ import { SaveObjectResponse } from '../model/saveObjectResponse';
1212
import { SearchParams } from '../model/searchParams';
1313
import { SearchParamsString } from '../model/searchParamsString';
1414
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';
1816

1917
export enum SearchApiApiKeys {
2018
apiKey,
@@ -25,14 +23,11 @@ export class SearchApi {
2523
private transporter: Transporter;
2624

2725
protected authentications = {
28-
default: <Authentication>new VoidAuth(),
2926
apiKey: new ApiKeyAuth('header', 'X-Algolia-API-Key'),
3027
appId: new ApiKeyAuth('header', 'X-Algolia-Application-Id'),
3128
};
3229

33-
protected interceptors: Interceptor[] = [];
34-
35-
constructor(appId: string, apiKey: string) {
30+
constructor(appId: string, apiKey: string, requester?: Requester) {
3631
this.setApiKey(SearchApiApiKeys.appId, appId);
3732
this.setApiKey(SearchApiApiKeys.apiKey, apiKey);
3833
this.transporter = new Transporter({
@@ -57,19 +52,25 @@ export class SearchApi {
5752
read: 5,
5853
write: 30,
5954
},
55+
requester,
6056
});
6157
}
6258

63-
public setDefaultAuthentication(auth: Authentication) {
64-
this.authentications.default = auth;
65-
}
66-
6759
public setApiKey(key: SearchApiApiKeys, value: string) {
68-
(this.authentications as any)[SearchApiApiKeys[key]].apiKey = value;
60+
this.authentications[SearchApiApiKeys[key]].apiKey = value;
6961
}
7062

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);
7374
}
7475

7576
/**
@@ -78,137 +79,65 @@ export class SearchApi {
7879
* @param indexName The index in which to perform the request
7980
* @param batchObject
8081
*/
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> {
8683
const path = '/1/indexes/{indexName}/batch'.replace(
8784
'{' + 'indexName' + '}',
8885
encodeURIComponent(String(indexName))
8986
);
90-
let headers: Headers = {};
87+
let headers: Headers = { Accept: 'application/json' };
9188
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> = {};
10089

101-
// verify required parameter 'indexName' is not null or undefined
10290
if (indexName === null || indexName === undefined) {
10391
throw new Error('Required parameter indexName was null or undefined when calling batch.');
10492
}
10593

106-
// verify required parameter 'batchObject' is not null or undefined
10794
if (batchObject === null || batchObject === undefined) {
10895
throw new Error('Required parameter batchObject was null or undefined when calling batch.');
10996
}
11097

111-
headers = { ...headers, ...options.headers };
112-
11398
const request: Request = {
11499
method: 'POST',
115100
path,
116-
data: ObjectSerializer.serialize(batchObject, 'BatchObject'),
101+
data: batchObject,
117102
};
118103

119104
const requestOptions: RequestOptions = {
120105
headers,
121106
queryParameters,
122107
};
123108

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);
147110
}
148111
/**
149112
*
150113
* @summary Get search results for the given requests.
151114
* @param multipleQueriesObject
152115
*/
153116
public async multipleQueries(
154-
multipleQueriesObject: MultipleQueriesObject,
155-
options: { headers: { [name: string]: string } } = { headers: {} }
117+
multipleQueriesObject: MultipleQueriesObject
156118
): Promise<MultipleQueriesResponse> {
157119
const path = '/1/indexes/*/queries';
158-
let headers: Headers = {};
120+
let headers: Headers = { Accept: 'application/json' };
159121
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> = {};
168122

169-
// verify required parameter 'multipleQueriesObject' is not null or undefined
170123
if (multipleQueriesObject === null || multipleQueriesObject === undefined) {
171124
throw new Error(
172125
'Required parameter multipleQueriesObject was null or undefined when calling multipleQueries.'
173126
);
174127
}
175128

176-
headers = { ...headers, ...options.headers };
177-
178129
const request: Request = {
179130
method: 'POST',
180131
path,
181-
data: ObjectSerializer.serialize(multipleQueriesObject, 'MultipleQueriesObject'),
132+
data: multipleQueriesObject,
182133
};
183134

184135
const requestOptions: RequestOptions = {
185136
headers,
186137
queryParameters,
187138
};
188139

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);
212141
}
213142
/**
214143
* Add an object to the index, automatically assigning it an object ID
@@ -218,74 +147,39 @@ export class SearchApi {
218147
*/
219148
public async saveObject(
220149
indexName: string,
221-
requestBody: { [key: string]: object },
222-
options: { headers: { [name: string]: string } } = { headers: {} }
150+
requestBody: { [key: string]: object }
223151
): Promise<SaveObjectResponse> {
224152
const path = '/1/indexes/{indexName}'.replace(
225153
'{' + 'indexName' + '}',
226154
encodeURIComponent(String(indexName))
227155
);
228-
let headers: Headers = {};
156+
let headers: Headers = { Accept: 'application/json' };
229157
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> = {};
238158

239-
// verify required parameter 'indexName' is not null or undefined
240159
if (indexName === null || indexName === undefined) {
241160
throw new Error(
242161
'Required parameter indexName was null or undefined when calling saveObject.'
243162
);
244163
}
245164

246-
// verify required parameter 'requestBody' is not null or undefined
247165
if (requestBody === null || requestBody === undefined) {
248166
throw new Error(
249167
'Required parameter requestBody was null or undefined when calling saveObject.'
250168
);
251169
}
252170

253-
headers = { ...headers, ...options.headers };
254-
255171
const request: Request = {
256172
method: 'POST',
257173
path,
258-
data: ObjectSerializer.serialize(requestBody, '{ [key: string]: object; }'),
174+
data: requestBody,
259175
};
260176

261177
const requestOptions: RequestOptions = {
262178
headers,
263179
queryParameters,
264180
};
265181

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);
289183
}
290184
/**
291185
*
@@ -295,74 +189,36 @@ export class SearchApi {
295189
*/
296190
public async search(
297191
indexName: string,
298-
searchParamsSearchParamsString: SearchParams | SearchParamsString,
299-
options: { headers: { [name: string]: string } } = { headers: {} }
192+
searchParamsSearchParamsString: SearchParams | SearchParamsString
300193
): Promise<SearchResponse> {
301194
const path = '/1/indexes/{indexName}/query'.replace(
302195
'{' + 'indexName' + '}',
303196
encodeURIComponent(String(indexName))
304197
);
305-
let headers: Headers = {};
198+
let headers: Headers = { Accept: 'application/json' };
306199
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> = {};
315200

316-
// verify required parameter 'indexName' is not null or undefined
317201
if (indexName === null || indexName === undefined) {
318202
throw new Error('Required parameter indexName was null or undefined when calling search.');
319203
}
320204

321-
// verify required parameter 'searchParamsSearchParamsString' is not null or undefined
322205
if (searchParamsSearchParamsString === null || searchParamsSearchParamsString === undefined) {
323206
throw new Error(
324207
'Required parameter searchParamsSearchParamsString was null or undefined when calling search.'
325208
);
326209
}
327210

328-
headers = { ...headers, ...options.headers };
329-
330211
const request: Request = {
331212
method: 'POST',
332213
path,
333-
data: ObjectSerializer.serialize(
334-
searchParamsSearchParamsString,
335-
'SearchParams | SearchParamsString'
336-
),
214+
data: searchParamsSearchParamsString,
337215
};
338216

339217
const requestOptions: RequestOptions = {
340218
headers,
341219
queryParameters,
342220
};
343221

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);
367223
}
368224
}

0 commit comments

Comments
 (0)