Skip to content

Commit 5fb60a5

Browse files
committed
release 0.0.4-beta source code for nodejs
1 parent 98518d6 commit 5fb60a5

File tree

12 files changed

+107
-22
lines changed

12 files changed

+107
-22
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# 0.0.4-beta # 0.0.4-beta 2023-02-20
2+
3+
### G42Cloud SDK IMS
4+
5+
- _Features_
6+
- None
7+
- _Bug Fix_
8+
- None
9+
- _Change_
10+
- Add the enum values `IsoImage` to the request parameter `type` to the interface `CreateImage`
11+
112
# 0.0.3-beta 2023-01-06
213

314
### G42Cloud SDK IMS

core/ClientBuilder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ export class ClientBuilder<T> {
9090
axiosOptions.headers["User-Agent"] = this.userOptions.customUserAgent;
9191
}
9292

93+
if (this.userOptions?.axiosRequestConfig) {
94+
axiosOptions.axiosRequestConfig = this.userOptions.axiosRequestConfig;
95+
}
96+
9397
if (!this.credential) {
9498
this.credential = this.getCredentialFromEnvironment();
9599
}

core/HcClient.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { ExceptionUtil } from "./exception/ExceptionUtil";
2828
import { getLogger, Logger, LogLevel } from './logger';
2929
import { DefaultHttpResponse } from "./http/DefaultHttpResponse";
3030
import { Region } from "./region/region";
31+
import { SdkStreamResponse } from "./SdkStreamResponse";
3132

3233
export class HcClient {
3334
private httpClient: HttpClient;
@@ -126,21 +127,30 @@ export class HcClient {
126127
httpRequest['responseHeaders'] = options['responseHeaders'];
127128
}
128129
httpRequest.proxy = this.proxyAgent;
130+
if(options.axiosRequestConfig){
131+
httpRequest.axiosRequestConfig= options.axiosRequestConfig;
132+
}
133+
129134
return httpRequest;
130135
}
131136

132137
private extractResponse<T extends SdkResponse>(result: DefaultHttpResponse<T>): T {
133138
const headers = result.headers;
134139
let contentType = headers['content-type'];
135-
contentType = contentType.toLowerCase();
136-
if (contentType && (contentType.startsWith('application/octet-stream') || contentType.startsWith("image"))) {
137-
return result.data as T;
140+
contentType = contentType?.toLowerCase();
141+
if (contentType
142+
&& (contentType.startsWith('application/octet-stream')
143+
|| contentType.startsWith("image")
144+
|| contentType.startsWith("application/zip"))) {
145+
const streamRes = new SdkStreamResponse();
146+
streamRes.body = result.data;
147+
streamRes.httpStatusCode = result.statusCode;
148+
return streamRes as T;
138149
} else {
139-
let response = result.data instanceof Object ? result.data : {} as T;
140-
let sdkRespone = response as SdkResponse;
150+
const response = result.data instanceof Object ? result.data : {} as T;
151+
const sdkRespone = response as SdkResponse;
141152
sdkRespone.httpStatusCode = result.statusCode;
142-
143-
return response;
153+
return sdkRespone as T;
144154
}
145155
}
146156
}

core/SdkStreamResponse.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2023 G42 Technologies Co.,Ltd.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
export class SdkStreamResponse {
23+
httpStatusCode?: number;
24+
body?: any;
25+
}

core/UserOptions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { AxiosRequestConfig } from "axios";
2+
13
export class UserOptions {
24
customUserAgent?: string;
5+
axiosRequestConfig?: AxiosRequestConfig;
36
}

