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

Commit

Permalink
feat: created submitFeeds, integ and unit tests, http update
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado authored and moltar committed Jun 28, 2020
1 parent 6c6d670 commit 01e0659
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 15 deletions.
43 changes: 30 additions & 13 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export class HttpClient {
public async request<TResource extends Resource, TRes>(
method: HttpMethod,
info: ResourceInfo<TResource>,
body?: string,
): Promise<[TRes | string, RequestMeta]> {
const marketplaceUri = this.options.marketplace.webServiceUri

Expand Down Expand Up @@ -301,19 +302,35 @@ export class HttpClient {
'user-agent': '@scaleleap/amazon-mws-api-sdk/1.0.0 (Language=JavaScript)',
}

const config =
method === 'GET'
? {
url: `${url}?${canonicalizeParameters(parametersWithSignature)}`,
method,
headers,
}
: {
url,
method,
headers,
data: canonicalizeParameters(parametersWithSignature),
}
let config: Request
if (method === 'GET') {
config = {
url: `${url}?${canonicalizeParameters(parametersWithSignature)}`,
method,
headers,
}
/**
* `SubmitFeed` has the feed passed as an XML file in the body
* and the other parameters as query parameters
*/
} else if (body && info.action === 'SubmitFeed') {
config = {
url: `${url}?${canonicalizeParameters(parametersWithSignature)}`,
method,
headers: {
'Content-Type': 'text/xml',
...headers,
},
data: body,
}
} else {
config = {
url,
method,
headers,
data: canonicalizeParameters(parametersWithSignature),
}
}

try {
const response = await this.fetch(config)
Expand Down
51 changes: 51 additions & 0 deletions src/sections/feeds.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'crypto'
import {
array,
boolean,
Expand Down Expand Up @@ -156,9 +157,59 @@ interface GetFeedSubmissionResultParameters {
FeedSubmissionId: string
}

export interface SubmitFeedParameters {
FeedContent: string
FeedType: FeedType
MarketplaceIdList?: string[]
PurgeAndReplace?: boolean
AmazonOrderId?: string
DocumentId?: string
}

const SubmitFeed = Codec.interface({
FeedSubmissionInfo: optional(oneOf([FeedSubmissionInfo, array(FeedSubmissionInfo), exactly('')])),
})

export type SubmitFeed = GetInterface<typeof SubmitFeed>

const SubmitFeedResponse = Codec.interface({
SubmitFeedResponse: Codec.interface({
SubmitFeedResult: SubmitFeed,
}),
})

export class Feeds {
constructor(private httpClient: HttpClient) {}

async submitFeed(parameters: SubmitFeedParameters): Promise<[SubmitFeed, RequestMeta]> {
const hash = crypto.createHash('md5').update(parameters.FeedContent).digest('base64')

const [response, meta] = await this.httpClient.request(
'POST',
{
resource: Resource.Feeds,
version: FEEDS_API_VERSION,
action: 'SubmitFeed',
parameters: {
FeedType: parameters.FeedType,
'MarketplaceIdList.Id': parameters.MarketplaceIdList,
PurgeAndReplace: parameters.PurgeAndReplace,
AmazonOrderId: parameters.AmazonOrderId,
DocumentId: parameters.DocumentId,
ContentMD5Value: hash,
},
},
parameters.FeedContent,
)

return SubmitFeedResponse.decode(response).caseOf({
Right: (x) => [x.SubmitFeedResponse.SubmitFeedResult, meta],
Left: (error) => {
throw new ParsingError(error)
},
})
}

async getFeedSubmissionResult(
parameters: GetFeedSubmissionResultParameters,
): Promise<[FeedSubmission, RequestMeta]> {
Expand Down
21 changes: 20 additions & 1 deletion test/integration/feeds.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Feeds } from '../../src'
import { readFileSync } from 'fs'
import { join } from 'path'

import { Feeds, SubmitFeedParameters } from '../../src'
import { Config } from './config'
import { itci } from './it'

Expand All @@ -8,6 +11,22 @@ const httpClient = new Config().createHttpClient()
describe(`${Feeds.name}`, () => {
const feeds = new Feeds(httpClient)

itci('should be able to submit sample feed', async () => {
expect.assertions(1)

const parameters: SubmitFeedParameters = {
FeedContent: readFileSync(
join(__dirname, `/__fixtures__/submit_feed_sample_feed_content.xml`),
{ encoding: 'utf8' },
),
FeedType: '_POST_PRODUCT_DATA_',
}

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

expect(response).toBeDefined()
})

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

Expand Down
82 changes: 82 additions & 0 deletions test/integration/submit_feed_sample_feed_content.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>M_EXAMPLE_123456</MerchantIdentifier>
</Header>
<MessageType>Product</MessageType>
<PurgeAndReplace>false</PurgeAndReplace>
<Message>
<MessageID>1</MessageID>
<OperationType>Update</OperationType>
<Product>
<SKU>56789</SKU>
<StandardProductID>
<Type>ASIN</Type>
<Value>B0EXAMPLEG</Value>
</StandardProductID>
<ProductTaxCode>A_GEN_NOTAX</ProductTaxCode>
<DescriptionData>
<Title>Example Product Title</Title>
<Brand>Example Product Brand</Brand>
<Description>This is an example product description.</Description>
<BulletPoint>Example Bullet Point 1</BulletPoint>
<BulletPoint>Example Bullet Point 2</BulletPoint>
<MSRP currency="USD">25.19</MSRP>
<Manufacturer>Example Product Manufacturer</Manufacturer>
<ItemType>example-item-type</ItemType>
</DescriptionData>
<ProductData>
<Health>
<ProductType>
<HealthMisc>
<Ingredients>Example Ingredients</Ingredients>
<Directions>Example Directions</Directions>
</HealthMisc>
</ProductType>
</Health>
</ProductData>
</Product>
</Message>
</AmazonEnvelope>

<!-- <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>M_EXAMPLE_123456</MerchantIdentifier>
</Header>
<MessageType>Product</MessageType>
<PurgeAndReplace>false</PurgeAndReplace>
<Message>
<MessageID>1</MessageID>
<OperationType>Update</OperationType>
<Product>
<SKU>56789</SKU>
<StandardProductID>
<Type>ASIN</Type>
<Value>B0EXAMPLEG</Value>
</StandardProductID>
<ProductTaxCode>A_GEN_NOTAX</ProductTaxCode>
<DescriptionData>
<Title>Example Product Title</Title>
<Brand>Example Product Brand</Brand>
<Description>This is an example product description.</Description>
<BulletPoint>Example Bullet Point 1</BulletPoint>
<BulletPoint>Example Bullet Point 2</BulletPoint>
<MSRP currency="USD">25.19</MSRP>
<Manufacturer>Example Product Manufacturer</Manufacturer>
<ItemType>example-item-type</ItemType>
</DescriptionData>
<ProductData>
<Health>
<ProductType>
<HealthMisc>
<Ingredients>Example Ingredients</Ingredients>
<Directions>Example Directions</Directions>
</HealthMisc>
</ProductType>
</Health>
</ProductData>
</Product>
</Message>
</AmazonEnvelope> -->
13 changes: 13 additions & 0 deletions test/unit/__fixtures__/feeds_submit_feed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<SubmitFeedResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">
<SubmitFeedResult>
<FeedSubmissionInfo>
<FeedSubmissionId>2291326430</FeedSubmissionId>
<FeedType>_POST_PRODUCT_DATA_</FeedType>
<SubmittedDate>2009-02-20T02:10:35+00:00</SubmittedDate>
<FeedProcessingStatus>_SUBMITTED_</FeedProcessingStatus>
</FeedSubmissionInfo>
</SubmitFeedResult>
<ResponseMetadata>
<RequestId>75424a38-f333-4105-98f0-2aa9592d665c</RequestId>
</ResponseMetadata>
</SubmitFeedResponse>
20 changes: 20 additions & 0 deletions test/unit/__snapshots__/feeds.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,23 @@ Array [
},
]
`;
exports[`feeds submitFeed returns feed info of submitted feeds if succesful 1`] = `
Array [
Object {
"FeedSubmissionInfo": Object {
"FeedProcessingStatus": "_SUBMITTED_",
"FeedSubmissionId": "2291326430",
"FeedType": "_POST_PRODUCT_DATA_",
"SubmittedDate": 2009-02-20T02:10:35.000Z,
},
},
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": 2020-04-06T10:22:23.582Z,
"requestId": "0",
"timestamp": 2020-05-06T09:22:23.582Z,
},
]
`;
25 changes: 24 additions & 1 deletion test/unit/feeds.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import { ParsingError } from '../../src'
import { ParsingError, SubmitFeedParameters } from '../../src'
import { NextToken } from '../../src/parsing'
import { createMockHttpClient, mockMwsFail, parsingError } from '../utils'

describe('feeds', () => {
describe('submitFeed', () => {
const parameters: SubmitFeedParameters = {
FeedContent: '',
FeedType: '_POST_PRODUCT_DATA_',
}

it('returns feed info of submitted feeds if succesful', async () => {
expect.assertions(1)

const mockSubmitFeed = createMockHttpClient('feeds_submit_feed')

expect(await mockSubmitFeed.feeds.submitFeed(parameters)).toMatchSnapshot()
})

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

await expect(() => mockMwsFail.feeds.submitFeed(parameters)).rejects.toStrictEqual(
new ParsingError(parsingError),
)
})
})

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

Expand Down

0 comments on commit 01e0659

Please sign in to comment.