import { Data } from '../Data/Data' ; import { Logger } from '../../logger' ; import { TMethod , IMapRequest , IMapResponse } from './ApiClient.types' ; export class ApiClient { private readonly data : Data ; private readonly logger : typeof Logger ; constructor ( data = new Data ( ) , logger = Logger ) { this . data = data ; this . logger = logger ; } public checkDevice ( options ) { return this . createRequest ( 'checkDevice' , options ) ; } public getConfig ( options ) { return this . createRequest ( 'getConfig' , options ) ; } public applicationOpen ( options ) { return this . createRequest ( 'applicationOpen' , options ) ; } public registerDevice ( options ) { return this . createRequest ( 'registerDevice' , options ) ; } public unregisterDevice ( options ) { return this . createRequest ( 'unregisterDevice' , options ) ; } public deleteDevice ( options ) { return this . createRequest ( 'deleteDevice' , options ) ; } public messageDeliveryEvent ( options ) { return this . createRequest ( 'messageDeliveryEvent' , options ) ; } public pushStat ( options ) { return this . createRequest ( 'pushStat' , options ) ; } public setTags ( options ) { return this . createRequest ( 'setTags' , options ) ; } public getTags ( options ) { return this . createRequest ( 'getTags' , options ) ; } public registerUser ( options ) { return this . createRequest ( 'registerUser' , options ) ; } public postEvent ( options ) { return this . createRequest ( 'postEvent' , options ) ; } public getInboxMessages ( options ) { return this . createRequest ( 'getInboxMessages' , options ) ; } public inboxStatus ( options ) { return this . createRequest ( 'inboxStatus' , options ) ; } public pageVisit ( options , url ) { return this . createRequest ( 'pageVisit' , options , url ) ; } public getInApps ( options ) { return this . createRequest ( 'getInApps' , options ) ; } public setPurchase ( options ) { return this . createRequest ( 'setPurchase' , options ) ; } private async createRequest < T extends TMethod > ( methodName , request , customUrl ? ) { const entrypoint = await this . data . getApiEntrypoint ( ) ; const url = customUrl || entrypoint + methodName ; const response = await fetch ( url , { method : 'POST' , headers : { 'Content-Type' : 'text/plain;charset=UTF-8' } , body : JSON . stringify ( { request } ) } ) ; const result = await this . checkResponse ( response ) ; if ( result . base_url ) { await this . data . setApiEntrypoint ( result . base_url ) ; } await this . logger . write ( 'apirequest' , `${ methodName } call with arguments: ${ JSON . stringify ( request ) } to Pushwoosh has been successful. Result: ${ JSON . stringify ( result . response ) }` ) ; return result . response as IMapResponse [ T ] ; } private async checkResponse ( response ) { if ( response . status !== 200 ) { throw new Error ( `Error code: ${ response . status }. Error text: ${ response . statusText }` ) ; } const data = await response . json ( ) ; if ( data . status_code !== 200 ) { throw new Error ( `Error code: ${ data . status_code }. Error text: ${ data . status_message }` ) ; } return data ; } }