forked from doubtfire-lms/doubtfire-web
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new Numbas Service to the frontend as part of Integration Changed by: Daniel Maddern
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; | ||
import { Observable, throwError } from 'rxjs'; | ||
import { catchError, map, retry } from 'rxjs/operators'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class NumbasService { | ||
private readonly API_URL = 'http://localhost:3000/api/numbas_api'; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
fetchResource(unitId: string, taskId: string, resourcePath: string): Observable<any> { | ||
const resourceUrl = `${this.API_URL}/${unitId}/${taskId}/${resourcePath}`; | ||
const resourceMimeType = this.getMimeType(resourcePath); | ||
|
||
return this.http.get(resourceUrl, { responseType: 'blob' }).pipe( | ||
retry(3), // Retrying up to 3 times before failing | ||
map((blob) => new Blob([blob], { type: resourceMimeType })), | ||
catchError((error: HttpErrorResponse) => { | ||
console.error('Error fetching Numbas resource:', error); | ||
return throwError('Error fetching Numbas resource.'); | ||
}) | ||
); | ||
} | ||
|
||
getMimeType(resourcePath: string): string { | ||
const extension = resourcePath.split('.').pop()?.toLowerCase(); | ||
const mimeTypeMap: { [key: string]: string } = { | ||
'html': 'text/html', | ||
'css': 'text/css', | ||
'js': 'application/javascript', | ||
'json': 'application/json', | ||
'png': 'image/png', | ||
'jpg': 'image/jpeg', | ||
'gif': 'image/gif', | ||
'svg': 'image/svg+xml' | ||
}; | ||
|
||
return mimeTypeMap[extension || ''] || 'text/plain'; | ||
} | ||
uploadTest(unitId: string, taskId: string, file: File): Observable<any> { | ||
const uploadUrl = `${this.API_URL}/uploadNumbasTest`; | ||
const formData = new FormData(); | ||
|
||
formData.append('file', file); | ||
formData.append('unit_code', unitId); | ||
formData.append('task_definition_id', taskId); | ||
|
||
const httpOptions = { | ||
headers: new HttpHeaders({ | ||
// You might need to set some headers here depending on your backend requirements | ||
'Accept': 'application/json' | ||
}) | ||
}; | ||
|
||
return this.http.post(uploadUrl, formData, httpOptions).pipe( | ||
retry(3), | ||
catchError((error: HttpErrorResponse) => { | ||
console.error('Error uploading Numbas test:', error); | ||
return throwError('Error uploading Numbas test.'); | ||
}) | ||
); | ||
} | ||
} |