| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import { Injectable } from '@angular/core';
- import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
- import { Observable, catchError, throwError } from 'rxjs';
- import { ExtractionResponse, ClaimRecord, UserAccount } from './extraction';
- @Injectable({
- providedIn: 'root'
- })
- export class ExtractionService {
- private apiUrl = 'http://localhost:8000/api/v1';
- constructor(private http: HttpClient) { }
- getUsers(): Observable<UserAccount[]> {
- return this.http.get<UserAccount[]>(`${this.apiUrl}/users`)
- .pipe(
- catchError(this.handleError)
- );
- }
- createUser(user: UserAccount): Observable<UserAccount> {
- return this.http.post<UserAccount>(`${this.apiUrl}/users`, user)
- .pipe(
- catchError(this.handleError)
- );
- }
- extractData(file: File, userName: string = 'Demo User', department: string = 'R&D'): Observable<ExtractionResponse> {
- const formData = new FormData();
- formData.append('file', file);
- formData.append('user_name', userName);
- formData.append('department', department);
- return this.http.post<ExtractionResponse>(`${this.apiUrl}/extract`, formData)
- .pipe(
- catchError(this.handleError)
- );
- }
- submitClaim(extractionData: ExtractionResponse, userId: string): Observable<ClaimRecord> {
- const headers = new HttpHeaders({
- 'user-id': userId
- });
- return this.http.post<ClaimRecord>(`${this.apiUrl}/claims`, extractionData, { headers })
- .pipe(
- catchError(this.handleError)
- );
- }
- getClaims(): Observable<ClaimRecord[]> {
- return this.http.get<ClaimRecord[]>(`${this.apiUrl}/claims`)
- .pipe(
- catchError(this.handleError)
- );
- }
- private handleError(error: HttpErrorResponse) {
- let errorMessage = 'An unknown error occurred!';
- if (error.error instanceof ErrorEvent) {
- errorMessage = `Error: ${error.error.message}`;
- } else {
- if (error.status === 504 || error.status === 0) {
- errorMessage = 'The request timed out. Please try again later.';
- } else if (error.status === 402) {
- errorMessage = 'AI extraction credits exhausted. Please contact support.';
- } else {
- errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
- }
- }
- return throwError(() => new Error(errorMessage));
- }
- }
|