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

Commit

Permalink
feat: get matching products for id api
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed May 28, 2020
1 parent 0d06593 commit b81f6f1
Show file tree
Hide file tree
Showing 2 changed files with 310 additions and 3 deletions.
59 changes: 56 additions & 3 deletions src/sections/products.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
array,
boolean,
Codec,
exactly,
Expand Down Expand Up @@ -145,7 +146,7 @@ const GetMyFeesEstimateResponse = Codec.interface({
}),
})

type GetMyFeesEstimateResponse = GetInterface<typeof GetMyFeesEstimate>
type GetMyFeesEstimate = GetInterface<typeof GetMyFeesEstimate>

interface ListMatchingProductsRequestParameters {
MarketplaceId: string
Expand Down Expand Up @@ -186,6 +187,33 @@ const GetMatchingProductResponse = Codec.interface({

type MatchingProduct = GetInterface<typeof MatchingProduct>

interface GetMatchingProductForIdParameters {
MarketplaceId: string
IdType: string
IdList: string[]
[key: string]: string | string[]
}

const MatchingProductForId = Codec.interface({
Products: ensureArray('Product', Product),
})

// GetMatchingProductForId is different than other APIs,
// The example on MWS points to it possibly being an array structured this way
const GetMatchingProductForIdResult = oneOf([
MatchingProductForId,
array(MatchingProductForId),
string,
])

const GetMatchingProductForIdResponse = Codec.interface({
GetMatchingProductForIdResponse: Codec.interface({
GetMatchingProductForIdResult,
}),
})

type GetMatchingProductForIdResult = GetInterface<typeof GetMatchingProductForIdResult>

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

Expand All @@ -209,7 +237,7 @@ export class Products {

async getMyFeesEstimate(
parameters: GetMyFeesEstimateParameters,
): Promise<[GetMyFeesEstimateResponse, RequestMeta]> {
): Promise<[GetMyFeesEstimate, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Products,
version: PRODUCTS_API_VERSION,
Expand All @@ -225,14 +253,21 @@ export class Products {
})
}

/**
*
* @todo Think about converting GetMatchingProductResposne to array to match getMatchingProductForId
*/
async getMatchingProduct(
parameters: GetMatchingProductParameters,
): Promise<[MatchingProduct, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Products,
version: PRODUCTS_API_VERSION,
action: 'GetMatchingProduct',
parameters,
parameters: {
'ASINList.ASIN': parameters.ASINList,
MarketplaceId: parameters.MarketplaceId,
},
})

return GetMatchingProductResponse.decode(response).caseOf({
Expand All @@ -242,4 +277,22 @@ export class Products {
},
})
}

async getMatchingProductForId(
parameters: GetMatchingProductForIdParameters,
): Promise<[GetMatchingProductForIdResult, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Products,
version: PRODUCTS_API_VERSION,
action: 'GetMatchingProductForId',
parameters,
})

