Skip to content

feat: add keys endpoints to search client #41

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 15 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
@@ -0,0 +1,10 @@
export type AddApiKeyResponse = {
/**
* Key string.
*/
key: string;
/**
* Date of creation (ISO-8601 format).
*/
createdAt: Date;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Api Key object.
*/
export type ApiKey = {
/**
* Set of permissions associated with the key.
*/
acl: ApiKey.AclEnum[];
/**
* A comment used to identify a key more easily in the dashboard. It is not interpreted by the API.
*/
description?: string;
/**
* Restrict this new API key to a list of indices or index patterns. If the list is empty, all indices are allowed.
*/
indexes?: string[];
/**
* Maximum number of hits this API key can retrieve in one query. If zero, no limit is enforced.
*/
maxHitsPerQuery?: number;
/**
* Maximum number of API calls per hour allowed from a given IP address or a user token.
*/
maxQueriesPerIPPerHour?: number;
/**
* URL-encoded query string. Force some query parameters to be applied for each query made with this API key.
*/
queryParameters?: string;
/**
* Restrict this new API key to specific referers. If empty or blank, defaults to all referers.
*/
referers?: string[];
/**
* Validity limit for this key in seconds. The key will automatically be removed after this period of time.
*/
validity?: number;
};

export namespace ApiKey {
export enum AclEnum {
Search = 'search',
Browse = 'browse',
AddObject = 'addObject',
DeleteObject = 'deleteObject',
DeleteIndex = 'deleteIndex',
Settings = 'settings',
EditSettings = 'editSettings',
Analytics = 'analytics',
Recommendation = 'recommendation',
ListIndexes = 'listIndexes',
Logs = 'logs',
SeeUnretrievableAttributes = 'seeUnretrievableAttributes',
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type DeleteApiKeyResponse = {
/**
* Date of deletion (ISO-8601 format).
*/
deletedAt: Date;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { ApiKey } from './apiKey';
import type { KeyObjectAllOf } from './keyObjectAllOf';

export type KeyObject = ApiKey & KeyObjectAllOf;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type KeyObjectAllOf = {
/**
* Date of creation (ISO-8601 format).
*/
createdAt: Date;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { KeyObject } from './keyObject';

export type ListApiKeysResponse = {
/**
* List of api keys.
*/
keys: KeyObject[];
};
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
/* eslint-disable no-param-reassign */
import type { RequestOptions } from '../utils/types';

export * from './addApiKeyResponse';
export * from './apiKey';
export * from './baseIndexSettings';
export * from './baseSearchParams';
export * from './baseSearchResponse';
export * from './baseSearchResponseFacetsStats';
export * from './batchObject';
export * from './batchResponse';
export * from './clearAllSynonymsResponse';
export * from './deleteApiKeyResponse';
export * from './deleteIndexResponse';
export * from './deleteSynonymResponse';
export * from './errorBase';
export * from './highlightResult';
export * from './index';
export * from './indexSettings';
export * from './indexSettingsAsSearchParams';
export * from './keyObject';
export * from './keyObjectAllOf';
export * from './listApiKeysResponse';
export * from './listIndicesResponse';
export * from './multipleQueries';
export * from './multipleQueriesObject';
Expand All @@ -37,6 +43,7 @@ export * from './setSettingsResponse';
export * from './snippetResult';
export * from './synonymHit';
export * from './synonymHitHighlightResult';
export * from './updateApiKeyResponse';

export interface Authentication {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type UpdateApiKeyResponse = {
/**
* Key string.
*/
key: string;
/**
* Date of last update (ISO-8601 format).
*/
updatedAt: Date;
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { AddApiKeyResponse } from '../model/addApiKeyResponse';
import type { ApiKey } from '../model/apiKey';
import type { BatchObject } from '../model/batchObject';
import type { BatchResponse } from '../model/batchResponse';
import type { ClearAllSynonymsResponse } from '../model/clearAllSynonymsResponse';
import type { DeleteApiKeyResponse } from '../model/deleteApiKeyResponse';
import type { DeleteIndexResponse } from '../model/deleteIndexResponse';
import type { DeleteSynonymResponse } from '../model/deleteSynonymResponse';
import type { IndexSettings } from '../model/indexSettings';
import type { KeyObject } from '../model/keyObject';
import type { ListApiKeysResponse } from '../model/listApiKeysResponse';
import type { ListIndicesResponse } from '../model/listIndicesResponse';
import { ApiKeyAuth } from '../model/models';
import type { MultipleQueriesObject } from '../model/multipleQueriesObject';
Expand All @@ -19,6 +24,7 @@ import type { SearchResponse } from '../model/searchResponse';
import type { SearchSynonymsResponse } from '../model/searchSynonymsResponse';
import type { SetSettingsResponse } from '../model/setSettingsResponse';
import type { SynonymHit } from '../model/synonymHit';
import type { UpdateApiKeyResponse } from '../model/updateApiKeyResponse';
import { Transporter } from '../utils/Transporter';
import { shuffle } from '../utils/helpers';
import type { Requester } from '../utils/requester/Requester';
Expand Down Expand Up @@ -114,6 +120,36 @@ export class SearchApi {
this.authentications[SearchApiKeys[key]].apiKey = value;
}

/**
* Add a new API Key with specific permissions/restrictions.
*
* @summary Create a new API key.
* @param apiKey - The apiKey.
*/
addApiKey(apiKey: ApiKey): Promise<AddApiKeyResponse> {
const path = '/1/keys';
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

if (apiKey === null || apiKey === undefined) {
throw new Error(
'Required parameter apiKey was null or undefined when calling addApiKey.'
);
}

const request: Request = {
method: 'POST',
path,
data: apiKey,
};

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

return this.sendRequest(request, requestOptions);
}
/**
* Performs multiple write operations in a single API call.
*
Expand Down Expand Up @@ -193,6 +229,38 @@ export class SearchApi {

return this.sendRequest(request, requestOptions);
}
/**
* Delete an existing API Key.
*
* @summary Delete an API key.
* @param key - API Key string.
*/
deleteApiKey(key: string): Promise<DeleteApiKeyResponse> {
const path = '/1/keys/{key}'.replace(
'{key}',
encodeURIComponent(String(key))
);
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

if (key === null || key === undefined) {
throw new Error(
'Required parameter key was null or undefined when calling deleteApiKey.'
);
}

const request: Request = {
method: 'DELETE',
path,
};

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

return this.sendRequest(request, requestOptions);
}
/**
* Delete an existing index.
*
Expand Down Expand Up @@ -272,6 +340,38 @@ export class SearchApi {

return this.sendRequest(request, requestOptions);
}
/**
* Get the permissions of an API key.
*
* @summary Get an API key.
* @param key - API Key string.
*/
getApiKey(key: string): Promise<KeyObject> {
const path = '/1/keys/{key}'.replace(
'{key}',
encodeURIComponent(String(key))
);
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

if (key === null || key === undefined) {
throw new Error(
'Required parameter key was null or undefined when calling getApiKey.'
);
}

const request: Request = {
method: 'GET',
path,
};

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

return this.sendRequest(request, requestOptions);
}
/**
* Retrieve settings of a given indexName.
*
Expand Down Expand Up @@ -341,6 +441,28 @@ export class SearchApi {

return this.sendRequest(request, requestOptions);
}
/**
* List API keys, along with their associated rights.
*
* @summary Get the full list of API Keys.
*/
listApiKeys(): Promise<ListApiKeysResponse> {
const path = '/1/keys';
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

const request: Request = {
method: 'GET',
path,
};

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

return this.sendRequest(request, requestOptions);
}
/**
* List existing indexes from an application.
*
Expand Down Expand Up @@ -442,6 +564,38 @@ export class SearchApi {

return this.sendRequest(request, requestOptions);
}
/**
* Restore a deleted API key, along with its associated rights.
*
* @summary Restore an API key.
* @param key - API Key string.
*/
restoreApiKey(key: string): Promise<AddApiKeyResponse> {
const path = '/1/keys/{key}/restore'.replace(
'{key}',
encodeURIComponent(String(key))
);
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

if (key === null || key === undefined) {
throw new Error(
'Required parameter key was null or undefined when calling restoreApiKey.'
);
}

const request: Request = {
method: 'POST',
path,
};

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

return this.sendRequest(request, requestOptions);
}
/**
* Add an object to the index, automatically assigning it an object ID.
*
Expand Down Expand Up @@ -750,6 +904,46 @@ export class SearchApi {
queryParameters,
};

return this.sendRequest(request, requestOptions);
}
/**
* Replace every permission of an existing API key.
*
* @summary Update an API key.
* @param key - API Key string.
* @param apiKey - The apiKey.
*/
updateApiKey(key: string, apiKey: ApiKey): Promise<UpdateApiKeyResponse> {
const path = '/1/keys/{key}'.replace(
'{key}',
encodeURIComponent(String(key))
);
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

if (key === null || key === undefined) {
throw new Error(
'Required parameter key was null or undefined when calling updateApiKey.'
);
}

if (apiKey === null || apiKey === undefined) {
throw new Error(
'Required parameter apiKey was null or undefined when calling updateApiKey.'
);
}

const request: Request = {
method: 'PUT',
path,
data: apiKey,
};

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

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