Skip to content

Commit

Permalink
Merge branch 'modularization' into ui-components/master
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanranz committed Dec 17, 2019
2 parents dc5755e + 8ac8798 commit 1d38c9a
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 42 deletions.
8 changes: 6 additions & 2 deletions packages/analytics/src/Providers/AWSKinesisProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
* and limitations under the License.
*/

import { ConsoleLogger as Logger, Credentials } from '@aws-amplify/core';
import {
ConsoleLogger as Logger,
Credentials,
appendAmplifyUserAgent,
} from '@aws-amplify/core';
import { KinesisClient } from '@aws-sdk/client-kinesis-browser/KinesisClient';
import { PutRecordsCommand } from '@aws-sdk/client-kinesis-browser/commands/PutRecordsCommand';
import { AnalyticsProvider } from '../types';
Expand Down Expand Up @@ -219,7 +223,7 @@ export class AWSKinesisProvider implements AnalyticsProvider {
region,
credentials,
});

appendAmplifyUserAgent(this._kinesis);
return true;
}

Expand Down
22 changes: 1 addition & 21 deletions packages/analytics/src/Providers/AWSPinpointProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,27 +521,7 @@ export class AWSPinpointProvider implements AnalyticsProvider {
const { region } = config;
logger.debug('init clients with credentials', credentials);
this.pinpointClient = new PinpointClient({ region, credentials });

if (Platform.isReactNative) {
const appendUserAgentMiddleware = next => args => {
const { request } = args;
if (!request.headers['User-Agent']) {
request.headers['User-Agent'] = Platform.userAgent;
} else {
request.headers['User-Agent'] += Platform.userAgent;
}
return next({
...args,
request,
});
};

this.pinpointClient.middlewareStack.add(appendUserAgentMiddleware, {
step: 'build',
priority: 0,
tags: { CUSTOM_USER_AGENT: true },
});
}
appendAmplifyUserAgent(this.pinpointClient);
}

private async _getEndpointId(cacheKey) {
Expand Down
9 changes: 7 additions & 2 deletions packages/analytics/src/Providers/AmazonPersonalizeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
* and limitations under the License.
*/

import { ConsoleLogger as Logger, Credentials, JS } from '@aws-amplify/core';
import {
ConsoleLogger as Logger,
Credentials,
JS,
appendAmplifyUserAgent,
} from '@aws-amplify/core';
import { PersonalizeEventsClient } from '@aws-sdk/client-personalize-events-browser/PersonalizeEventsClient';
import { PutEventsCommand } from '@aws-sdk/client-personalize-events-browser/commands/PutEventsCommand';
import { PutEventsInput } from '@aws-sdk/client-personalize-events-browser/types/PutEventsInput';
Expand Down Expand Up @@ -374,7 +379,7 @@ export class AmazonPersonalizeProvider implements AnalyticsProvider {
region,
credentials,
});

appendAmplifyUserAgent(this._personalize);
return true;
}

Expand Down
17 changes: 13 additions & 4 deletions packages/core/src/Credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StorageHelper } from './StorageHelper';
import { makeQuerablePromise } from './JS';
import { FacebookOAuth, GoogleOAuth } from './OAuthHelper';
import { ICredentials } from './types';
import { appendAmplifyUserAgent } from './Platform';
import { Amplify } from './Amplify';
import {
fromCognitoIdentity,
Expand Down Expand Up @@ -210,7 +211,7 @@ export class CredentialsClass {
signer: {} as any,
});
cognitoClient.middlewareStack.remove('SIGNATURE');

