import { Pet } from "../model/pet" ; import { REST_API_NAME } from "../config/amplifyConfig" ; import { Auth , API } from "aws-amplify" ; export interface APIService { /**
     * returns all pets that the user is permitted to see
     */ getAllPets ( ) : Promise < Pet [ ] > ; param pet savePet ( pet ) : Promise < void > ; param pet deletePet ( pet ) : Promise < void > ; /**
     * forces sign out globally
     */ forceSignOut ( ) : Promise < void > ; } /**
 * As the name suggests, handles API calls to the Pet service.
 */ export class HttpAPIService implements APIService { constructor ( private api , private auth ) { } /**
     * returns all pets that the user is permitted to see
     */ public async getAllPets ( ) { const authorizationHeader = await this . getAuthorizationHeader ( ) ; return await this . api . get ( REST_API_NAME , '/pets' , { headers : authorizationHeader } ) ; } param pet public async savePet ( pet ) { const authorizationHeader = await this . getAuthorizationHeader ( ) ; if ( pet . id ) { await this . api . put ( REST_API_NAME , `/pets/${ pet . id }` , { body : pet , headers : authorizationHeader } ) ; } else { await this . api . post ( REST_API_NAME , '/pets' , { body : pet , headers : authorizationHeader } ) ; } } param pet public async deletePet ( pet ) { const authorizationHeader = await this . getAuthorizationHeader ( ) ; await this . api . del ( REST_API_NAME , `/pets/${ pet . id }` , { headers : authorizationHeader } ) ; } /**
     * forces sign out globally
     */ public async forceSignOut ( ) { const authorizationHeader = await this . getAuthorizationHeader ( ) ; try { await this . api . post ( 'main' , '/forceSignOut' , { headers : authorizationHeader } ) ; } finally { await this . auth . signOut ( ) ; } } private async getAuthorizationHeader ( ) { const session = await this . auth . currentSession ( ) ; const idToken = session . getIdToken ( ) . getJwtToken ( ) ; return { Authorization : idToken } ; } }