-
Notifications
You must be signed in to change notification settings - Fork 734
deps(sdk): barebones general public client builder. #5940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
624407d
create barebone sdk builder
Hweinstock d721869
migrate ec2 (working)
Hweinstock 31fcd52
solve typing problem
Hweinstock 8a0c77c
work towards changing type system
Hweinstock 013677d
more progress, without compile errors
Hweinstock 82e92c3
remove event listener tests
Hweinstock 4dc8f7a
passing basic tests
Hweinstock 6a32dff
find workaround to add middlestack
Hweinstock 36c2c75
add test case for region
Hweinstock f506b47
add comments and remove half-supported features
Hweinstock f9fed75
add more tests
Hweinstock 666bfc8
add comment explaining test
Hweinstock f24eb45
undo change to .eslintrc.js
Hweinstock f94d505
remove console.log
Hweinstock d806de9
rename to ec2Wrapper to avoid confusion
Hweinstock 93f7ae2
move utility function to util file
Hweinstock 642487b
Merge branch 'master' into migrateEc2
Hweinstock ab817b1
fix merge conflicts
Hweinstock 7a3b885
try different type
Hweinstock 4f46c54
merge master, resolve conflicts
Hweinstock 7d3cdf6
Merge branch 'master' into migrateEc2
Hweinstock 12ded34
merge in master (step1)
Hweinstock 9b45344
update package.json/package-lock.json
Hweinstock fcf13d3
update package/package-lock.json
Hweinstock 8ca643f
update package.json/package-lock again
Hweinstock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { CredentialsShim } from '../auth/deprecated/loginManager' | ||
| import { AwsContext } from './awsContext' | ||
| import { AwsCredentialIdentityProvider } from '@smithy/types' | ||
| import { Client as IClient } from '@smithy/types' | ||
| import { getUserAgent } from './telemetry/util' | ||
| import { DevSettings } from './settings' | ||
| import { | ||
| DeserializeHandler, | ||
| DeserializeHandlerOptions, | ||
| DeserializeMiddleware, | ||
| HandlerExecutionContext, | ||
| Provider, | ||
| UserAgent, | ||
| } from '@aws-sdk/types' | ||
| import { HttpResponse } from '@aws-sdk/protocol-http' | ||
| import { telemetry } from './telemetry' | ||
| import { getRequestId } from './errors' | ||
| import { extensionVersion } from '.' | ||
| import { getLogger } from './logger' | ||
|
|
||
| export type AwsClient = IClient<any, any, any> | ||
| interface AwsConfigOptions { | ||
| credentials: AwsCredentialIdentityProvider | ||
| region: string | Provider<string> | ||
| customUserAgent: UserAgent | ||
| requestHandler: any | ||
| apiVersion: string | ||
| endpoint: string | ||
| } | ||
| export type AwsClientOptions = AwsConfigOptions | ||
|
|
||
| export interface AWSClientBuilderV3 { | ||
| createAwsService<C extends AwsClient>( | ||
| type: new (o: AwsClientOptions) => C, | ||
| options?: Partial<AwsClientOptions>, | ||
| region?: string, | ||
| userAgent?: boolean, | ||
| settings?: DevSettings | ||
| ): Promise<C> | ||
| } | ||
|
|
||
| export class DefaultAWSClientBuilderV3 implements AWSClientBuilderV3 { | ||
| public constructor(private readonly context: AwsContext) {} | ||
|
|
||
| private getShim(): CredentialsShim { | ||
| const shim = this.context.credentialsShim | ||
| if (!shim) { | ||
| throw new Error('Toolkit is not logged-in.') | ||
| } | ||
| return shim | ||
| } | ||
|
|
||
| public async createAwsService<C extends AwsClient>( | ||
| type: new (o: AwsClientOptions) => C, | ||
| options?: Partial<AwsClientOptions>, | ||
| region?: string, | ||
| userAgent: boolean = true, | ||
| settings?: DevSettings | ||
| ): Promise<C> { | ||
| const shim = this.getShim() | ||
| const opt = (options ?? {}) as AwsClientOptions | ||
|
|
||
| if (!opt.region && region) { | ||
| opt.region = region | ||
| } | ||
|
|
||
| if (!opt.customUserAgent && userAgent) { | ||
| opt.customUserAgent = [[getUserAgent({ includePlatform: true, includeClientId: true }), extensionVersion]] | ||
| } | ||
| // TODO: add tests for refresh logic. | ||
| opt.credentials = async () => { | ||
| const creds = await shim.get() | ||
| if (creds.expiration && creds.expiration.getTime() < Date.now()) { | ||
| return shim.refresh() | ||
| } | ||
| return creds | ||
| } | ||
|
|
||
| const service = new type(opt) | ||
| // TODO: add middleware for logging, telemetry, endpoints. | ||
| service.middlewareStack.add(telemetryMiddleware, { step: 'deserialize' } as DeserializeHandlerOptions) | ||
| return service | ||
| } | ||
| } | ||
|
|
||
| export function getServiceId(context: { clientName?: string; commandName?: string }): string { | ||
| return context.clientName?.toLowerCase().replace(/client$/, '') ?? 'unknown-service' | ||
| } | ||
|
|
||
| /** | ||
| * Record request IDs to the current context, potentially overriding the field if | ||
| * multiple API calls are made in the same context. We only do failures as successes are generally uninteresting and noisy. | ||
| */ | ||
| export function recordErrorTelemetry(err: Error, serviceName?: string) { | ||
| interface RequestData { | ||
| requestId?: string | ||
| requestServiceType?: string | ||
| } | ||
|
|
||
| telemetry.record({ | ||
| requestId: getRequestId(err), | ||
| requestServiceType: serviceName, | ||
| } satisfies RequestData as any) | ||
| } | ||
|
|
||
| export function omitIfPresent<T extends Record<string, unknown>>(obj: T, keys: string[]): T { | ||
| const objCopy = { ...obj } | ||
| for (const key of keys) { | ||
| if (key in objCopy) { | ||
| ;(objCopy as any)[key] = '[omitted]' | ||
| } | ||
| } | ||
| return objCopy | ||
| } | ||
|
|
||
| function logAndThrow(e: any, serviceId: string, errorMessageAppend: string): never { | ||
| if (e instanceof Error) { | ||
| recordErrorTelemetry(e, serviceId) | ||
| const err = { ...e } | ||
| delete err['stack'] | ||
| getLogger().error('API Response %s: %O', errorMessageAppend, err) | ||
| } | ||
| throw e | ||
| } | ||
| /** | ||
| * Telemetry logic to be added to all created clients. Adds logging and emitting metric on errors. | ||
| */ | ||
|
|
||
| const telemetryMiddleware: DeserializeMiddleware<any, any> = | ||
| (next: DeserializeHandler<any, any>, context: HandlerExecutionContext) => async (args: any) => { | ||
| if (!HttpResponse.isInstance(args.request)) { | ||
| return next(args) | ||
| } | ||
| const serviceId = getServiceId(context as object) | ||
| const { hostname, path } = args.request | ||
| const logTail = `(${hostname} ${path})` | ||
| const result = await next(args).catch((e: any) => logAndThrow(e, serviceId, logTail)) | ||
| if (HttpResponse.isInstance(result.response)) { | ||
| // TODO: omit credentials / sensitive info from the logs / telemetry. | ||
| const output = omitIfPresent(result.output, []) | ||
| getLogger().debug('API Response %s: %O', logTail, output) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.