Spaces:
Runtime error
Runtime error
File size: 5,621 Bytes
afc0068 f0004a7 afc0068 f0004a7 afc0068 7f8f9a6 c3aaa58 7f8f9a6 afc0068 | 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | /**
* API Client Module
* Handles all API communication with authentication
*/
export class ApiClient {
constructor(authManager) {
this.authManager = authManager;
}
async authenticatedFetch(url, options = {}) {
const headers = {
...this.authManager.getAuthHeaders(),
...options.headers
};
const response = await fetch(url, {
...options,
headers
});
if (response.status === 401) {
// Token expired or invalid
this.authManager.clearAuthData();
window.location.href = '/login';
return null;
}
return response;
}
async loadFormOptions() {
try {
const [utilityResponse, phenologyResponse, categoriesResponse] = await Promise.all([
this.authenticatedFetch('/api/utilities'),
this.authenticatedFetch('/api/phenology-stages'),
this.authenticatedFetch('/api/photo-categories')
]);
if (!utilityResponse || !phenologyResponse || !categoriesResponse) {
throw new Error('Failed to load form options');
}
const [utilityData, phenologyData, categoriesData] = await Promise.all([
utilityResponse.json(),
phenologyResponse.json(),
categoriesResponse.json()
]);
return {
utilities: utilityData.utilities,
phenologyStages: phenologyData.stages,
photoCategories: categoriesData.categories
};
} catch (error) {
console.error('Error loading form options:', error);
throw error;
}
}
async saveTree(treeData) {
const response = await this.authenticatedFetch('/api/trees', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(treeData)
});
if (!response) return null;
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Unknown error');
}
return await response.json();
}
async updateTree(treeId, treeData) {
const response = await this.authenticatedFetch(`/api/trees/${treeId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(treeData)
});
if (!response) return null;
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Unknown error');
}
return await response.json();
}
async deleteTree(treeId) {
const response = await this.authenticatedFetch(`/api/trees/${treeId}`, {
method: 'DELETE'
});
if (!response) return null;
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Unknown error');
}
return true;
}
async loadTrees(limit = 20) {
const response = await this.authenticatedFetch(`/api/trees?limit=${limit}`);
if (!response) return [];
if (!response.ok) {
throw new Error('Failed to load trees');
}
return await response.json();
}
async loadTree(treeId) {
const response = await this.authenticatedFetch(`/api/trees/${treeId}`);
if (!response) return null;
if (!response.ok) {
throw new Error('Failed to fetch tree data');
}
return await response.json();
}
async loadTreeCodes() {
const response = await this.authenticatedFetch('/api/tree-codes');
if (!response) return [];
if (!response.ok) {
throw new Error('Failed to load tree codes');
}
const data = await response.json();
return data.tree_codes || [];
}
async searchTreeSuggestions(query, limit = 10) {
const response = await this.authenticatedFetch(
`/api/tree-suggestions?query=${encodeURIComponent(query)}&limit=${limit}`
);
if (!response) return [];
if (!response.ok) {
throw new Error('Failed to search tree suggestions');
}
const data = await response.json();
return data.suggestions || [];
}
async uploadFile(file, type, category = null) {
const formData = new FormData();
formData.append('file', file);
if (category) {
formData.append('category', category);
}
const endpoint = type === 'image' ? '/api/upload/image' : '/api/upload/audio';
let resJson = null;
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.authManager.authToken}`
},
body: formData
});
if (!response.ok) {
// Avoid noisy telemetry; just throw error
throw new Error('Upload failed');
}
resJson = await response.json();
return resJson;
} catch (e) {
// Network or other errors
throw e;
}
}
}
|