1- import axios from 'axios'
2- import quidException from "./exceptions"
1+ import axios from 'axios' ;
2+ import quidException from "./exceptions" ;
33
44export default class QuidRequests {
55 refreshToken = null ;
66 #accessToken = null ;
77
8- constructor ( { namespace, axiosConfig, timeouts = {
8+ constructor ( { quidUri , namespace, axiosConfig, timeouts = {
99 accessToken : "20m" ,
1010 refreshToken : "24h"
1111 } ,
1212 accessTokenUri = null ,
1313 verbose = false } ) {
14+ if ( typeof quidUri !== 'string' ) {
15+ throw quidException ( { error : 'Parameter quidUri has to be set' } ) ;
16+ }
1417 if ( typeof namespace !== 'string' ) {
1518 throw quidException ( { error : 'Parameter namespace has to be set' } ) ;
1619 }
1720 if ( typeof axiosConfig !== 'object' ) {
1821 throw quidException ( { error : 'Parameter axiosConfig has to be set' } ) ;
1922 }
20- this . namespace = namespace
23+ this . quidUri = quidUri ;
24+ this . namespace = namespace ;
2125 this . axiosConfig = axiosConfig ;
2226 this . axios = axios . create ( this . axiosConfig ) ;
23- this . timeouts = timeouts
24- this . verbose = verbose
25- this . accessTokenUri = accessTokenUri
27+ this . axiosQuid = axios . create (
28+ {
29+ baseURL : quidUri ,
30+ timeout : 5000 ,
31+ headers : { 'Content-Type' : 'application/json' }
32+ }
33+ ) ;
34+ this . timeouts = timeouts ;
35+ this . verbose = verbose ;
36+ this . accessTokenUri = accessTokenUri ;
2637 }
2738
2839 async get ( uri ) {
29- return await this . _requestWithRetry ( uri , "get" )
40+ return await this . _requestWithRetry ( uri , "get" ) ;
3041 }
3142
3243 async post ( uri , payload ) {
33- return await this . _requestWithRetry ( uri , "post" , payload )
44+ return await this . _requestWithRetry ( uri , "post" , payload ) ;
3445 }
3546
36- async adminLogin ( username , password ) {
37- let uri = "/admin_login" ;
47+ async getRefreshToken ( { namespace , username, password, refreshTokenTtl = "24h" } ) {
48+ let uri = "/token/refresh/" + refreshTokenTtl ;
3849 let payload = {
39- namespace : "quid" ,
50+ namespace : namespace ,
4051 username : username ,
4152 password : password ,
4253 }
4354 try {
44- let response = await axios . post ( uri , payload , this . axiosConfig ) ;
55+ let response = await this . axiosQuid . post ( uri , payload ) ;
4556 this . refreshToken = response . data . token ;
4657 } catch ( e ) {
4758 if ( e . response ) {
@@ -60,9 +71,11 @@ export default class QuidRequests {
6071 await this . checkTokens ( ) ;
6172 try {
6273 if ( method === "get" ) {
63- return await this . axios . get ( uri , this . axiosConfig ) ;
74+ console . log ( "GET" , this . #accessToken, uri ) ;
75+ return await this . axios . get ( uri ) ;
6476 } else {
65- return await axios . post ( uri , payload , this . axiosConfig ) ;
77+ //console.log("POST REQ", uri, payload)
78+ return await axios . post ( uri , payload ) ;
6679 }
6780 } catch ( e ) {
6881 if ( e . response ) {
@@ -130,7 +143,7 @@ export default class QuidRequests {
130143 if ( this . verbose ) {
131144 console . log ( "Getting an access token from" , url , payload )
132145 }
133- let response = await axios . post ( url , payload , this . axiosConfig ) ;
146+ let response = await this . axiosQuid . post ( url , payload ) ;
134147 return { token : response . data . token , error : null , statusCode : response . status } ;
135148 } catch ( e ) {
136149 if ( e . response !== undefined ) {
0 commit comments