|
|
|
|
|
|
|
|
|
|
|
|
|
|
import api from './api-wrapper.ts'; |
|
|
import type { |
|
|
AnalysisRequest, |
|
|
AnalysisResponse, |
|
|
GetAnalysisResponse, |
|
|
} from '../types/analysis.types.ts'; |
|
|
import { API_ENDPOINTS } from '../constants/index.ts'; |
|
|
import { getUserId } from '../utils/index.ts'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function analyzeArguments( |
|
|
request: AnalysisRequest |
|
|
): Promise<AnalysisResponse> { |
|
|
return api.post<AnalysisResponse, AnalysisRequest>( |
|
|
API_ENDPOINTS.ANALYSIS, |
|
|
request |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function analyzeArgumentsFromCsv( |
|
|
file: File |
|
|
): Promise<AnalysisResponse> { |
|
|
const formData = new FormData(); |
|
|
formData.append('file', file); |
|
|
|
|
|
const BASE_URL = |
|
|
process.env.REACT_APP_API_BASE_URL?.replace(/\/$/, '') || ''; |
|
|
const url = `${BASE_URL}${API_ENDPOINTS.ANALYSIS_CSV}`; |
|
|
|
|
|
|
|
|
const userId = getUserId(); |
|
|
const headers: Record<string, string> = {}; |
|
|
if (userId) { |
|
|
headers['X-User-ID'] = userId; |
|
|
} |
|
|
|
|
|
const response = await fetch(url, { |
|
|
method: 'POST', |
|
|
headers, |
|
|
body: formData, |
|
|
}); |
|
|
|
|
|
const contentType = response.headers.get('content-type'); |
|
|
const isJson = contentType?.includes('application/json'); |
|
|
const payload = isJson ? await response.json().catch(() => undefined) : undefined; |
|
|
|
|
|
if (!response.ok) { |
|
|
const error = { |
|
|
status: response.status, |
|
|
message: |
|
|
(payload as { detail?: string })?.detail || |
|
|
(payload as { message?: string })?.message || |
|
|
response.statusText || |
|
|
'Request failed', |
|
|
details: payload, |
|
|
}; |
|
|
throw error; |
|
|
} |
|
|
|
|
|
return payload as AnalysisResponse; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function getAnalysisResults( |
|
|
limit: number = 100, |
|
|
offset: number = 0 |
|
|
): Promise<GetAnalysisResponse> { |
|
|
return api.get<GetAnalysisResponse>( |
|
|
`${API_ENDPOINTS.ANALYSIS}?limit=${limit}&offset=${offset}` |
|
|
); |
|
|
} |
|
|
|
|
|
|