import { addSeconds, format } from 'date-fns'; import axios, { Axios } from 'axios'; import CODE_MESSAGES from './constants/codeMessages'; import IApiPut from './interfaces/IApiPut'; import IApiPost from './interfaces/IApiPost'; import IApiGet from './interfaces/IApiGet'; import { ITokenResponse } from './interfaces/ITokenResponse'; import { ParameterStoreService } from './services/parameterStoreService'; import { AuthService } from './services/authService'; import IApiConnection from './interfaces/IApiConnection'; import TokenRepository from './repository/tokenRepository'; import BusinessError from './exceptions/businessError'; import { log } from 'ic-logs'; import { TIME } from './constants/expirationTime'; export default class CoreCaller { private auth_service: AuthService; private driver: Axios; private base_url: string; private auth_url: string; private parameter_store = new ParameterStoreService(); private token_repository: TokenRepository; constructor(connection_options: IApiConnection, auth_url: string) { this.parameter_store = new ParameterStoreService(); this.base_url = connection_options.base_url; this.auth_url = auth_url; this.token_repository = new TokenRepository(); return this; } public async get(parameters?: IApiGet, axios_service = axios) { const { path } = parameters; try { const token = await this.verifyToken(); this.driver = axios_service.create({ baseURL: this.base_url, headers: { Authorization: `Bearer ${token.access_token}`, RefreshToken: `${token.refresh_token}` }, }); const response = await this.driver.get(`${path}`); log('Get Method Request Status::', response.status); return response; } catch (err) { const error_log = { ...CODE_MESSAGES.FAIL_CALLING_GET_API, content: err, }; throw new BusinessError(error_log); } } public async post(parameters: IApiPost, axios_service = axios) { const { path, body } = parameters; try { const token = await this.verifyToken(); this.driver = axios_service.create({ baseURL: this.base_url, headers: { Authorization: `Bearer ${token.access_token}`, RefreshToken: `${token.refresh_token}` }, }); const response = await this.driver.post(`${path}`, body); log('Post Method Request Status::', response.status); return response; } catch (err) { const error_log = { ...CODE_MESSAGES.FAIL_CALLING_POST_API, content: err, }; throw new BusinessError(error_log); } } public async put(parameters: IApiPut, axios_service = axios) { const { path, body } = parameters; try { const token = await this.verifyToken(); this.driver = axios_service.create({ baseURL: this.base_url, headers: { Authorization: `Bearer ${token.access_token}`, RefreshToken: `${token.refresh_token}` }, }); const response = await this.driver.put(`${path}`, body); log('Put Method Request Status::', response.status); return response; } catch (err) { const error_log = { ...CODE_MESSAGES.FAIL_CALLING_PUT_API, content: err, }; throw new BusinessError(error_log); } } async authenticate(): Promise { const { username, password } = await this.parameter_store.getPartnerParameters(); this.auth_service = new AuthService(this.auth_url); const token_response = await this.auth_service.registerToken({ username, password }); return token_response; } async verifyToken(): Promise { const event_date = format(new Date(), 'yyyy-MM-dd'); let token_validation; try { token_validation = await this.token_repository.getToken(event_date); } catch (err) { log('Error on get token', err); token_validation = false; } if (!token_validation) { const token_response = await this.authenticate(); const ttl_token = Math.floor(addSeconds(new Date(), TIME.EXPIRATION_TIME).getTime() / 1000); const build_token_body = { date: event_date, access_token: token_response.body.access_token, ttl_token, refresh_token: token_response.body.refresh_token, }; await this.token_repository.saveToken(build_token_body); return build_token_body; } return token_validation; } }