return GetMatchingProductForIdResponse.decode(response).caseOf({
Right: (x) => [x.GetMatchingProductForIdResponse.GetMatchingProductForIdResult, meta],
Left: (error) => {
throw new ParsingError(error)
},
})
}
}
254 changes: 254 additions & 0 deletions test/unit/__snapshots__/products.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,260 @@ Array [
]
`;

exports[`products getMatchingProductForId returns a matching product when the response is valid 1`] = `
Array [
Array [
Object {
"Products": Array [
Object {
"AttributeSets": Object {
"ns2:ItemAttributes": Object {
"ns2:Author": Array [
"Marmanis, Haralambos",
"Babenko, Dmitry",
],
"ns2:Binding": "Paperback",
"ns2:Edition": 1,
"ns2:IsEligibleForTradeIn": true,
"ns2:ItemDimensions": Object {
"ns2:Height": 9.17,
"ns2:Length": 7.36,
"ns2:Weight": 1.4,
"ns2:Width": 0.75,
},
"ns2:Label": "Manning Publications",
"ns2:Languages": Object {
"ns2:Language": Array [
Object {
"ns2:Name": "english",
"ns2:Type": "Unknown",
},
Object {
"ns2:Name": "english",
"ns2:Type": "Original Language",
},
Object {
"ns2:Name": "english",
"ns2:Type": "Published",
},
],
},
"ns2:ListPrice": Object {
"ns2:Amount": 44.99,
"ns2:CurrencyCode": "USD",
},
"ns2:Manufacturer": "Manning Publications",
"ns2:NumberOfItems": 1,
"ns2:NumberOfPages": 368,
"ns2:PackageDimensions": Object {
"ns2:Height": 0.8,
"ns2:Length": 9.1,
"ns2:Weight": 1.35,
"ns2:Width": 7.3,
},
"ns2:ProductGroup": "Book",
"ns2:ProductTypeName": "ABIS_BOOK",
"ns2:PublicationDate": "2009-07-05",
"ns2:Publisher": "Manning Publications",
"ns2:SmallImage": Object {
"ns2:Height": 75,
"ns2:URL": "http://ecx.images-amazon.com/images/I/51EEz05N2HL._SL75_.jpg",
"ns2:Width": 60,
},
"ns2:Studio": "Manning Publications",
"ns2:Title": "Algorithms of the Intelligent Web",
},
},
"Identifiers": Object {
"MarketplaceASIN": Object {
"ASIN": 1933988665,
"MarketplaceId": "ATVPDKIKX0DER",
},
},
"Relationships": "",
"SalesRankings": Object {
"SalesRank": Array [
Object {
"ProductCategoryId": "book_display_on_website",
"Rank": 59485,
},
Object {
"ProductCategoryId": 377886011,
"Rank": 32,
},
Object {
"ProductCategoryId": 3887,
"Rank": 66,
},
Object {
"ProductCategoryId": 3870,
"Rank": 82,
},
],
},
},
],
},
Object {
"Products": Array [
Object {
"AttributeSets": Object {
"ns2:ItemAttributes": Object {
"ns2:Author": "Rowling, J.K.",
"ns2:Binding": "Paperback",
"ns2:Brand": "Scholastic Press",
"ns2:Creator": "GrandPré, Mary",
"ns2:Edition": "1st",
"ns2:Feature": "Recommended Age: 9 years and up",
"ns2:IsAutographed": false,
"ns2:IsMemorabilia": false,
"ns2:ItemDimensions": Object {
"ns2:Height": 0.8,
"ns2:Length": 7.5,
"ns2:Weight": 0.5,
"ns2:Width": 5.2,
},
"ns2:Label": "Scholastic Paperbacks",
"ns2:Languages": Object {
"ns2:Language": Array [
Object {
"ns2:Name": "english",
"ns2:Type": "Unknown",
},
Object {
"ns2:Name": "english",
"ns2:Type": "Original Language",
},
Object {
"ns2:Name": "english",
"ns2:Type": "Published",
},
],
},
"ns2:ListPrice": Object {
"ns2:Amount": 10.99,
"ns2:CurrencyCode": "USD",
},
"ns2:Manufacturer": "Scholastic Paperbacks",
"ns2:NumberOfItems": 1,
"ns2:NumberOfPages": 320,
"ns2:PackageDimensions": Object {
"ns2:Height": 1,
"ns2:Length": 7.5,
"ns2:Weight": 0.5,
"ns2:Width": 5.2,
},
"ns2:PackageQuantity": 1,
"ns2:PartNumber": 9780590353427,
"ns2:ProductGroup": "Book",
"ns2:ProductTypeName": "ABIS_BOOK",
"ns2:PublicationDate": "1999-10-01",
"ns2:Publisher": "Scholastic Paperbacks",
"ns2:ReleaseDate": "1999-09-08",
"ns2:SmallImage": Object {
"ns2:Height": 75,
"ns2:URL": "http://ecx.images-amazon.com/images/I/51MU5VilKpL._SL75_.jpg",
"ns2:Width": 51,
},
"ns2:Studio": "Scholastic Paperbacks",
"ns2:Title": "Harry Potter and the Sorcerer's Stone (Book 1)",
},
},
"Identifiers": Object {
"MarketplaceASIN": Object {
"ASIN": "059035342X",
"MarketplaceId": "ATVPDKIKX0DER",
},
},
"Relationships": "",
"SalesRankings": Object {
"SalesRank": Array [
Object {
"ProductCategoryId": "book_display_on_website",
"Rank": 362,
},
Object {
"ProductCategoryId": 15356791,
"Rank": 6,
},
Object {
"ProductCategoryId": 3153,
"Rank": 9,
},
Object {
"ProductCategoryId": 3045,
"Rank": 14,
},
],
},
},
Object {
"AttributeSets": Object {
"ns2:ItemAttributes": Object {
"ns2:Author": "J.K. Rowling, Mary GrandPré",
"ns2:Binding": "Paperback",
"ns2:Label": "Scholastic Paperbacks",
"ns2:Languages": Object {
"ns2:Language": Array [
Object {
"ns2:Name": "english",
"ns2:Type": "Unknown",
},
Object {
"ns2:Name": "english",
"ns2:Type": "Published",
},
],
},
"ns2:Manufacturer": "Scholastic Paperbacks",
"ns2:NumberOfItems": 1,
"ns2:NumberOfPages": 784,
"ns2:PackageDimensions": Object {
"ns2:Height": 0.73,
"ns2:Length": 8.43,
"ns2:Weight": 0.79,
"ns2:Width": 5.85,
},
"ns2:ProductGroup": "Book",
"ns2:ProductTypeName": "BOOKS_1973_AND_LATER",
"ns2:PublicationDate": "1999-09-08",
"ns2:Publisher": "Scholastic Paperbacks",
"ns2:SmallImage": Object {
"ns2:Height": 75,
"ns2:URL": "http://ecx.images-amazon.com/images/I/51STfI7UiCL._SL75_.jpg",
"ns2:Width": 52,
},
"ns2:Studio": "Scholastic Paperbacks",
"ns2:Title": "Harry Potter and the Sorcerer's Stone",
},
},
"Identifiers": Object {
"MarketplaceASIN": Object {
"ASIN": 439708184,
"MarketplaceId": "ATVPDKIKX0DER",
},
},
"Relationships": "",
"SalesRankings": Object {
"SalesRank": Object {
"ProductCategoryId": "book_display_on_website",
"Rank": 2091024,
},
},
},
],
},
],
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": "2020-04-06T10:22:23.582Z",
"requestId": "0",
"timestamp": "2020-05-06T09:22:23.582Z",
},
]
`;

exports[`products getMyFeesEstimate returns a list fee estimates when the response is valid 1`] = `
Array [
Object {
Expand Down

0 comments on commit b81f6f1

Please sign in to comment.