File size: 1,638 Bytes
a63cedf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * API client for backend communication
 */
export class ApiClient {
    /**
     * @param {string} baseUrl - Base URL for API (default: current origin)
     */
    constructor(baseUrl = '') {
        this.baseUrl = baseUrl;
    }

    /**
     * Uploads an image for analysis
     * @param {FormData} formData - Form data containing image file
     * @returns {Promise<Object>} { task_id: string }
     * @throws {Error} If upload fails
     */
    async uploadImage(formData) {
        const response = await fetch(`${this.baseUrl}/upload`, {
            method: 'POST',
            body: formData
        });

        if (!response.ok) {
            throw new Error(`Upload failed: ${response.statusText}`);
        }

        return response.json();
    }

    /**
     * Gets progress for a task
     * @param {string} taskId - Task ID
     * @returns {Promise<Object|null>} Task data or null if not found
     */
    async getProgress(taskId) {
        try {
            const response = await fetch(`${this.baseUrl}/progress/${taskId}`);
            if (!response.ok) return null;
            return response.json();
        } catch (error) {
            console.error('Failed to fetch progress:', error);
            return null;
        }
    }

    /**
     * Gets aggregate statistics
     * @returns {Promise<Object>} Statistics data
     * @throws {Error} If fetch fails
     */
    async getStats() {
        const response = await fetch(`${this.baseUrl}/stats`);
        if (!response.ok) {
            throw new Error(`Failed to fetch stats: ${response.statusText}`);
        }
        return response.json();
    }
}