Skip to content

Commit

Permalink
Add PATCH method (#24)
Browse files Browse the repository at this point in the history
route all methods to _request & add patch
  • Loading branch information
bh2smith authored Feb 9, 2024
1 parent 0cc528d commit bd0634e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
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));
}

0 comments on commit bd0634e

Please sign in to comment.