import {Service} from "./Service"; import axios from "axios" import {ApiCallID} from "../ApiCallID"; /** * Base Construct for every Call */ export abstract class AbstractCall{ //Request Data readonly data : Request; readonly taefikIP = process.env.TRAEFIK_IP || "traefik"; readonly useHttps = process.env.USE_HTTPS ? "https" : "http" || "http"; //HOW TO NOT RESOLVE CIRCULAR DEPENDENCY static MeetznowService ; public constructor(data : Request){ this.data = data; } abstract getService() : Service; abstract getRequestName() : string; /** * Method that send the data and handles the response * @returns {Promise>} */ async get() : Promise> { //HOW TO NOT RESOLVE CIRCULAR DEPENDENCY if(!AbstractCall.MeetznowService) AbstractCall.MeetznowService = require("../src/MeetznowService").MeetznowService; if(!process.env.TRAEFIK_IP) throw Error("No IP for Traefik provided! Please add the env variable TRAEFIK_IP!"); const url = `${this.useHttps}://${process.env.TRAEFIK_IP}/${this.getService()}/${this.getRequestName()}`; console.log(url); const response = await axios.post(url, this.data); if(AbstractCall.MeetznowService.INSTANCE) { AbstractCall.MeetznowService.INSTANCE.getLogger().debug( this.data.id, "[ ---> | " + this.getRequestName() + " ]", { url: response.config.url, status: response.status, request: this.data, response: JSON.stringify(response.data) } ); } if(response.data.error) throw new Error(response.data.error.message); if(response.status !== 200) throw new Error(response.status + " | " + response.statusText); return response.data.valueOf() } } /** * Base Request Model */ export interface Request{ id : ApiCallID payload : RequestPayload } /** * Base Response Model */ export interface Response{ id : ApiCallID successful : boolean error? : { message : string code? : string } payload? : ResponsePayload } export interface BaseRequestPayload{} export interface BaseResponsePayload {}