NLP-IBM-Debater / src /app /services /analysis.service.ts
Yassine Mhirsi
Add routing and analysis features
b6bed80
/**
* Analysis service for API calls related to argument analysis
*/
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';
/**
* Analyze arguments from JSON body
*/
export async function analyzeArguments(
request: AnalysisRequest
): Promise<AnalysisResponse> {
return api.post<AnalysisResponse, AnalysisRequest>(
API_ENDPOINTS.ANALYSIS,
request
);
}
/**
* Analyze arguments from CSV file upload
*/
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}`;
// Get user ID for header
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;
}
/**
* Get user's analysis results
*/
export async function getAnalysisResults(
limit: number = 100,
offset: number = 0
): Promise<GetAnalysisResponse> {
return api.get<GetAnalysisResponse>(
`${API_ENDPOINTS.ANALYSIS}?limit=${limit}&offset=${offset}`
);
}