Skip to content
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

Add PATCH method #24

Merged
merged 2 commits into from
Feb 9, 2024
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
44 changes: 29 additions & 15 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const TERMINAL_STATES = [
ExecutionState.FAILED,
];

enum RequestMethod {
GET = "GET",
POST = "POST",
PATCH = "PATCH",
}

// This class implements all the routes defined in the Dune API Docs: https://dune.com/docs/api/
export class DuneClient {
apiKey: string;
Expand Down Expand Up @@ -52,37 +58,45 @@ export class DuneClient {
return apiResponse;
}

private async _get<T>(url: string): Promise<T> {
log.debug(logPrefix, `GET received input url=${url}`);
const response = fetch(url, {
method: "GET",
headers: {
"x-dune-api-key": this.apiKey,
},
});
return this._handleResponse<T>(response);
}

private async _post<T>(url: string, params?: QueryParameter[]): Promise<T> {
private async _request<T>(
method: RequestMethod,
url: string,
params?: QueryParameter[],
): Promise<T> {
log.debug(
logPrefix,
`POST received input url=${url}, params=${JSON.stringify(params)}`,
`${method} received input url=${url}, params=${JSON.stringify(params)}`,
);
// Transform Query Parameter list into "dict"
const reducedParams = params?.reduce<Record<string, string>>(
(acc, { name, value }) => ({ ...acc, [name]: value }),
{},
);
const response = fetch(url, {
method: "POST",
body: JSON.stringify({ query_parameters: reducedParams || {} }),
method,
headers: {
"x-dune-api-key": this.apiKey,
},
// conditionally add the body property
...(method !== RequestMethod.GET && {
body: JSON.stringify({ query_parameters: reducedParams || {} }),
}),
});
return this._handleResponse<T>(response);
}

private async _get<T>(url: string): Promise<T> {
return this._request(RequestMethod.GET, url);
}

private async _post<T>(url: string, params?: QueryParameter[]): Promise<T> {
return this._request(RequestMethod.POST, url, params);
}

private async _patch<T>(url: string, params?: QueryParameter[]): Promise<T> {
return this._request(RequestMethod.PATCH, url, params);
}

async execute(
queryID: number,
parameters?: QueryParameter[],
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const logPrefix = "dune-client:";

export function sleep(seconds: number) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}