import axios , { Method } from 'axios' ; import { setServer , getServer } from '../store/connectivity/server' ; import { store } from '../store/store' ; class RestClient { private axios ; private serverEndpoint ; constructor ( serverEndpoint ) { this . axios = axios ; this . serverEndpoint = serverEndpoint ; } setEndpoint ( url ) { this . serverEndpoint = url ; } getEndpoint ( ) { return this . serverEndpoint ; } get ( url ) { return this . axios . get ( this . serverEndpoint + url ) ; } post ( url , payload ) { return this . axios . post ( this . serverEndpoint + url , payload ) ; } query ( gql ) { console . log ( this . serverEndpoint ) ; if ( this . serverEndpoint === "" ) { this . setEndpoint ( getServer ( store . getState ( ) ) ) ; } console . log ( getServer ( store . getState ( ) ) ) ; return this . axios ( { method : 'post' , url : this . serverEndpoint + "/api/query" , data : { query : gql } } ) ; } infer ( method , url , payload ) { const options = { method : method , url : this . serverEndpoint + url , data : payload } ; return this . axios . request ( options ) ; } ws_log ( logid , onEvent ) { const wsEndpoint = this . serverEndpoint . replace ( "http" , "ws" ) ; console . log ( wsEndpoint + "/api/logs/" + logid ) ; const socket = new WebSocket ( wsEndpoint + "/api/logs/" + logid ) ; socket . addEventListener ( 'message' , function ( event ) { onEvent ( event . data ) ; } ) ; } } let restclient = new RestClient ( "" ) ; function setServerEndpoint ( endpoint ) { restclient . setEndpoint ( endpoint ) ; store . dispatch ( setServer ( endpoint ) ) ; } function getServerEndpoint ( ) { return store . getState ( ) . connectivity . server ; } function initServerEndpoint ( ) { if ( store . getState ( ) . connectivity . server ) { let serverEndpoint = store . getState ( ) . connectivity . server ; restclient . setEndpoint ( serverEndpoint ) ; } } export { restclient , setServerEndpoint , getServerEndpoint , initServerEndpoint } ;