UNPKG

benchling_typescript_sdk

Version:

Typescript SDK for Benchling API

52 lines (46 loc) 1.86 kB
import { BaseClient } from "../BaseClient"; import { chunkQueries } from "../superModules/chunkQueries"; import { AsyncTaskLink, ExportAuditLogAsyncTask } from "../types"; export class Audit { private client: BaseClient; constructor(client: BaseClient) { this.client = client; } public async waitForAuditLog( object_id: string, format: "CSV" | "PDF" ): Promise<{ object_id: string; downloadURL: string | null }> { let task = await this.client.postData<AsyncTaskLink>(`audit/log/${object_id}`, { format }, {}); if (task.taskId === undefined) { throw new Error(`Task ID is undefined for audit log request for object ${object_id}`); } let response = await this.client.awaitForTask<{ downloadURL: string | undefined; errors: any }>( task.taskId ); if (response.downloadURL) { return { object_id, downloadURL: response.downloadURL }; } else if (response.errors) { console.error(`Audit log download URL is undefined for object ${object_id}`); console.error(response.errors); console.error(response); } return { object_id, downloadURL: null }; } public async *getAuditDownloadUrls( object_ids: string[], format: "CSV" | "PDF" ): AsyncGenerator<{ object_id: string; downloadURL: string | null }> { const seconds_interval = 10 * 1000; // 9 seconds in milliseconds for (const object_id of object_ids) { const startTime = Date.now(); // Call waitForAuditLog and get the download URL const res = await this.waitForAuditLog(object_id, format); yield res; // Calculate elapsed time and wait for the remaining interval const elapsedTime = Date.now() - startTime; if (elapsedTime < seconds_interval) { await new Promise((resolve) => setTimeout(resolve, seconds_interval - elapsedTime)); } } } }