11/*
2- * Copyright 2020 G42 Technologies Co.,Ltd.
2+ * Copyright 2023 G42 Technologies Co.,Ltd.
33 *
44 * Licensed to the Apache Software Foundation (ASF) under one
55 * or more contributor license agreements. See the NOTICE file
2222import { HcClient } from "./HcClient" ;
2323import { ICredential } from "./auth/ICredential" ;
2424import { ClientOptions , DefaultHttpClient } from "./http/DefaultHttpClient" ;
25- import { RequiredError } from "./auth/AKSKSigner" ;
2625import { BasicCredentials } from "./auth/BasicCredentials" ;
2726import { GlobalCredentials } from "./auth/GlobalCredentials" ;
2827import { SdkException } from "./exception/SdkException" ;
2928import { Region } from "./region/region" ;
3029import { UserOptions } from "./UserOptions" ;
31- const path = require ( ' path' ) ;
30+ import * as path from " path" ;
3231
3332interface CredParams {
3433 ak ?: string ;
3534 sk ?: string ,
3635 project_id ?: string ,
3736 domain_id ?: string ,
37+ [ key : string ] : any ;
3838}
39+
3940export class ClientBuilder < T > {
4041 private init : Function ;
41- private endpoint ?: string ;
42+ private endpoints ?: string [ ] ;
4243 private credential ?: ICredential ;
4344 private proxyAgent ?: string ;
4445 private credentialType : string [ ] = [ "BasicCredentials" , "GlobalCredentials" ] ;
4546 private envParams : CredParams = process . env ;
4647 private region ?: Region ;
4748 private userOptions ?: UserOptions ;
49+ private credentials : { [ key : string ] : ICredential } = { } ;
4850
4951 public constructor ( init : ( hcClient : HcClient ) => T , credentialType ?: string ) {
5052 this . init = init ;
@@ -53,8 +55,13 @@ export class ClientBuilder<T> {
5355 }
5456 }
5557
56- public withEndpoint ( endpoint : string ) : ClientBuilder < T > {
57- this . endpoint = endpoint ;
58+ public withEndpoint ( endpoint : string | string [ ] ) : ClientBuilder < T > {
59+ this . endpoints = [ ] ;
60+ if ( typeof endpoint === 'string' ) {
61+ this . endpoints . push ( endpoint ) ;
62+ } else if ( Array . isArray ( endpoint ) ) {
63+ this . endpoints . push ( ...endpoint ) ;
64+ }
5865 return this ;
5966 }
6067
@@ -102,85 +109,75 @@ export class ClientBuilder<T> {
102109 throw new SdkException ( `credential can not be null, ${ this . credentialType } Credential objects are required` ) ;
103110 }
104111
105- const client = new DefaultHttpClient ( axiosOptions ) ;
106- const hcClient = new HcClient ( client ) ;
107- hcClient . withEndpoint ( this . endpoint ) . withCredential ( this . credential ) ;
108112 if ( this . region ) {
109- hcClient . withRegion ( this . region ) ;
113+ this . endpoints = this . region . endpoints ;
110114 }
115+ const client = new DefaultHttpClient ( axiosOptions , this . endpoints ) ;
116+ const hcClient = new HcClient ( client ) ;
117+
118+ this . region && hcClient . withRegion ( this . region ) ;
119+
120+ hcClient . withCredential ( this . credential ) . withEndpoints ( this . endpoints ) ;
111121 return this . init ( hcClient ) ;
112122 }
113123
114124 /**
115- * 从环境变量获取 HUAWEICLOUD_SDK_TYPE
116- * 环境变量里没有则使用 credentialType[0]
117- * 生成credential实体
118- * 从环境变量获取 AK SK projectId/domainId 进行赋值, 如果环境变量是GlobalCredentials,则赋值domainId
119- * @returns Credentials
125+ * Get 'HUAWEICLOUD_SDK_TYPE' from environment variables
126+ * If the variable does not exist, use the first credential type
127+ * Generate credential entity
128+ * Assign AK, SK, projectId/domainId from the environment variables
129+ * If the environment variable is 'GlobalCredentials', assign the domainId
130+ * @returns ICredential
120131 */
121- public getCredentialFromEnvironment ( ) : ICredential {
132+ private getCredentialFromEnvironment ( ) : ICredential {
122133 const sdkType : any = process . env . HUAWEICLOUD_SDK_TYPE ;
123134 const credentialTYPE = this . whichCredential ( sdkType )
124135 return this . getInputParamCredential ( credentialTYPE , this . envParams ) ;
125136 }
126137
127- public whichCredential ( sdkType : string ) {
128- let credentialTYPE ;
129- if ( sdkType ) {
130- switch ( sdkType ) {
131- case 'BasicCredentials' :
132- credentialTYPE = new BasicCredentials ( ) ;
133- break ;
134- case 'GlobalCredentials' :
135- credentialTYPE = new GlobalCredentials ( ) ;
136- break ;
137- default :
138- const obj = { } ;
139- const definedCredPath = path . join ( this . init ( ) . getPath ( ) , `${ sdkType } ` ) ;
140- if ( ! obj [ sdkType ] ) {
141- credentialTYPE = require ( definedCredPath ) ;
142- // 多加一层
143- obj [ sdkType ] = credentialTYPE [ sdkType ] ;
144- }
145- credentialTYPE = new obj [ sdkType ] ( ) ;
146- break ;
147- }
148- } else {
149- // 默认是basic
150- credentialTYPE = new BasicCredentials ( ) ;
138+ private whichCredential ( sdkType : string ) {
139+ if ( ! sdkType ) {
140+ return new BasicCredentials ( ) ;
141+ }
142+
143+ switch ( sdkType ) {
144+ case 'BasicCredentials' :
145+ return new BasicCredentials ( ) ;
146+ case 'GlobalCredentials' :
147+ return new GlobalCredentials ( ) ;
148+ default :
149+ if ( this . credentials [ sdkType ] ) {
150+ return this . credentials [ sdkType ] ;
151+ }
152+
153+ const definedCredPath = path . join ( this . init ( ) . getPath ( ) , `${ sdkType } ` ) ;
154+ const credentialTYPE = require ( definedCredPath ) ;
155+ this . credentials [ sdkType ] = new credentialTYPE ( ) ;
156+ return this . credentials [ sdkType ] ;
151157 }
152- return credentialTYPE ;
153158 }
154159
155- public getInputParamCredential ( CredentialsType : any , credential : CredParams ) {
156- // 判断是否有_
157- let hash = { } ;
158- for ( let key in credential ) {
159- if ( key . indexOf ( "HUAWEICLOUD_SDK_" ) == 0 ) {
160- const value = credential [ key ]
161- key = key . substring ( 16 ) ;
162- if ( key . indexOf ( '_' ) == - 1 ) {
163- key = key . toLowerCase ( ) ;
164- key = 'with' + key . charAt ( 0 ) . toUpperCase ( ) + key . slice ( 1 ) ;
160+ private getInputParamCredential ( CredentialsType : any , credential : CredParams ) {
161+ const hash = Object . keys ( credential )
162+ . filter ( key => key . indexOf ( "HUAWEICLOUD_SDK_" ) === 0 )
163+ . reduce ( ( acc : { [ key : string ] : string } , key ) => {
164+ let newKey = key . substring ( 16 ) ;
165+ if ( newKey . indexOf ( '_' ) === - 1 ) {
166+ newKey = `with${ newKey . toLowerCase ( ) . replace ( / ^ \w / , c => c . toUpperCase ( ) ) } ` ;
165167 } else {
166- const arr = key . split ( '_' ) . map ( item => {
167- item = item . toLowerCase ( ) ;
168- return item . charAt ( 0 ) . toUpperCase ( ) + item . slice ( 1 ) ;
169- } )
170- if ( Array . isArray ( arr ) ) {
171- key = 'with' + arr . join ( "" ) ;
172- }
168+ newKey = newKey . split ( '_' ) . map ( word => `${ word . toLowerCase ( ) . replace ( / ^ \w / , c => c . toUpperCase ( ) ) } ` ) . join ( '' ) ;
169+ newKey = `with${ newKey } ` ;
173170 }
174- hash [ key ] = value
175- }
176- }
177- credential = hash ;
178- for ( const key in credential ) {
171+ acc [ newKey ] = credential [ key ] ;
172+ return acc ;
173+ } , { } ) ;
174+
175+ Object . keys ( hash ) . forEach ( ( key : string ) => {
179176 if ( CredentialsType [ key ] ) {
180- CredentialsType [ key ] ( credential [ key ] ) ;
177+ CredentialsType [ key ] ( hash [ key ] ) ;
181178 }
182- }
179+ } ) ;
180+
183181 return CredentialsType ;
184182 }
185-
186183}
0 commit comments