Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
feat: add option for getFeedSubmissionResult to return json obj
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Aug 10, 2020
1 parent 032f363 commit ff949e2
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 38 deletions.
14 changes: 2 additions & 12 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ export class HttpClient {
method: HttpMethod,
info: ResourceInfo<TResource>,
body?: string,
stringResponse = false,
): Promise<[TRes | string, RequestMeta]> {
const marketplaceUri = this.options.marketplace.webServiceUri

Expand Down Expand Up @@ -427,18 +428,7 @@ export class HttpClient {
throw new InvalidUPCIdentifier(`${info.action} request failed`)
}

/**
* GetReport and GetFeedSubmissionResult returns a string that should be treated as a file instead of XML data
* Get Report
* http://docs.developer.amazonservices.com/en_CA/reports/Reports_GetReport.html
* GetFeedSubmissionResult
* http://docs.developer.amazonservices.com/en_CA/feeds/Feeds_GetFeedSubmissionResult.html
*/
if (info.action === 'GetReport' || info.action === 'GetFeedSubmissionResult') {
return parseResponse(response, true)
}

return parseResponse(response)
return parseResponse(response, stringResponse)
} catch (error) {
if (parser.validate(error) !== true) {
throw error
Expand Down
53 changes: 38 additions & 15 deletions src/sections/feeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
number,
oneOf,
optional,
record,
string,
unknown,
} from 'purify-ts'

import { ParsingError } from '../error'
import { InvalidParameterValue, ParsingError } from '../error'
import { HttpClient, RequestMeta, Resource } from '../http'
import { ensureString, mwsDate, NextToken, nextToken as nextTokenCodec } from '../parsing'

Expand Down Expand Up @@ -167,6 +169,7 @@ const GetFeedSubmissionResultResponse = Codec.custom<string>({

export interface GetFeedSubmissionResultParameters {
FeedSubmissionId: string
format?: string
}

export interface SubmitFeedParameters {
Expand Down Expand Up @@ -224,22 +227,42 @@ export class Feeds {

async getFeedSubmissionResult(
parameters: GetFeedSubmissionResultParameters,
): Promise<[FeedSubmission, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Feeds,
version: FEEDS_API_VERSION,
action: 'GetFeedSubmissionResult',
parameters: {
FeedSubmissionId: parameters.FeedSubmissionId,
},
})
): Promise<[FeedSubmission | Record<string, unknown>, RequestMeta] | void> {
const stringResponse = parameters.format === 'xml'

return GetFeedSubmissionResultResponse.decode(response).caseOf({
Right: (x) => [x, meta],
Left: (error) => {
throw new ParsingError(error)
const [response, meta] = await this.httpClient.request(
'POST',
{
resource: Resource.Feeds,
version: FEEDS_API_VERSION,
action: 'GetFeedSubmissionResult',
parameters: {
FeedSubmissionId: parameters.FeedSubmissionId,
},
},
})
'',
stringResponse,
)
if (stringResponse) {
return GetFeedSubmissionResultResponse.decode(response).caseOf({
Right: (x) => [x, meta],
Left: (error) => {
throw new ParsingError(error)
},
})
}
if (parameters.format === 'json') {
return record(string, unknown)
.decode(response)
.caseOf({
Right: (x) => [x, meta],
Left: (error) => {
throw new ParsingError(error)
},
})
}

throw new InvalidParameterValue('"format" parameter is incorrect')
}

async cancelFeedSubmissions(
Expand Down
19 changes: 12 additions & 7 deletions src/sections/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,19 @@ export class Reports {
}

async getReport(parameters: GetReportParameters): Promise<[Report, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Reports,
version: REPORTS_API_VERSION,
action: 'GetReport',
parameters: {
ReportId: parameters.ReportId,
const [response, meta] = await this.httpClient.request(
'POST',
{
resource: Resource.Reports,
version: REPORTS_API_VERSION,
action: 'GetReport',
parameters: {
ReportId: parameters.ReportId,
},
},
})
'',
true,
)

return GetReportResponse.decode(response).caseOf({
Right: (x) => [x, meta],
Expand Down
17 changes: 17 additions & 0 deletions test/integration/feeds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,30 @@ describe(`${Feeds.name}`, () => {

const parameters = {
FeedSubmissionId: '51793018437',
format: 'xml',
}

const [response] = await feeds.getFeedSubmissionResult(parameters)

expect(typeof response).toBe('string')
})

itci(
'should be able to get an XML string and parse it to JSON from get submission result',
async () => {
expect.assertions(1)

const parameters = {
FeedSubmissionId: '51793018437',
format: 'json',
}

const [response] = await feeds.getFeedSubmissionResult(parameters)

expect(typeof response).toBe('object')
},
)

itci('should be able to list feed recently submitted', async () => {
expect.assertions(1)

Expand Down
31 changes: 31 additions & 0 deletions test/unit/__snapshots__/feeds.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,37 @@ Array [
]
`;

exports[`feeds getFeedSubmissionResult returns an XML file parsed to json when succesful 1`] = `
Array [
Object {
"AmazonEnvelope": Object {
"Header": Object {
"DocumentVersion": 1.02,
"MerchantIdentifier": "T_M_GOOD_83835495",
},
"Message": Object {
"MessageID": 1,
"ProcessingReport": Object {
"DocumentTransactionID": 4319742521,
},
},
"MessageType": "ProcessingReport",
"attr": Object {
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsi:noNamespaceSchemaLocation": "amzn-envelope.xsd",
},
},
},
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": 2020-04-06T10:22:23.582Z,
"requestId": "0",
"timestamp": 2020-05-06T09:22:23.582Z,
},
]
`;

exports[`feeds getFeedSubmissionResult returns an XML file when succesful 1`] = `
Array [
"<AmazonEnvelope xmlns:xsi=\\"http://www.w3.org/2001/XMLSchema-instance\\" xsi:noNamespaceSchemaLocation=\\"amzn-envelope.xsd\\">
Expand Down
41 changes: 37 additions & 4 deletions test/unit/feeds.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParsingError, SubmitFeedParameters } from '../../src'
import { InvalidParameterValue, ParsingError, SubmitFeedParameters } from '../../src'
import { NextToken } from '../../src/parsing'
import { createMockHttpClient, mockMwsFail, mockParsingError, parsingErrorRegex } from '../utils'

Expand Down Expand Up @@ -27,23 +27,56 @@ describe('feeds', () => {
})

describe('getFeedSubmissionResult', () => {
const parameters = { FeedSubmissionId: '' }
const xmlParameters = {
FeedSubmissionId: '',
format: 'xml',
}

const jsonParameters = {
FeedSubmissionId: '',
format: 'json',
}

const incorrectParameters = {
FeedSubmissionId: '',
format: 'yml',
}

it('returns an XML file when succesful', async () => {
expect.assertions(1)

const mockGetFeedSubmissionResult = createMockHttpClient('feeds_get_feed_submission_result')

expect(
await mockGetFeedSubmissionResult.feeds.getFeedSubmissionResult(parameters),
await mockGetFeedSubmissionResult.feeds.getFeedSubmissionResult(xmlParameters),
).toMatchSnapshot()
})

it('returns an XML file parsed to json when succesful', async () => {
expect.assertions(1)

const mockGetFeedSubmissionResult = createMockHttpClient('feeds_get_feed_submission_result')

expect(
await mockGetFeedSubmissionResult.feeds.getFeedSubmissionResult(jsonParameters),
).toMatchSnapshot()
})

it('throws invalid parameters error if format parameter is incorrect', async () => {
expect.assertions(1)

const mockGetFeedSubmissionResult = createMockHttpClient('feeds_get_feed_submission_result')

await expect(() =>
mockGetFeedSubmissionResult.feeds.getFeedSubmissionResult(incorrectParameters),
).rejects.toStrictEqual(new InvalidParameterValue('"format" parameter is incorrect'))
})

it('throws a parsing error when the response isnt valid', async () => {
expect.assertions(1)

await expect(() =>
mockMwsFail.feeds.getFeedSubmissionResult(parameters),
mockMwsFail.feeds.getFeedSubmissionResult(xmlParameters),
).rejects.toStrictEqual(new ParsingError('Expected feed to have length of more than 0'))
})
})
Expand Down

0 comments on commit ff949e2

Please sign in to comment.