11
11
12
12
/* eslint-disable @typescript-eslint/no-floating-promises */
13
13
14
- import {
15
- Authentication ,
16
- GateApiV4Auth ,
17
- HttpBasicAuth ,
18
- HttpBearerAuth ,
19
- OAuth ,
20
- Interceptor ,
21
- ObjectSerializer ,
22
- } from '../model/models' ;
23
- import localVarRequest = require( 'request' ) ;
24
- import http = require( 'http' ) ;
25
- import { HttpError } from './apis' ;
14
+ import { Authentication , GateApiV4Auth , HttpBasicAuth , HttpBearerAuth , OAuth , ObjectSerializer } from '../model/models' ;
15
+ import globalAxios , { AxiosInstance , AxiosRequestConfig , AxiosResponse } from 'axios' ;
26
16
27
17
export class ApiClient {
28
18
protected _basePath = 'https://api.gateio.ws/api/v4' ;
@@ -32,7 +22,9 @@ export class ApiClient {
32
22
apiv4 : new GateApiV4Auth ( ) ,
33
23
} ;
34
24
35
- protected interceptors : Interceptor [ ] = [ ] ;
25
+ constructor ( basePath ?: string , protected axiosInstance : AxiosInstance = globalAxios ) {
26
+ this . _basePath = basePath || this . _basePath ;
27
+ }
36
28
37
29
set basePath ( basePath : string ) {
38
30
this . _basePath = basePath ;
@@ -56,47 +48,31 @@ export class ApiClient {
56
48
auth . secret = secret ;
57
49
}
58
50
59
- public addInterceptor ( interceptor : Interceptor ) {
60
- this . interceptors . push ( interceptor ) ;
61
- }
62
-
63
- public applyToRequest ( options : localVarRequest . Options , authSettings : Array < string > ) {
51
+ public applyToRequest ( config : AxiosRequestConfig , authSettings : Array < string > ) : AxiosRequestConfig {
64
52
for ( const auth of authSettings ) {
65
53
const authenticator = this . authentications [ auth ] ;
66
54
if ( authenticator ) {
67
- authenticator . applyToRequest ( options ) ;
55
+ config = authenticator . applyToRequest ( config ) ;
68
56
}
69
57
}
58
+ return config ;
70
59
}
71
60
72
61
public async request < T > (
73
- options : localVarRequest . Options ,
62
+ config : AxiosRequestConfig ,
74
63
responseType : string ,
75
64
authSettings : Array < string > ,
76
- ) : Promise < { response : http . IncomingMessage ; body : T } > {
77
- const authenticationPromise = Promise . resolve ( ) ;
78
- let interceptorPromise = authenticationPromise . then ( ( ) => this . applyToRequest ( options , authSettings ) ) ;
79
- for ( const interceptor of this . interceptors ) {
80
- interceptorPromise = interceptorPromise . then ( ( ) => interceptor ( options ) ) ;
81
- }
82
-
83
- return interceptorPromise . then ( ( ) => {
84
- return new Promise < { response : http . IncomingMessage ; body : T } > ( ( resolve , reject ) => {
85
- localVarRequest ( options , ( error , response , body ) => {
86
- if ( error ) {
87
- reject ( error ) ;
88
- } else {
89
- if ( response . statusCode && response . statusCode >= 200 && response . statusCode <= 299 ) {
90
- if ( responseType . length > 0 ) {
91
- body = ObjectSerializer . deserialize ( body , responseType ) ;
92
- }
93
- resolve ( { response : response , body : body } ) ;
94
- } else {
95
- reject ( new HttpError ( response , body , response . statusCode ) ) ;
96
- }
65
+ ) : Promise < { response : AxiosResponse ; body : T } > {
66
+ return Promise . resolve ( config )
67
+ . then ( ( c ) => this . applyToRequest ( c , authSettings ) )
68
+ . then ( ( c ) => {
69
+ return this . axiosInstance . request ( c ) . then ( ( rsp ) => {
70
+ let body = rsp . data ;
71
+ if ( responseType . length > 0 ) {
72
+ body = ObjectSerializer . deserialize ( rsp . data , responseType ) ;
97
73
}
74
+ return { response : rsp , body : body } ;
98
75
} ) ;
99
76
} ) ;
100
- } ) ;
101
77
}
102
78
}
0 commit comments