core/auth/AKSKSigner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class AKSKSigner {
7979
let allHeaders = {};
8080
let current_headers:any = {};
8181
Object.assign(current_headers, request.headers);
82-
if (current_headers['content-type'].indexOf('multipart/form-data') !== -1) {
82+
if (current_headers['content-type']?.indexOf('multipart/form-data') !== -1) {
8383
delete current_headers['content-type'];
8484
}
8585
Object.assign(allHeaders, current_headers, authenticationHeaders);

core/http/DefaultHttpClient.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* under the License.
2020
*/
2121

22-
import axios, { AxiosResponse, AxiosError } from 'axios';
22+
import axios, { AxiosResponse, AxiosError, AxiosRequestConfig } from 'axios';
2323
import { IHttpRequest } from './IHttpRequest';
2424
import { stringify as qsStringify } from 'querystring';
2525
import { HttpClient } from './HttpClient';
@@ -66,8 +66,7 @@ export class DefaultHttpClient implements HttpClient {
6666
if (this.defaultOption.proxy && this.defaultOption.proxy !== '') {
6767
proxyAgent = HttpsProxyAgent(this.defaultOption.proxy);
6868
}
69-
70-
this.axiosInstance = axios.create({
69+
const axiosRequestConfig: AxiosRequestConfig = {
7170
maxContentLength: Infinity,
7271
headers: Object.assign(
7372
this.DEFAULT_HEADERS,
@@ -76,7 +75,13 @@ export class DefaultHttpClient implements HttpClient {
7675
proxy: false,
7776
httpAgent: proxyAgent,
7877
httpsAgent: proxyAgent
79-
});
78+
};
79+
80+
if (this.defaultOption.axiosRequestConfig) {
81+
Object.assign(axiosRequestConfig, this.defaultOption.axiosRequestConfig);
82+
}
83+
84+
this.axiosInstance = axios.create(axiosRequestConfig);
8085

8186
this.axiosInstance.interceptors.request.use((request: any) => {
8287
const { url, method, data, headers } = request;
@@ -118,7 +123,7 @@ export class DefaultHttpClient implements HttpClient {
118123
const result = this._convertResponse<T>(httpRequest, res);
119124

120125
return {
121-
data: result instanceof String ? undefined : result,
126+
data: result,
122127
statusCode: res.status,
123128
headers: res.headers
124129
};
@@ -143,7 +148,7 @@ export class DefaultHttpClient implements HttpClient {
143148
if (this.defaultOption.headers) {
144149
const customUserAgent = this.defaultOption.headers['User-Agent'];
145150
if (customUserAgent) {
146-
headers['User-Agent'] = ["huaweicloud-usdk-nodejs/3.0",customUserAgent].join(" ");
151+
headers['User-Agent'] = ["huaweicloud-usdk-nodejs/3.0", customUserAgent].join(" ");
147152
} else {
148153
headers['User-Agent'] = "huaweicloud-usdk-nodejs/3.0"
149154
}
@@ -159,6 +164,11 @@ export class DefaultHttpClient implements HttpClient {
159164
return qsStringify(params);
160165
},
161166
};
167+
168+
if (httpRequest.axiosRequestConfig) {
169+
Object.assign(requestParams, httpRequest.axiosRequestConfig);
170+
}
171+
162172
const methods: string[] = ['PUT', 'POST', 'PATCH', 'DELETE'];
163173
if (method && methods.indexOf(method.toUpperCase()) !== -1) {
164174
requestParams = Object.assign(requestParams, {
@@ -204,7 +214,7 @@ export class DefaultHttpClient implements HttpClient {
204214
data: error.response ? error.response.data : undefined,
205215
status: error.response ? error.response.status : undefined,
206216
headers: error.response ? error.response.headers : undefined,
207-
message: error.message || undefined,
217+
message: error.message || undefined,
208218
requestId: error.response?.headers['x-request-id']
209219
}
210220
return transformedResponse;
@@ -223,4 +233,5 @@ export interface ClientOptions {
223233
headers?: any,
224234
logger?: Logger,
225235
logLevel?: LogLevel;
236+
axiosRequestConfig?: AxiosRequestConfig
226237
}

core/http/IHttpRequest.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* under the License.
2020
*/
2121

22+
import { AxiosRequestConfig } from "axios";
23+
2224

2325
export interface IHttpRequest {
2426
params?: any;
@@ -33,9 +35,11 @@ export interface IHttpRequest {
3335

3436
getPathParams?: () => any;
3537
responseHeaders?: any;
38+
axiosRequestConfig?: AxiosRequestConfig;
3639
}
3740

3841
export class HttpRequestImpl implements IHttpRequest {
42+
axiosRequestConfig?: AxiosRequestConfig;
3943
public params: any;
4044
public endpoint: string | undefined;
4145
public contentType: string | undefined;

core/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"@types/lodash": "^4.14.172",
3737
"@types/mocha": "^8.2.2",
3838
"@types/node": "^14.14.37",
39-
"@types/sinon": "^10.0.2",
39+
"@types/sinon": "^10.0.13",
4040
"busboy": "^0.3.1",
4141
"chai": "^4.3.4",
4242
"chai-like": "^1.1.1",
@@ -47,8 +47,8 @@
4747
"nock": "^13.1.0",
4848
"nyc": "^15.1.0",
4949
"shx": "^0.3.2",
50-
"sinon": "^11.1.2",
51-
"ts-node": "^9.1.1",
50+
"sinon": "^15.0.1",
51+
"ts-node": "^10.9.1",
5252
"tslint": "^6.1.3",
5353
"typescript": "^4.1.0"
5454
}

core/tests/HttpClient.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { GlobalCredentials } from "../auth/GlobalCredentials";
77
import { HcClient } from "../HcClient";
88
import { DefaultHttpClient } from "../http/DefaultHttpClient";
99
import { Region } from "../region/region";
10+
import { SdkResponse } from "../SdkResponse";
1011

1112
const regions =
1213
{
@@ -119,7 +120,7 @@ describe('httpclient tests', () => {
119120
after(() => {
120121
nock.cleanAll();
121122
});
122-
it('should set domain id correctly', function (done) {
123+
it('should set domain id correctly', function (done) {
123124
const client = new DefaultHttpClient();
124125
const hcClient = new HcClient(client);
125126
let credential = new GlobalCredentials();
@@ -149,4 +150,19 @@ describe('httpclient tests', () => {
149150

150151
});
151152
});
153+
154+
describe("should return sdkstreamresponse", function () {
155+
const defaultClient = new DefaultHttpClient();
156+
let hcClient = new HcClient(defaultClient);
157+
158+
const res = hcClient['extractResponse']({
159+
data: new SdkResponse(),
160+
statusCode: 200,
161+
headers: { 'content-type': 'application/zip' }
162+
});
163+
164+
expect(res).not.to.be.null;
165+
expect(res.httpStatusCode).to.equal(200);
166+
expect(res).to.have.property('body');
167+
});
152168
});

0 commit comments

Comments
 (0)