1111
1212/* eslint-disable @typescript-eslint/no-floating-promises */
1313
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' ;
2616
2717export class ApiClient {
2818 protected _basePath = 'https://api.gateio.ws/api/v4' ;
@@ -32,7 +22,9 @@ export class ApiClient {
3222 apiv4 : new GateApiV4Auth ( ) ,
3323 } ;
3424
35- protected interceptors : Interceptor [ ] = [ ] ;
25+ constructor ( basePath ?: string , protected axiosInstance : AxiosInstance = globalAxios ) {
26+ this . _basePath = basePath || this . _basePath ;
27+ }
3628
3729 set basePath ( basePath : string ) {
3830 this . _basePath = basePath ;
@@ -56,47 +48,31 @@ export class ApiClient {
5648 auth . secret = secret ;
5749 }
5850
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 {
6452 for ( const auth of authSettings ) {
6553 const authenticator = this . authentications [ auth ] ;
6654 if ( authenticator ) {
67- authenticator . applyToRequest ( options ) ;
55+ config = authenticator . applyToRequest ( config ) ;
6856 }
6957 }
58+ return config ;
7059 }
7160
7261 public async request < T > (
73- options : localVarRequest . Options ,
62+ config : AxiosRequestConfig ,
7463 responseType : string ,
7564 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 ) ;
9773 }
74+ return { response : rsp , body : body } ;
9875 } ) ;
9976 } ) ;
100- } ) ;
10177 }
10278}
0 commit comments