import { get , post } from "request-promise-native" ; import { LoggingHelper } from "../core/logging-helper" ; const HttpsProxyAgent = require ( "https-proxy-agent" ) ; const Logger = "SPOKES-CLIENT" ; export class SpokesClient { private _secretKey : string ; private _id : string ; public constructor ( private id , private secretKey ) { this . _secretKey = secretKey ; this . _id = id ; } public async verifyUUIDisNew ( ) { const options = { uri : `https://api.bespoken.link/pipe/${ this . _secretKey }` , headers : { "x-access-token" : "4772616365-46696f72656c6c61" , } , body : { } , json : true , timeout : 30000 } ; const proxy = process . env . HTTPS_PROXY ; if ( proxy ) { options . agent = new HttpsProxyAgent ( proxy ) ; } try { await get ( options ) ; } catch ( error ) { if ( error . statusCode && error . statusCode !== 404 ) { LoggingHelper . error ( Logger , `Error while verifying id: ${ error . message }` ) ; throw error ; } return true ; } return false ; } public async createPipe ( ) { const options = { uri : "https://api.bespoken.link/pipe" , headers : { "x-access-token" : "4772616365-46696f72656c6c61" , } , body : { uuid : this . _secretKey , diagnosticsKey : null , endPoint : { name : this . _id , } , http : { url : "https://proxy.bespoken.tools" , } , path : "/" , pipeType : "HTTP" , proxy : true , } , json : true , timeout : 30000 } ; const proxy = process . env . HTTPS_PROXY ; if ( proxy ) { options . agent = new HttpsProxyAgent ( proxy ) ; } const response = await post ( options ) ; response . endPoint = { name : this . _id , } ; return response ; } }