appendAmplifyUserAgent(cognitoClient);
let credentials = undefined;
if (identityId && identityId !== 'undefined') {
const cognitoIdentityParams: FromCognitoIdentityParameters = {
Expand Down Expand Up @@ -273,6 +274,7 @@ export class CredentialsClass {
signer: {} as any,
});
cognitoClient.middlewareStack.remove('SIGNATURE');
appendAmplifyUserAgent(cognitoClient);
const cognitoIdentityParams: FromCognitoIdentityPoolParameters = {
logins,
identityPoolId,
Expand Down Expand Up @@ -309,14 +311,15 @@ export class CredentialsClass {
signer: {} as any,
});
cognitoClient.middlewareStack.remove('SIGNATURE');
appendAmplifyUserAgent(cognitoClient);

/*
Retreiving identityId with GetIdCommand to mimic the behavior in the following code in aws-sdk-v3:
https://git.io/JeDxU
Note: Retreive identityId from CredentialsProvider once aws-sdk-js v3 supports this.
*/
let credentials: CredentialProvider = async () => {
const credentialsProvider: CredentialProvider = async () => {
const { IdentityId } = await cognitoClient.send(
new GetIdCommand({
IdentityPoolId: identityPoolId,
Expand All @@ -331,11 +334,17 @@ export class CredentialsClass {
identityId: IdentityId,
};

credentials = fromCognitoIdentity(cognitoIdentityParams);
const credentialsFromCognitoIdentity = fromCognitoIdentity(
cognitoIdentityParams
);

return credentials();
return credentialsFromCognitoIdentity();
};

const credentials = credentialsProvider().catch(async err => {
throw err;
});

return this._loadCredentials(credentials, 'userPool', true, null);
}

Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/Platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ const appendUserAgentMiddleware = next => args => {
};

export const appendAmplifyUserAgent = client => {
client.middlewareStack.add(appendUserAgentMiddleware, {
step: 'build',
priority: 0,
tags: { CUSTOM_USER_AGENT: true },
});
if (
typeof client !== undefined &&
client &&
typeof client.middlewareStack !== undefined &&
client.middlewareStack
) {
client.middlewareStack.add(appendUserAgentMiddleware, {
step: 'build',
priority: 0,
tags: { CUSTOM_USER_AGENT: true },
});
}
};

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/interactions/src/Providers/AWSLexProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import { InteractionsOptions, InteractionsMessage } from '../types';
import { LexRuntimeServiceClient } from '@aws-sdk/client-lex-runtime-service-browser/LexRuntimeServiceClient';
import { PostTextCommand } from '@aws-sdk/client-lex-runtime-service-browser/commands/PostTextCommand';
import { PostContentCommand } from '@aws-sdk/client-lex-runtime-service-browser/commands/PostContentCommand';
import { ConsoleLogger as Logger, Credentials } from '@aws-amplify/core';
import {
ConsoleLogger as Logger,
Credentials,
appendAmplifyUserAgent,
} from '@aws-amplify/core';

const logger = new Logger('AWSLexProvider');

Expand Down Expand Up @@ -95,6 +99,7 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
region: this._config[botname].region,
credentials,
});
appendAmplifyUserAgent(this.lexRuntimeServiceClient);

let params;
if (typeof message === 'string') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Credentials,
ConsoleLogger as Logger,
Signer,
appendAmplifyUserAgent,
} from '@aws-amplify/core';
import {
EventStreamMarshaller,
Expand Down Expand Up @@ -65,6 +66,7 @@ export class AmazonAIConvertPredictionsProvider extends AbstractConvertPredictio
}

this.translateClient = new TranslateClient({ region, credentials });
appendAmplifyUserAgent(this.translateClient);
const translateTextCommand = new TranslateTextCommand({
SourceLanguageCode: sourceLanguageCode,
TargetLanguageCode: targetLanguageCode,
Expand Down Expand Up @@ -108,6 +110,7 @@ export class AmazonAIConvertPredictionsProvider extends AbstractConvertPredictio
}

this.pollyClient = new PollyClient({ region, credentials });
appendAmplifyUserAgent(this.pollyClient);
const synthesizeSpeechCommand = new SynthesizeSpeechCommand({
OutputFormat: 'mp3',
Text: input.textToSpeech.source.text,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Credentials, ConsoleLogger as Logger } from '@aws-amplify/core';
import {
Credentials,
ConsoleLogger as Logger,
appendAmplifyUserAgent,
} from '@aws-amplify/core';
import Storage from '@aws-amplify/storage';
import { AbstractIdentifyPredictionsProvider } from '../types/Providers';
import { RekognitionClient } from '@aws-sdk/client-rekognition-browser/RekognitionClient';
Expand Down Expand Up @@ -129,7 +133,9 @@ export class AmazonAIIdentifyPredictionsProvider extends AbstractIdentifyPredict
} = {},
} = this._config;
this.rekognitionClient = new RekognitionClient({ region, credentials });
appendAmplifyUserAgent(this.rekognitionClient);
this.textractClient = new TextractClient({ region, credentials });
appendAmplifyUserAgent(this.textractClient);
let inputDocument: Document;

try {
Expand Down Expand Up @@ -223,6 +229,7 @@ export class AmazonAIIdentifyPredictionsProvider extends AbstractIdentifyPredict
} = {},
} = this._config;
this.rekognitionClient = new RekognitionClient({ region, credentials });
appendAmplifyUserAgent(this.rekognitionClient);
let inputImage: Image;
await this.configureSource(input.labels.source)
.then(data => {
Expand Down Expand Up @@ -338,6 +345,7 @@ export class AmazonAIIdentifyPredictionsProvider extends AbstractIdentifyPredict
// default arguments

this.rekognitionClient = new RekognitionClient({ region, credentials });
appendAmplifyUserAgent(this.rekognitionClient);
let inputImage: Image;
await this.configureSource(input.entities.source)
.then(data => (inputImage = data))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Credentials } from '@aws-amplify/core';
import { Credentials, appendAmplifyUserAgent } from '@aws-amplify/core';
import { AbstractInterpretPredictionsProvider } from '../types/Providers';

import {
Expand Down Expand Up @@ -51,6 +51,7 @@ export class AmazonAIInterpretPredictionsProvider extends AbstractInterpretPredi
credentials,
region,
});
appendAmplifyUserAgent(comprehendClient);

const doAll = interpretType === InterpretTextCategories.ALL;

Expand Down
12 changes: 7 additions & 5 deletions packages/storage/src/providers/AWSS3Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Hub,
Credentials,
Parser,
appendAmplifyUserAgent,
} from '@aws-amplify/core';
import { S3Client } from '@aws-sdk/client-s3-browser/S3Client';
import { formatUrl } from '@aws-sdk/util-format-url';
Expand Down Expand Up @@ -165,10 +166,9 @@ export class AWSS3Provider implements StorageProvider {
try {
const signer = new S3RequestPresigner({ ...s3.config });
const request = await createRequest(s3, new GetObjectCommand(params));
const url = formatUrl((await signer.presignRequest(
request,
params.Expires
)) as any);
const url = formatUrl(
(await signer.presignRequest(request, params.Expires)) as any
);
dispatchStorageEvent(
track,
'getSignedUrl',
Expand Down Expand Up @@ -479,11 +479,13 @@ export class AWSS3Provider implements StorageProvider {
};
}

return new S3Client({
const s3client = new S3Client({
region,
credentials,
...localTestingConfig,
});
appendAmplifyUserAgent(s3client);
return s3client;
}
}

Expand Down

0 comments on commit 1d38c9a

Please sign in to comment.