clean FastenAPI service.

This commit is contained in:
Jason Kulatunga 2022-10-08 20:44:20 -07:00
parent c209ce681c
commit cbac02415b
1 changed files with 5 additions and 115 deletions

View File

@ -1,12 +1,8 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import { Router } from '@angular/router';
import {map} from 'rxjs/operators'; import {map} from 'rxjs/operators';
import {ResponseWrapper} from '../models/response-wrapper'; import {ResponseWrapper} from '../models/response-wrapper';
import {Source} from '../models/fasten/source';
import {User} from '../models/fasten/user';
import {ResourceFhir} from '../models/fasten/resource_fhir';
import {MetadataSource} from '../models/fasten/metadata-source'; import {MetadataSource} from '../models/fasten/metadata-source';
@Injectable({ @Injectable({
@ -14,121 +10,15 @@ import {MetadataSource} from '../models/fasten/metadata-source';
}) })
export class FastenApiService { export class FastenApiService {
AUTH_TOKEN_KEY = 'token'; constructor(private _httpClient: HttpClient) {
constructor(private _httpClient: HttpClient, private router: Router) {
} }
getBasePath(): string { private getBasePath(): string {
return window.location.pathname.split('/web').slice(0, 1)[0]; return window.location.pathname.split('/web').slice(0, 1)[0];
} }
// Auth functions public GetMetadataSources(): Observable<{[name: string]: MetadataSource}> {
token() { return this._httpClient.get<ResponseWrapper>(`${this.getBasePath()}/api/metadata/source`)
return localStorage.getItem(this.AUTH_TOKEN_KEY);
}
isAuthenticated() {
return !!localStorage.getItem(this.AUTH_TOKEN_KEY);
}
logout() {
localStorage.removeItem(this.AUTH_TOKEN_KEY);
}
signup(newUser: User): Observable<any> {
return this._httpClient.post<any>(`${this.getBasePath()}/api/auth/signup`, newUser).pipe(
map((res: any) => {
if(res.success){
localStorage.setItem(this.AUTH_TOKEN_KEY, res.data);
return res.data
} else {
throw new Error(res.error)
}
}
));
}
signin(username: string, pass: string): Observable<any> {
const data = {
username: username,
password: pass
};
return this._httpClient.post<any>(`${this.getBasePath()}/api/auth/signin`, data).pipe(
map((res: any) => {
if(res.success){
localStorage.setItem(this.AUTH_TOKEN_KEY, res.data);
return res.data
} else {
throw new Error(res.error)
}
}
));
}
/*
SECURE ENDPOINTS
*/
createManualSource(file: File): Observable<Source> {
const formData = new FormData();
formData.append('file', file);
return this._httpClient.post<any>(`${this.getBasePath()}/api/secure/source/manual`, formData)
.pipe(
map((response: ResponseWrapper) => {
console.log("MANUAL SOURCE RESPONSE", response)
return response.data as Source
})
);
}
syncSource(sourceId: string): Observable<any> {
return this._httpClient.post<any>(`${this.getBasePath()}/api/secure/source/${sourceId}/sync`, {})
.pipe(
map((response: ResponseWrapper) => {
console.log("SOURCE RESPONSE", response)
return response.data
})
);
}
getResources(sourceResourceType?: string, sourceID?: string): Observable<ResourceFhir[]> {
let queryParams = {}
if(sourceResourceType){
queryParams["sourceResourceType"] = sourceResourceType
}
if(sourceID){
queryParams["sourceID"] = sourceID
}
return this._httpClient.get<any>(`${this.getBasePath()}/api/secure/resource/fhir`, {params: queryParams})
.pipe(
map((response: ResponseWrapper) => {
console.log("RESPONSE", response)
return response.data as ResourceFhir[]
})
);
}
getResourceBySourceId(sourceId: string, resourceId: string): Observable<ResourceFhir> {
return this._httpClient.get<any>(`${this.getBasePath()}/api/secure/resource/fhir/${sourceId}/${resourceId}`)
.pipe(
map((response: ResponseWrapper) => {
console.log("RESPONSE", response)
return response.data as ResourceFhir
})
);
}
getMetadataSources(): Observable<{[name: string]: MetadataSource}> {
return this._httpClient.get<any>(`${this.getBasePath()}/api/metadata/source`)
.pipe( .pipe(
map((response: ResponseWrapper) => { map((response: ResponseWrapper) => {
console.log("Metadata RESPONSE", response) console.log("Metadata RESPONSE", response)