Skip to content

fix: add echo requester and fix timeouts #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import { SearchApi } from './searchApi';
export class searchClient extends SearchApi {}

export * from '../utils/errors';
export { EchoRequester } from '../utils/EchoRequester';

export const APIS = [SearchApi];
208 changes: 32 additions & 176 deletions clients/algoliasearch-client-javascript/client-search/searchApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from 'http';
import { shuffle } from '../utils/helpers';
import { Transporter } from '../utils/Transporter';
import { Headers, Host, Request, RequestOptions } from '../utils/types';
import { Requester } from '../utils/Requester';

import { BatchObject } from '../model/batchObject';
import { BatchResponse } from '../model/batchResponse';
Expand All @@ -12,9 +12,7 @@ import { SaveObjectResponse } from '../model/saveObjectResponse';
import { SearchParams } from '../model/searchParams';
import { SearchParamsString } from '../model/searchParamsString';
import { SearchResponse } from '../model/searchResponse';

import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
import { HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
import { ApiKeyAuth } from '../model/models';

export enum SearchApiApiKeys {
apiKey,
Expand All @@ -25,14 +23,11 @@ export class SearchApi {
private transporter: Transporter;

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

protected interceptors: Interceptor[] = [];

constructor(appId: string, apiKey: string) {
constructor(appId: string, apiKey: string, requester?: Requester) {
this.setApiKey(SearchApiApiKeys.appId, appId);
this.setApiKey(SearchApiApiKeys.apiKey, apiKey);
this.transporter = new Transporter({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not in the scope of this PR, but just wanted to make sure everyone's aware of this; I would recommend making the hosts (or the whole transporter) something you can pass to the constructor as well. Once Metis is released, the client would need to work with different hosts than the one we set by default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should prepare this codebase for Metis even if we don't have an ETA?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Does not cost anything, but just wondering)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good to keep in mind for sure, otherwise we might have to rework a lot later on 🙂 Besides the hosts part, not a lot should change, though (perhaps the retry strategy but that is a problem for later 😄 )

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point I'll include the host too

Expand All @@ -57,19 +52,25 @@ export class SearchApi {
read: 5,
write: 30,
},
requester,
});
}

public setDefaultAuthentication(auth: Authentication) {
this.authentications.default = auth;
}

public setApiKey(key: SearchApiApiKeys, value: string) {
(this.authentications as any)[SearchApiApiKeys[key]].apiKey = value;
this.authentications[SearchApiApiKeys[key]].apiKey = value;
}

public addInterceptor(interceptor: Interceptor) {
this.interceptors.push(interceptor);
private async sendRequest<TResponse>(
request: Request,
requestOptions: RequestOptions
): Promise<TResponse> {
if (this.authentications.apiKey.apiKey) {
this.authentications.apiKey.applyToRequest(requestOptions);
}
if (this.authentications.appId.apiKey) {
this.authentications.appId.applyToRequest(requestOptions);
}
return this.transporter.request(request, requestOptions);
}

/**
Expand All @@ -78,137 +79,65 @@ export class SearchApi {
* @param indexName The index in which to perform the request
* @param batchObject
*/
public async batch(
indexName: string,
batchObject: BatchObject,
options: { headers: { [name: string]: string } } = { headers: {} }
): Promise<BatchResponse> {
public async batch(indexName: string, batchObject: BatchObject): Promise<BatchResponse> {
const path = '/1/indexes/{indexName}/batch'.replace(
'{' + 'indexName' + '}',
encodeURIComponent(String(indexName))
);
let headers: Headers = {};
let headers: Headers = { Accept: 'application/json' };
let queryParameters: Record<string, string> = {};
const produces = ['application/json'];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
headers.Accept = 'application/json';
} else {
headers.Accept = produces.join(',');
}
let formParams: Record<string, string> = {};

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

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

headers = { ...headers, ...options.headers };

const request: Request = {
method: 'POST',
path,
data: ObjectSerializer.serialize(batchObject, 'BatchObject'),
data: batchObject,
};

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(requestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(requestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(requestOptions)
);

let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}

await interceptorPromise;

return this.transporter.retryableRequest(request, requestOptions);
return this.sendRequest(request, requestOptions);
}
/**
*
* @summary Get search results for the given requests.
* @param multipleQueriesObject
*/
public async multipleQueries(
multipleQueriesObject: MultipleQueriesObject,
options: { headers: { [name: string]: string } } = { headers: {} }
multipleQueriesObject: MultipleQueriesObject
): Promise<MultipleQueriesResponse> {
const path = '/1/indexes/*/queries';
let headers: Headers = {};
let headers: Headers = { Accept: 'application/json' };
let queryParameters: Record<string, string> = {};
const produces = ['application/json'];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
headers.Accept = 'application/json';
} else {
headers.Accept = produces.join(',');
}
let formParams: Record<string, string> = {};

// verify required parameter 'multipleQueriesObject' is not null or undefined
if (multipleQueriesObject === null || multipleQueriesObject === undefined) {
throw new Error(
'Required parameter multipleQueriesObject was null or undefined when calling multipleQueries.'
);
}

headers = { ...headers, ...options.headers };

const request: Request = {
method: 'POST',
path,
data: ObjectSerializer.serialize(multipleQueriesObject, 'MultipleQueriesObject'),
data: multipleQueriesObject,
};

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(requestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(requestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(requestOptions)
);

let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}

await interceptorPromise;

return this.transporter.retryableRequest(request, requestOptions);
return this.sendRequest(request, requestOptions);
}
/**
* Add an object to the index, automatically assigning it an object ID
Expand All @@ -218,74 +147,39 @@ export class SearchApi {
*/
public async saveObject(
indexName: string,
requestBody: { [key: string]: object },
options: { headers: { [name: string]: string } } = { headers: {} }
requestBody: { [key: string]: object }
): Promise<SaveObjectResponse> {
const path = '/1/indexes/{indexName}'.replace(
'{' + 'indexName' + '}',
encodeURIComponent(String(indexName))
);
let headers: Headers = {};
let headers: Headers = { Accept: 'application/json' };
let queryParameters: Record<string, string> = {};
const produces = ['application/json'];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
headers.Accept = 'application/json';
} else {
headers.Accept = produces.join(',');
}
let formParams: Record<string, string> = {};

// verify required parameter 'indexName' is not null or undefined
if (indexName === null || indexName === undefined) {
throw new Error(
'Required parameter indexName was null or undefined when calling saveObject.'
);
}

// verify required parameter 'requestBody' is not null or undefined
if (requestBody === null || requestBody === undefined) {
throw new Error(
'Required parameter requestBody was null or undefined when calling saveObject.'
);
}

headers = { ...headers, ...options.headers };

const request: Request = {
method: 'POST',
path,
data: ObjectSerializer.serialize(requestBody, '{ [key: string]: object; }'),
data: requestBody,
};

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(requestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(requestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(requestOptions)
);

let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}

await interceptorPromise;

return this.transporter.retryableRequest(request, requestOptions);
return this.sendRequest(request, requestOptions);
}
/**
*
Expand All @@ -295,74 +189,36 @@ export class SearchApi {
*/
public async search(
indexName: string,
searchParamsSearchParamsString: SearchParams | SearchParamsString,
options: { headers: { [name: string]: string } } = { headers: {} }
searchParamsSearchParamsString: SearchParams | SearchParamsString
): Promise<SearchResponse> {
const path = '/1/indexes/{indexName}/query'.replace(
'{' + 'indexName' + '}',
encodeURIComponent(String(indexName))
);
let headers: Headers = {};
let headers: Headers = { Accept: 'application/json' };
let queryParameters: Record<string, string> = {};
const produces = ['application/json'];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
headers.Accept = 'application/json';
} else {
headers.Accept = produces.join(',');
}
let formParams: Record<string, string> = {};

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

// verify required parameter 'searchParamsSearchParamsString' is not null or undefined
if (searchParamsSearchParamsString === null || searchParamsSearchParamsString === undefined) {
throw new Error(
'Required parameter searchParamsSearchParamsString was null or undefined when calling search.'
);
}

headers = { ...headers, ...options.headers };

const request: Request = {
method: 'POST',
path,
data: ObjectSerializer.serialize(
searchParamsSearchParamsString,
'SearchParams | SearchParamsString'
),
data: searchParamsSearchParamsString,
};

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(requestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(requestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(requestOptions)
);

let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}

await interceptorPromise;

return this.transporter.retryableRequest(request, requestOptions);
return this.sendRequest(request, requestOptions);
}
}
Loading