@visionfi/desktop-sdk
Version:
Desktop SDK for VisionFI Cloud Run services with Azure AD authentication
142 lines (125 loc) • 4.16 kB
text/typescript
/**
* Processing Client - handles processing operations for packages in Desktop SDK
* Copyright (c) 2024-2025 VisionFI. All Rights Reserved.
*/
import { AxiosInstance } from 'axios';
import {
ExecuteProcessingOptions,
ExecuteProcessingResponse,
ProcessingHistoryOptions,
ProcessingHistoryResponse,
ProcessingResult,
ProcessingViewOptions,
ProcessingViewResponse,
VisionFiError
} from '@visionfi/core';
export class ProcessingClient {
private apiClient: AxiosInstance;
constructor(apiClient: AxiosInstance) {
this.apiClient = apiClient;
}
/**
* Execute processing on a package
*/
async execute(packageId: string, options: ExecuteProcessingOptions): Promise<ExecuteProcessingResponse> {
try {
const response = await this.apiClient.post(`/operations/packages/${packageId}/processing/execute`, options);
return response.data;
} catch (error) {
throw this.handleError(error, 'Failed to execute processing');
}
}
/**
* Get processing history for a package
*/
async getHistory(packageId: string, options?: ProcessingHistoryOptions): Promise<ProcessingHistoryResponse> {
try {
const response = await this.apiClient.get(`/operations/packages/${packageId}/processing`, {
params: options
});
return response.data;
} catch (error) {
throw this.handleError(error, 'Failed to get processing history');
}
}
/**
* Get processing result
*/
async getResult(packageId: string, processingId: string): Promise<ProcessingResult> {
try {
const response = await this.apiClient.get(`/operations/packages/${packageId}/processing/${processingId}`);
return response.data;
} catch (error) {
throw this.handleError(error, 'Failed to get processing result');
}
}
/**
* Get processing view
*/
async getView(packageId: string, processingId: string, options?: ProcessingViewOptions): Promise<ProcessingViewResponse> {
try {
const response = await this.apiClient.get(`/operations/packages/${packageId}/processing/${processingId}/view`, {
params: options
});
return response.data;
} catch (error) {
throw this.handleError(error, 'Failed to get processing view');
}
}
/**
* Get processing with results (convenience method)
*/
async getWithResults(packageId: string): Promise<ProcessingHistoryResponse> {
try {
const response = await this.apiClient.get(`/operations/packages/${packageId}/processing`, {
params: { includeResults: true }
});
return response.data;
} catch (error) {
throw this.handleError(error, 'Failed to get processing with results');
}
}
/**
* Poll for processing completion with configurable intervals and attempts
*/
async pollForCompletion(
packageId: string,
processingId: string,
pollInterval: number = 3000,
maxAttempts: number = 20
): Promise<ProcessingResult> {
return new Promise((resolve, reject) => {
let attemptCount = 0;
const poll = async () => {
attemptCount++;
try {
const result = await this.getResult(packageId, processingId);
if (result.status === 'completed' || result.status === 'failed' || attemptCount >= maxAttempts) {
resolve(result);
return;
}
if (result.status === 'queued' || result.status === 'processing') {
setTimeout(poll, pollInterval);
return;
}
resolve(result);
} catch (error) {
reject(this.handleError(error, 'Error while polling for processing completion'));
}
};
poll();
});
}
/**
* Handle errors consistently
*/
private handleError(error: any, defaultMessage: string): Error {
if (error instanceof VisionFiError) {
return error;
}
const message = error.response?.data?.message || error.message || defaultMessage;
const statusCode = error.response?.status;
const code = error.response?.data?.code || 'unknown_error';
return new VisionFiError(message, statusCode, code);
}
}