Integrate v2 task api (#1566)

This commit is contained in:
Shuchang Zheng
2025-01-15 11:41:31 -08:00
committed by GitHub
parent 5c26374f4b
commit 9cd542eae3
10 changed files with 41 additions and 27 deletions

View File

@@ -1,8 +1,19 @@
import { apiBaseUrl, artifactApiBaseUrl, envCredential } from "@/util/env";
import axios from "axios";
const apiV1BaseUrl = apiBaseUrl;
const apiV2BaseUrl = apiBaseUrl.replace("v1", "v2");
const client = axios.create({
baseURL: apiBaseUrl,
baseURL: apiV1BaseUrl,
headers: {
"Content-Type": "application/json",
"x-api-key": envCredential,
},
});
const v2Client = axios.create({
baseURL: apiV2BaseUrl,
headers: {
"Content-Type": "application/json",
"x-api-key": envCredential,
@@ -15,35 +26,44 @@ const artifactApiClient = axios.create({
export function setAuthorizationHeader(token: string) {
client.defaults.headers.common["Authorization"] = `Bearer ${token}`;
v2Client.defaults.headers.common["Authorization"] = `Bearer ${token}`;
}
export function removeAuthorizationHeader() {
if (client.defaults.headers.common["Authorization"]) {
delete client.defaults.headers.common["Authorization"];
delete v2Client.defaults.headers.common["Authorization"];
}
}
export function setApiKeyHeader(apiKey: string) {
client.defaults.headers.common["X-API-Key"] = apiKey;
v2Client.defaults.headers.common["X-API-Key"] = apiKey;
}
export function removeApiKeyHeader() {
if (client.defaults.headers.common["X-API-Key"]) {
delete client.defaults.headers.common["X-API-Key"];
}
if (v2Client.defaults.headers.common["X-API-Key"]) {
delete v2Client.defaults.headers.common["X-API-Key"];
}
}
async function getClient(credentialGetter: CredentialGetter | null) {
async function getClient(
credentialGetter: CredentialGetter | null,
version: string = "v1",
) {
if (credentialGetter) {
removeApiKeyHeader();
const credential = await credentialGetter();
if (!credential) {
console.warn("No credential found");
return client;
return version === "v1" ? client : v2Client;
}
setAuthorizationHeader(credential);
}
return client;
return version === "v1" ? client : v2Client;
}
export type CredentialGetter = () => Promise<string | null>;

View File

@@ -215,7 +215,7 @@ export type WorkflowRunStatusApiResponse = {
downloaded_file_urls: Array<string> | null;
total_steps: number | null;
total_cost: number | null;
observer_cruise: ObserverCruise | null;
observer_task: ObserverTask | null;
};
export type TaskGenerationApiResponse = {
@@ -242,8 +242,8 @@ export type ActionsApiResponse = {
response: string | null;
};
export type ObserverCruise = {
observer_cruise_id: string;
export type ObserverTask = {
task_id: string;
status: Status;
workflow_run_id: string | null;
workflow_id: string | null;