extraction.service.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
  3. import { Observable, catchError, throwError } from 'rxjs';
  4. import { ExtractionResponse, ClaimRecord, UserAccount } from './extraction';
  5. @Injectable({
  6. providedIn: 'root'
  7. })
  8. export class ExtractionService {
  9. private apiUrl = 'http://localhost:8000/api/v1';
  10. constructor(private http: HttpClient) { }
  11. getUsers(): Observable<UserAccount[]> {
  12. return this.http.get<UserAccount[]>(`${this.apiUrl}/users`)
  13. .pipe(
  14. catchError(this.handleError)
  15. );
  16. }
  17. createUser(user: UserAccount): Observable<UserAccount> {
  18. return this.http.post<UserAccount>(`${this.apiUrl}/users`, user)
  19. .pipe(
  20. catchError(this.handleError)
  21. );
  22. }
  23. extractData(file: File, userName: string = 'Demo User', department: string = 'R&D'): Observable<ExtractionResponse> {
  24. const formData = new FormData();
  25. formData.append('file', file);
  26. formData.append('user_name', userName);
  27. formData.append('department', department);
  28. return this.http.post<ExtractionResponse>(`${this.apiUrl}/extract`, formData)
  29. .pipe(
  30. catchError(this.handleError)
  31. );
  32. }
  33. submitClaim(extractionData: ExtractionResponse, userId: string): Observable<ClaimRecord> {
  34. const headers = new HttpHeaders({
  35. 'user-id': userId
  36. });
  37. return this.http.post<ClaimRecord>(`${this.apiUrl}/claims`, extractionData, { headers })
  38. .pipe(
  39. catchError(this.handleError)
  40. );
  41. }
  42. getClaims(): Observable<ClaimRecord[]> {
  43. return this.http.get<ClaimRecord[]>(`${this.apiUrl}/claims`)
  44. .pipe(
  45. catchError(this.handleError)
  46. );
  47. }
  48. private handleError(error: HttpErrorResponse) {
  49. let errorMessage = 'An unknown error occurred!';
  50. if (error.error instanceof ErrorEvent) {
  51. errorMessage = `Error: ${error.error.message}`;
  52. } else {
  53. if (error.status === 504 || error.status === 0) {
  54. errorMessage = 'The request timed out. Please try again later.';
  55. } else if (error.status === 402) {
  56. errorMessage = 'AI extraction credits exhausted. Please contact support.';
  57. } else {
  58. errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
  59. }
  60. }
  61. return throwError(() => new Error(errorMessage));
  62. }
  63. }