From b3149d8df74382bf78dc091bce50703c5436ed66 Mon Sep 17 00:00:00 2001 From: Roy Calderon Date: Wed, 8 Mar 2017 18:12:56 -0400 Subject: [PATCH] AWS Endpoint connected. Main details extracted and exposed. The options should be mapped accordingly, So they could be displayed and selected. --- bower.json | 4 +- stitch-order-form.html | 18 ++--- stitch-order-request.html | 146 +++++++++++++------------------------- 3 files changed, 60 insertions(+), 108 deletions(-) diff --git a/bower.json b/bower.json index f239ea2..42486ac 100644 --- a/bower.json +++ b/bower.json @@ -9,8 +9,8 @@ "iron-image": "PolymerElements/iron-image#^1.2.5", "paper-button": "PolymerElements/paper-button#^1.0.14", "vaadin-combo-box": "vaadin/vaadin-combo-box#^1.3.2", - "jsSHA": "jssha#^2.2.0", - "iron-ajax": "PolymerElements/iron-ajax#^1.4.3" + "iron-ajax": "PolymerElements/iron-ajax#^1.4.3", + "paper-spinner": "PolymerElements/paper-spinner#^1.2.1" }, "devDependencies": { "iron-component-page": "PolymerElements/iron-component-page#^1.0.0", diff --git a/stitch-order-form.html b/stitch-order-form.html index 52c87da..e2cbcc4 100644 --- a/stitch-order-form.html +++ b/stitch-order-form.html @@ -3,6 +3,7 @@ + @@ -56,11 +57,13 @@ .left iron-image { display: block; - width: 150px; - height: 150px; + width: 160px; + height: 160px; + @apply(--shadow-elevation-4dp); } .left .checkPrime { + margin: 15px; height: 29px; } @@ -112,19 +115,18 @@

Order Product

+ +
- - - + the Prime Logo with a checkmark
-

[[productDetails.Title]]

+

[[productDetails.title]]

Select Options

@@ -156,7 +158,7 @@

Shipping information

Total Cost

-
$ [[productDetails.cost]] + shipping
+
[[productDetails.cost]] + shipping

(Note: shipping is calculated when purchase is made)

diff --git a/stitch-order-request.html b/stitch-order-request.html index 51ca1e5..6a22509 100644 --- a/stitch-order-request.html +++ b/stitch-order-request.html @@ -3,7 +3,9 @@ @@ -21,7 +23,7 @@ */ /** - * Fired when a response is received. + * Fired when a response is received and parsed. * * @event order-response */ @@ -33,24 +35,13 @@ */ properties: { - /** - * The Amazon Access Key Id. - */ - accessKeyID: String, - /** - * The Amazon Associate Tag. - */ - associateTag: String, - /** - * The Amazon Secret Access Key. - * Required - */ - secretAccessKey: String, - /** * The URL target of the request. */ - productUrl: String, + productUrl: { + type: String, + observer: '_setRequestParams' + }, /** * productUrl's response. @@ -65,108 +56,67 @@ */ productDetails: { type: Object, - notify: true + notify: true, + value: {} } }, - observers: [ - '_setRequestParams(productUrl, accessKeyID, associateTag, secretAccessKey)' - ], + ready: function(){ + this.request = (function(){ + var endpoint = 'https://kc5nsidmrk.execute-api.us-west-2.amazonaws.com/production/item/lookup'; - attached: function(){ - this.secretAccessKey = 'qSgTh1+jiDkga1Tt/ql8EcnS/ze6gHP6W+oqHlQn'; - this.associateTag = 'buttonjoy-20'; - this.accessKeyID = 'AKIAIO5T5P757JAH47QA'; + var body = { + itemId: undefined, //Required, extracted from amazon product URL + ResponseGroup: 'Images,ItemAttributes,OfferListings,Offers' + }; + + return { + body: function(itemId){ + var request = JSON.parse(JSON.stringify(body)); + request.itemId = itemId; + + return request; + }, + get endpoint(){ + return endpoint; + } + }; + })(); }, - _setRequestParams: function(productUrl, accessKeyID, associateTag, secretAccessKey){ + _setRequestParams: function(productUrl){ this.debounce('requesting-product', function(){ - if (productUrl.length > 23 && accessKeyID && associateTag && secretAccessKey) { + if (productUrl.length > 23) { var ajax = this.$$('iron-ajax'); var itemID = this._getItemIdFromURL(productUrl); - var query = _service.paramsAsQuery(itemID, accessKeyID, associateTag); - var url = this._signedUrl(_service.url(), query, secretAccessKey); - ajax.set('url', url); - var request = ajax.generateRequest(); - this.fire('order-request', {url: url, itemId: itemID}); + ajax.set('body', this.request.body(itemID)); + ajax.generateRequest(); + + this.fire('order-request', {itemId: itemID}); } }, 300); }, _handleResponse: function(e){ - var response = e.detail.response; - console.log(response); - this.fire('order-response', response); + var response = e.detail.response[0]; + var product = { + id: response.ASIN[0], + title: response.ItemAttributes[0].Title[0], + image: response.MediumImage[0].URL[0], + cost: response.ItemAttributes[0].ListPrice[0].FormattedPrice[0], + prime: !!Number(response.Offers[0].Offer[0].OfferListing[0].IsEligibleForPrime[0]) + }; + + this.set('productDetails', product); + this.fire('order-response', product); }, _getItemIdFromURL: function(url){ - var regex = /\/dp\/(\w+)\//; + var regex = /\/dp\/(\w+)\/?/; return regex.exec(url)[1]; - }, - _signString: function(stringToSignParam, secret){ - var shaObj = new jsSHA('SHA-256', 'TEXT'); - shaObj.setHMACKey(secret, 'TEXT'); - shaObj.update(stringToSignParam); - var signature = shaObj.getHMAC('B64'); - return encodeURIComponent(signature); - }, - _signedUrl: function(url, canonicalizedQueryString, secret){ - var parser = document.createElement('a'); - parser.href = url; - var hostname = parser.hostname; - var pathname = parser.pathname; - var stringToSign = ['GET', hostname, pathname, canonicalizedQueryString].join('\n'); - - return url + '?' + [canonicalizedQueryString, 'Signature=' + this._signString(stringToSign, secret)].join('&'); } }); - - var _service = (function(){ - var awsUrl = 'http://webservices.amazon.com/onca/xml'; - - var params = { - Service: 'AWSECommerceService', - Operation: 'ItemLookup', - IdType: 'ASIN', - ResponseGroup: ['Images', 'ItemAttributes', 'Offers'] - }; - - return { - params: function(itemID, accessKeyID, associateTag){ - var requestParams = JSON.parse(JSON.stringify(params)); - requestParams.ItemId = itemID; - requestParams.AWSAccessKeyId = accessKeyID; - requestParams.AssociateTag = associateTag; - - return requestParams; - }, - query: function(params, timestampParam){ - var query = []; - for (var param in params) { - if (params.hasOwnProperty(param)) { - query.push(param + '=' + params[param]); - } - } - - var tiemstamp = timestampParam || new Date().toISOString(); - query = query.join('&').replace(/,/g, '%2C').split('&'); - query.push('Timestamp=' + encodeURIComponent(tiemstamp)); - - return query.sort().join('&'); - }, - paramsAsQuery: function(itemID, accessKeyID, associateTag, timestampParam){ - return this.query(this.params(itemID, accessKeyID, associateTag, timestampParam)); - }, - url: function(query){ - if (query instanceof Object) { - query = this.query(query); - } - - return awsUrl + (query ? '?' + query : ''); - } - } - })(); })();