Skip to content
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

feat(clients) Add getInAppMessages API #11348

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { fetchTransferHandler } from '../../../src/clients/handlers/fetch';
import {
getInAppMessages,
GetInAppMessagesInput,
GetInAppMessagesOutput,
} from '../../../src/AwsClients/Pinpoint';
import {
mockApplicationId,
mockEndpointId,
mockFailureResponse,
mockJsonResponse,
mockRequestId,
pinpointHandlerOptions,
} from '../testUtils/data';

jest.mock('../../../src/clients/handlers/fetch');

// API reference: https://docs.aws.amazon.com/pinpoint/latest/apireference/apps-application-id-endpoints-endpoint-id-inappmessages.html#GetInAppMessages
describe('Pinpoint - getInAppMessages', () => {
const InAppMessagesResponse = {
InAppMessageCampaigns: [],
};
const params: GetInAppMessagesInput = {
ApplicationId: mockApplicationId,
EndpointId: mockEndpointId,
};

test('happy case', async () => {
const expectedRequest = expect.objectContaining({
url: expect.objectContaining({
href: `https://pinpoint.us-east-1.amazonaws.com/v1/apps/${mockApplicationId}/endpoints/${mockEndpointId}/inappmessages`,
}),
method: 'GET',
headers: expect.objectContaining({
authorization: expect.stringContaining('Signature'),
'content-type': 'application/json',
host: 'pinpoint.us-east-1.amazonaws.com',
'x-amz-date': expect.anything(),
'x-amz-user-agent': expect.stringContaining('aws-amplify'),
}),
});
const successfulResponse = {
status: 200,
headers: {
'x-amzn-requestid': mockRequestId,
},
body: { ...InAppMessagesResponse },
};
const expectedOutput: GetInAppMessagesOutput = {
InAppMessagesResponse,
$metadata: expect.objectContaining({
attempts: 1,
requestId: mockRequestId,
httpStatusCode: 200,
}),
};
(fetchTransferHandler as jest.Mock).mockResolvedValue(
mockJsonResponse(successfulResponse)
);
const response = await getInAppMessages(pinpointHandlerOptions, params);
expect(response).toEqual(expectedOutput);
expect(fetchTransferHandler).toBeCalledWith(
expectedRequest,
expect.anything()
);
});

test('error case', async () => {
const expectedError = {
name: 'ForbiddenException',
message: mockFailureResponse.body.message,
};
(fetchTransferHandler as jest.Mock).mockResolvedValue(
mockJsonResponse(mockFailureResponse)
);
expect.assertions(1);
try {
await getInAppMessages(pinpointHandlerOptions, params);
fail('test should fail');
} catch (e) {
expect(e).toEqual(expect.objectContaining(expectedError));
}
});
});
27 changes: 7 additions & 20 deletions packages/core/__tests__/AwsClients/Pinpoint/putEvents-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ import {
import {
mockApplicationId,
mockEventsRequest,
mockFailureResponse,
mockJsonResponse,
mockRequestId,
pinpointHandlerOptions,
} from '../testUtils/data';

jest.mock('../../../src/clients/handlers/fetch');

// API reference: https://docs.aws.amazon.com/pinpoint/latest/apireference/apps-application-id-endpoints-endpoint-id.html#UpdateEndpoint
// API reference: https://docs.aws.amazon.com/pinpoint/latest/apireference/apps-application-id-events.html#PutEvents
describe('Pinpoint - putEvents', () => {
const MessageBody = {
Message: 'success',
RequestID: mockRequestId,
};
const EventsResponse = { Results: {} };
const params: PutEventsInput = {
ApplicationId: mockApplicationId,
EventsRequest: mockEventsRequest,
Expand All @@ -48,10 +46,10 @@ describe('Pinpoint - putEvents', () => {
headers: {
'x-amzn-requestid': mockRequestId,
},
body: { ...MessageBody },
body: { ...EventsResponse },
};
const expectedOutput: PutEventsOutput = {
EventsResponse: {},
EventsResponse,
$metadata: expect.objectContaining({
attempts: 1,
requestId: mockRequestId,
Expand All @@ -70,23 +68,12 @@ describe('Pinpoint - putEvents', () => {
});

test('error case', async () => {
const failureResponse = {
status: 400,
headers: {
'x-amzn-requestid': mockRequestId,
'x-amzn-errortype': 'ForbiddenException',
},
body: {
__type: 'ForbiddenException',
message: `Forbidden`,
},
};
const expectedError = {
name: 'ForbiddenException',
message: failureResponse.body.message,
message: mockFailureResponse.body.message,
};
(fetchTransferHandler as jest.Mock).mockResolvedValue(
mockJsonResponse(failureResponse)
mockJsonResponse(mockFailureResponse)
);
expect.assertions(1);
try {
Expand Down
16 changes: 3 additions & 13 deletions packages/core/__tests__/AwsClients/Pinpoint/updateEndpoint-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
mockApplicationId,
mockEndpointId,
mockEndpointRequest,
mockFailureResponse,
mockJsonResponse,
mockRequestId,
pinpointHandlerOptions,
Expand Down Expand Up @@ -72,23 +73,12 @@ describe('Pinpoint - updateEndpoint', () => {
});

test('error case', async () => {
const failureResponse = {
status: 400,
headers: {
'x-amzn-requestid': mockRequestId,
'x-amzn-errortype': 'ForbiddenException',
},
body: {
__type: 'ForbiddenException',
message: `Forbidden`,
},
};
const expectedError = {
name: 'ForbiddenException',
message: failureResponse.body.message,
message: mockFailureResponse.body.message,
};
(fetchTransferHandler as jest.Mock).mockResolvedValue(
mockJsonResponse(failureResponse)
mockJsonResponse(mockFailureResponse)
);
expect.assertions(1);
try {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/__tests__/AwsClients/testUtils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ export const mockEventsRequest = {
},
};

export const mockFailureResponse = {
status: 400,
headers: {
'x-amzn-requestid': mockRequestId,
'x-amzn-errortype': 'ForbiddenException',
},
body: {
__type: 'ForbiddenException',
message: `Forbidden`,
},
};

export const pinpointHandlerOptions = {
credentials: {
accessKeyId: mockCredentials.AccessKeyId,
Expand Down
56 changes: 56 additions & 0 deletions packages/core/src/AwsClients/Pinpoint/getInAppMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import type {
GetInAppMessagesCommandInput as GetInAppMessagesInput,
GetInAppMessagesCommandOutput as GetInAppMessagesOutput,
} from '@aws-sdk/client-pinpoint';
import { authenticatedHandler } from '../../clients/handlers/authenticated';
import { composeServiceApi } from '../../clients/internal/composeServiceApi';
import { extendedEncodeURIComponent } from '../../clients/middleware/signing/utils/extendedEncodeURIComponent';
import {
parseJsonBody,
parseJsonError,
parseMetadata,
} from '../../clients/serde';
import { Endpoint, HttpRequest, HttpResponse } from '../../clients/types';
import { defaultConfig, getSharedHeaders } from './base';

export type { GetInAppMessagesInput, GetInAppMessagesOutput };

const getInAppMessagesSerializer = (
{ ApplicationId, EndpointId }: GetInAppMessagesInput,
endpoint: Endpoint
): HttpRequest => {
const headers = getSharedHeaders();
const url = new URL(endpoint.url);
url.pathname = `v1/apps/${extendedEncodeURIComponent(
ApplicationId
)}/endpoints/${extendedEncodeURIComponent(EndpointId)}/inappmessages`;
return { method: 'GET', headers, url };
};

const getInAppMessagesDeserializer = async (
response: HttpResponse
): Promise<GetInAppMessagesOutput> => {
if (response.statusCode >= 300) {
const error = await parseJsonError(response);
throw error;
} else {
const { InAppMessageCampaigns } = await parseJsonBody(response);
return {
InAppMessagesResponse: { InAppMessageCampaigns },
$metadata: parseMetadata(response),
};
}
};

/**
* @internal
*/
export const getInAppMessages = composeServiceApi(
authenticatedHandler,
getInAppMessagesSerializer,
getInAppMessagesDeserializer,
defaultConfig
);
7 changes: 6 additions & 1 deletion packages/core/src/AwsClients/Pinpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export {
getInAppMessages,
GetInAppMessagesInput,
GetInAppMessagesOutput,
} from './getInAppMessages';
export { putEvents, PutEventsInput, PutEventsOutput } from './putEvents';
export {
updateEndpoint,
UpdateEndpointInput,
UpdateEndpointOutput,
} from './updateEndpoint';
export { putEvents, PutEventsInput, PutEventsOutput } from './putEvents';