Skip to content

Commit fb605f1

Browse files
committed
implement general wrapper
1 parent 470e0f5 commit fb605f1

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

packages/core/src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { registerCommands } from './commands'
5252
import endpoints from '../resources/endpoints.json'
5353
import { getLogger, maybeShowMinVscodeWarning, setupUninstallHandler } from './shared'
5454
import { showViewLogsMessage } from './shared/utilities/messages'
55+
import { AWSClientBuilderV3 } from './shared/awsClientBuilderV3'
5556

5657
disableAwsSdkWarning()
5758

@@ -116,6 +117,7 @@ export async function activateCommon(
116117
globals.machineId = await getMachineId()
117118
globals.awsContext = new DefaultAwsContext()
118119
globals.sdkClientBuilder = new DefaultAWSClientBuilder(globals.awsContext)
120+
globals.sdkClientBuilderV3 = new AWSClientBuilderV3(globals.awsContext)
119121
globals.loginManager = new LoginManager(globals.awsContext, new CredentialsStore())
120122

121123
// order matters here

packages/core/src/shared/awsClientBuilderV3.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import { omitIfPresent } from './utilities/tsUtils'
2727

2828
export type AwsClientConstructor<C> = new (o: AwsClientOptions) => C
2929

30-
interface AwsClient {
30+
export interface AwsClient {
3131
middlewareStack: any // Ideally this would extends MiddlewareStack<Input, Output>, but this causes issues on client construction.
32+
send: any
33+
destroy: () => void
3234
}
3335

3436
interface AwsConfigOptions {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
import * as vscode from 'vscode'
6+
import globals from '../extensionGlobals'
7+
import { AwsClient, AwsClientConstructor } from '../awsClientBuilderV3'
8+
import { pageableToCollection } from '../utilities/collectionUtils'
9+
10+
export abstract class ClientWrapper<C extends AwsClient> implements vscode.Disposable {
11+
protected client: C | undefined
12+
13+
public constructor(
14+
public readonly regionCode: string,
15+
private readonly clientType: AwsClientConstructor<any>
16+
) {}
17+
18+
protected async getClient() {
19+
if (this.client) {
20+
return this.client
21+
}
22+
this.client = await globals.sdkClientBuilderV3.createAwsService(this.clientType, undefined, this.regionCode)
23+
return this.client!
24+
}
25+
26+
protected async makeRequest<CommandInput extends object, Command extends object>(
27+
command: new (o: CommandInput) => Command,
28+
commandOptions: CommandInput
29+
) {
30+
const client = await this.getClient()
31+
return await client.send(new command(commandOptions))
32+
}
33+
34+
protected makePaginatedRequest<CommandInput extends object, CommandOutput extends object, Command extends object>(
35+
command: new (o: CommandInput) => Command,
36+
commandOptions: CommandInput,
37+
collectKey: keyof CommandOutput & string,
38+
nextTokenKey?: keyof CommandOutput & keyof CommandInput & string
39+
) {
40+
const requester = async (req: CommandInput) => await this.makeRequest(command, req)
41+
const response = pageableToCollection(
42+
requester,
43+
commandOptions,
44+
nextTokenKey ?? ('NextToken' as never),
45+
collectKey
46+
)
47+
return response
48+
}
49+
50+
public dispose() {
51+
this.client?.destroy()
52+
}
53+
}

packages/core/src/shared/extensionGlobals.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { UriHandler } from './vscode/uriHandler'
1919
import { GlobalState } from './globalState'
2020
import { setContext } from './vscode/setContext'
2121
import { getLogger } from './logger/logger'
22+
import { AWSClientBuilderV3 } from './awsClientBuilderV3'
2223

2324
type Clock = Pick<
2425
typeof globalThis,
@@ -197,6 +198,7 @@ export interface ToolkitGlobals {
197198
awsContext: AwsContext
198199
regionProvider: RegionProvider
199200
sdkClientBuilder: AWSClientBuilder
201+
sdkClientBuilderV3: AWSClientBuilderV3
200202
telemetry: TelemetryService & { logger: TelemetryLogger }
201203
/** template.yaml registry. _Avoid_ calling this until it is actually needed (for SAM features). */
202204
templateRegistry: Promise<CloudFormationTemplateRegistry>

0 commit comments

Comments
 (0)