import { HttpClient , HttpHeaders } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; @ Injectable ( ) export class ApiRunner { constructor ( private readonly http ) { } public async callPostApi < T > ( url , input , options ? ) { return new Promise < T > ( ( resolve , reject ) => { let headers = new HttpHeaders ( { 'Content-Type' : options ?. contentType ?? 'application/json' , } ) ; if ( options ?. bearerToken ) { headers = headers . set ( 'Authorization' , `Bearer ${ options . bearerToken }` ) ; console . debug ( 'set authorization' , headers , options . bearerToken ) ; } this . http . post ( url , input , { headers : headers } ) . subscribe ( ( result ) => { console . log ( 'retrieved POST call' , url ) ; resolve ( result ) ; } , ( error ) => { console . error ( 'Could not execute POST call' , url , input , error ) ; resolve ( null ) ; } ) ; } ) ; } public async callGetApi < T > ( url ) { console . debug ( 'calling GET call' , url ) ; return new Promise < T > ( ( resolve , reject ) => { this . http . get ( url ) . subscribe ( ( result ) => { console . log ( 'retrieved GET call' , url ) ; resolve ( result ) ; } , ( error ) => { if ( ! url . includes ( '.json' ) ) { console . error ( 'Could not execute GET call' , url , error ) ; } resolve ( null ) ; } ) ; } ) ; } public async get ( url ) { console . debug ( 'calling GET' , url ) ; return this . http . get ( url , { responseType : 'text' , } ) . toPromise ( ) ; } }