Skip to content

Commit

Permalink
use ExecutionParams interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith committed Feb 15, 2024
1 parent 1ce122e commit acee6f9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
17 changes: 12 additions & 5 deletions src/api/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ import {
import log from "loglevel";
import { logPrefix } from "../utils";
import { Router } from "./router";
import { ExecutionPerformance } from "../types/requestPayload";
import { ExecutionParams, ExecutionPerformance } from "../types/requestPayload";

// This class implements all the routes defined in the Dune API Docs: https://dune.com/docs/api/
export class ExecutionClient extends Router {
async executeQuery(
queryID: number,
parameters?: QueryParameter[],
performance?: ExecutionPerformance,
params?: ExecutionParams,
): Promise<ExecutionResponse> {
// Extract possible ExecutionParams
let query_parameters: QueryParameter[] = [];
let performance = ExecutionPerformance.Medium;
if (params !== undefined) {
query_parameters = params.query_parameters ? params.query_parameters : [];
performance = performance ? performance : ExecutionPerformance.Medium;
}

const response = await this._post<ExecutionResponse>(`query/${queryID}/execute`, {
query_parameters: parameters ? parameters : [],
performance: performance ? performance : ExecutionPerformance.Medium,
query_parameters,
performance,
});
log.debug(logPrefix, `execute response ${JSON.stringify(response)}`);
return response as ExecutionResponse;
Expand Down
13 changes: 4 additions & 9 deletions src/api/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import log from "loglevel";
import { logPrefix } from "../utils";
import { ExecutionClient } from "./execution";
import { POLL_FREQUENCY_SECONDS, THREE_MONTHS_IN_HOURS } from "../constants";
import { ExecutionPerformance } from "../types/requestPayload";
import { ExecutionParams, ExecutionPerformance } from "../types/requestPayload";

const TERMINAL_STATES = [
ExecutionState.CANCELLED,
Expand All @@ -15,21 +15,16 @@ const TERMINAL_STATES = [
export class ExtendedClient extends ExecutionClient {
async runQuery(
queryID: number,
parameters?: QueryParameter[],
params?: ExecutionParams,
pingFrequency: number = POLL_FREQUENCY_SECONDS,
performance?: ExecutionPerformance,
): Promise<ResultsResponse> {
log.info(
logPrefix,
`refreshing query https://dune.com/queries/${queryID} with parameters ${JSON.stringify(
parameters,
params,
)}`,
);
const { execution_id: jobID } = await this.executeQuery(
queryID,
parameters,
performance,
);
const { execution_id: jobID } = await this.executeQuery(queryID, params);
let { state } = await this.getExecutionStatus(jobID);
while (!TERMINAL_STATES.includes(state)) {
log.info(
Expand Down
5 changes: 5 additions & 0 deletions src/types/requestPayload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { QueryParameter } from "./queryParameter";

export interface ExecutionParams {
query_parameters?: QueryParameter[];
performance?: ExecutionPerformance;
}

export enum ExecutionPerformance {
Medium = "medium",
Large = "large",
Expand Down
17 changes: 9 additions & 8 deletions tests/e2e/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ describe("DuneClient: native routes", () => {
QueryParameter.enum("ListField", "Option 1"),
];
// Execute and check state
const execution = await client.executeQuery(queryID, parameters);
const execution = await client.executeQuery(queryID, {
query_parameters: parameters,
});
expect(execution.execution_id).is.not.null;
});

it("execute with Large tier performance", async () => {
const client = new DuneClient(apiKey);
const execution = await client.executeQuery(
1215383,
undefined,
ExecutionPerformance.Large,
);
const execution = await client.executeQuery(1215383, {
performance: ExecutionPerformance.Large,
});
expect(execution.execution_id).is.not.null;
});

Expand Down Expand Up @@ -191,9 +191,10 @@ describe("DuneClient: Errors", () => {
const client = new DuneClient(apiKey);
const queryID = 1215383;
const invalidParameterName = "Invalid Parameter Name";
const parameters = [QueryParameter.text(invalidParameterName, "")];
await expectAsyncThrow(
client.executeQuery(queryID, parameters),
client.executeQuery(queryID, {
query_parameters: [QueryParameter.text(invalidParameterName, "")],
}),
`unknown parameters (${invalidParameterName})`,
);
});
Expand Down

0 comments on commit acee6f9

Please sign in to comment.