import { DyFM_Error, DyFM_HttpCallType, DyFM_Log, } from '@futdevpro/fsm-dynamo'; import { DyNTS_AuthService } from '../core/auth.service'; import { DyNTS_SingletonService } from './singleton.service'; import { DyNTS_ApiService } from '../core/api.service'; import { DyNTS_ApiCall_Params, DyNTS_global_settings } from '../..'; export abstract class DyNTS_ApiService_Base extends DyNTS_SingletonService { /* protected abstract readonly auth_CS: DyNTS_AuthService */ /* abstract get token(): string; */ /* abstract get env(): { baseUrl: string, test: string }; */ abstract get baseUrl(): string; abstract get testEndpoint(): string; abstract get connectingSystemName(): string; async runApiCallWithAvailabilityCheck( apiCall: () => Promise, issuer: string ): Promise { try { return await apiCall(); } catch (error) { await this.checkServerAvailability(issuer); throw error; } } async checkServerAvailability(issuer: string): Promise { try { await this.testGet(issuer); } catch (error) { throw new DyFM_Error({ ...this.getDefaultErrorSettings( 'checkServerAvailability', new Error( `${this.connectingSystemName} server is not available` + `\n baseUrl: ${this.baseUrl}` ), issuer ), userMessage: `${this.connectingSystemName} server is not available`, errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-ASB-CSA0`, }); } } async testGet(issuer: string): Promise { if (!this.testEndpoint) { throw new DyFM_Error({ ...this.getDefaultErrorSettings( 'testGet', new Error(`testEndpoint is not set for ${this.connectingSystemName}`), issuer ), }); } await DyNTS_ApiService.startApiCall( new DyNTS_ApiCall_Params({ name: `${this.connectingSystemName} testGet`, type: DyFM_HttpCallType.get, baseUrl: this.baseUrl, endpoint: this.testEndpoint, }), ); DyFM_Log.success(`${this.connectingSystemName} is available`); } }