Skip to content

Commit ccbdffa

Browse files
committed
add request client
1 parent 80722bb commit ccbdffa

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/commons/tencent/user/index.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { localize } from "vscode-nls-i18n";
2-
import { ExtensionContext, workspace, ConfigurationTarget, window, ProgressLocation, MessageItem } from "vscode";
2+
import { ExtensionContext, workspace, ConfigurationTarget, window, ProgressLocation, MessageItem, extensions } from "vscode";
33

44
import { container } from "../../container";
55
import { Context } from "../../context";
66
import { tree } from "../treeDataProvider";
77
import { getCredentailByInput } from "./auth";
88
import { AbstractClient } from "tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client";
99
import { Credential } from "tencentcloud-sdk-nodejs/tencentcloud/common/interface";
10-
import { getCamClient, getStsClient } from "@/connectivity/client";
10+
import { getCamClient, getCommonClient, getStsClient } from "@/connectivity/client";
1111
import * as loginMgt from "../../../views/login/loginMgt";
1212
import * as settingUtils from "../../../utils/settingUtils";
1313

@@ -24,6 +24,7 @@ export namespace user {
2424
arn?: string;
2525
}
2626

27+
export const REQUEST_CLIENT_PREFIX = "Terraform-Vscode-";//Terraform-1.81.61@vscode";
2728
export const AKSK_TITLE = "TcTerraform.pickup.aksk";
2829
export const OAUTH_TITLE = "TcTerraform.pickup.oauth";
2930
export const AKSK_PLACEHOLD = "TcTerraform.pickup.aksk.placeholder";
@@ -71,10 +72,16 @@ export namespace user {
7172
try {
7273
// query user info
7374
const stsClient = await getStsClient();
74-
const stsResp = await stsClient?.GetCallerIdentity().
75+
const currentVersion = getExtensionVersion();
76+
const reqCli = `${REQUEST_CLIENT_PREFIX}v${currentVersion}`;
77+
stsClient.sdkVersion = reqCli;
78+
console.log('[DEBUG]--------------------getStsClient:', stsClient);
79+
// const stsClient = await getCommonClient("sts.tencentcloudapi.com", "2018-08-13");
80+
// const stsResp = await stsClient.request("GetCallerIdentity", req).
81+
const stsResp = await stsClient?.GetCallerIdentity(null).
7582
then(
7683
(result) => {
77-
console.debug('[DEBUG]--------------------------------result:', result);
84+
console.debug('[DEBUG]--------------------------------GetCallerIdentity result:', result);
7885
if (!result) {
7986
throw new Error('[Warn] GetCallerIdentity result.TotalCount is 0.');
8087
}
@@ -84,12 +91,15 @@ export namespace user {
8491
throw new Error(err);
8592
}
8693
);
94+
// ) as stsModels.GetCallerIdentityResponse;
8795

8896
const camClient = await getCamClient();
89-
const camResp = await camClient?.GetUserAppId().
97+
camClient.sdkVersion = reqCli;
98+
console.log('[DEBUG]--------------------getCamClient:', camClient);
99+
const camResp = await camClient?.GetUserAppId(null).
90100
then(
91101
(result) => {
92-
console.debug('[DEBUG]--------------------------------result:', result);
102+
console.debug('[DEBUG]--------------------------------GetUserAppId result:', result);
93103
if (!result) {
94104
throw new Error('[Warn] GetUserAppId result.TotalCount is 0.');
95105
}
@@ -122,6 +132,12 @@ export namespace user {
122132
}
123133
}
124134

135+
function getExtensionVersion(): string {
136+
let extension = extensions.getExtension('Tencent-Cloud.vscode-tencentcloud-terraform');
137+
let currentVersion = extension.packageJSON.version;
138+
return currentVersion;
139+
}
140+
125141
export async function loginOut() {
126142
const yesBtn: MessageItem = { title: localize("TcTerraform.common.yes") };
127143
const action = await window.showWarningMessage(

src/connectivity/client.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as tencentcloud from "tencentcloud-sdk-nodejs";
1111
import { localize } from "vscode-nls-i18n";
1212
import * as settingUtils from "../utils/settingUtils";
1313

14+
const DefaultReqCliHeader = { "X-TC-RequestClient": "Terraform-Vscode-v0.0.26" };
1415

1516
export async function getTkeClient(): Promise<TkeClient> {
1617
const [secretId, secretKey, region] = settingUtils.getAKSKandRegion();
@@ -32,6 +33,7 @@ export async function getTkeClient(): Promise<TkeClient> {
3233
httpProfile: {
3334
reqMethod: "POST", // 请求方法
3435
reqTimeout: 30, // 请求超时时间,默认60s
36+
headers: DefaultReqCliHeader,
3537
},
3638
},
3739
});
@@ -60,12 +62,13 @@ export async function getCvmClient(): Promise<CvmClient> {
6062
reqMethod: "POST", // 请求方法
6163
// reqTimeout: 60, // 请求超时时间,默认60s
6264
endpoint: "cvm.tencentcloudapi.com",
65+
headers: DefaultReqCliHeader,
6366
},
6467
},
6568
});
6669
}
6770

68-
export async function getCommonClient(): Promise<AbstractClient> {
71+
export async function getCommonClient(ep?, version?, reqCli?: string): Promise<AbstractClient> {
6972
const [secretId, secretKey, region] = settingUtils.getAKSKandRegion();
7073

7174
if (secretId === undefined || secretKey === undefined || secretId === null || secretKey === null || secretId === '' || secretKey === '') {
@@ -75,20 +78,24 @@ export async function getCommonClient(): Promise<AbstractClient> {
7578
}
7679

7780
const client = new AbstractClient(
78-
"open.test.tencentcloudapi.com",
79-
"2018-12-25",
81+
ep ?? "open.test.tencentcloudapi.com",
82+
version ?? "2018-12-25",
8083
{
8184
credential: {
8285
secretId: secretId,
8386
secretKey: secretKey,
8487
},
88+
region: region ?? "ap-guangzhou",
8589
profile: {
8690
httpProfile: {
87-
proxy: "http://9.135.97.58:8899",
91+
reqMethod: "POST", // 请求方法
92+
endpoint: ep ?? "open.test.tencentcloudapi.com",
93+
headers: DefaultReqCliHeader,
8894
},
8995
},
9096
}
9197
);
98+
client.sdkVersion = reqCli ?? "Terraform-1.81.61@vscode";
9299

93100
return client;
94101
}

0 commit comments

Comments
 (0)