From e01272e1ccb1d7fdb54a7dbcf69b791164b7ba83 Mon Sep 17 00:00:00 2001 From: MarijnGroen <56343501+MarijnGroen@users.noreply.github.com> Date: Wed, 7 Oct 2020 12:06:25 +0200 Subject: [PATCH 1/9] added basic auth --- index.js | 3 ++- lib/OpenCatalog/service.js | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 69dff06..56987b6 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,8 @@ const openCatalog = require('./lib/OpenCatalog/service'); const icecat = function (username, password, httpUrl = 'data.icecat.biz/xml_s3/xml_server3.cgi') { this.VERSION = packagejson.version; this.scheme = 'https://'; - this.httpAuth = username + ':' + encodeURIComponent(password); + this.username = username; + this.password = password; this.httpUrl = httpUrl; this.openCatalog = new openCatalog(this); }; diff --git a/lib/OpenCatalog/service.js b/lib/OpenCatalog/service.js index d70be75..4ffd790 100644 --- a/lib/OpenCatalog/service.js +++ b/lib/OpenCatalog/service.js @@ -90,7 +90,7 @@ openCatalog.prototype._getProductByXMLdata = function(xmlData, httpRequestUrl) { * @param {string} lang */ openCatalog.prototype._getBaseUrl = function(lang) { - return `${this.icecat.scheme}${this.icecat.httpAuth}@${this.icecat.httpUrl}?lang=${lang};output=productxml`; + return `${this.icecat.scheme}${this.icecat.httpUrl}?lang=${lang};output=productxml`; }; /** @@ -100,8 +100,11 @@ openCatalog.prototype._getBaseUrl = function(lang) { * @returns {Promise} */ openCatalog.prototype._requestProduct = function(httpRequestUrl) { + return new Promise((resolve, reject) => { - const request = this.https.get(httpRequestUrl, (response) => { + const request = this.https.get(httpRequestUrl, { + 'Authorization': 'Basic ' + new Buffer(this.icecat.username + ':' + this.icecat.password).toString('base64') + }, (response) => { let body = ''; response.on('data', (chunk) => { From 20cbd2717b8143cf7b42bec621f0ea2ccf94c4c8 Mon Sep 17 00:00:00 2001 From: MarijnGroen <56343501+MarijnGroen@users.noreply.github.com> Date: Wed, 7 Oct 2020 12:14:42 +0200 Subject: [PATCH 2/9] added debug --- lib/OpenCatalog/service.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/OpenCatalog/service.js b/lib/OpenCatalog/service.js index 4ffd790..35f85f0 100644 --- a/lib/OpenCatalog/service.js +++ b/lib/OpenCatalog/service.js @@ -112,6 +112,7 @@ openCatalog.prototype._requestProduct = function(httpRequestUrl) { }); response.on('end', () => { + console.log(body); return resolve(this._getProductByXMLdata(body, httpRequestUrl)); }); }); From 6b07f4f5c9b7381cd1315ca3fecfcad9147c207b Mon Sep 17 00:00:00 2001 From: MarijnGroen <56343501+MarijnGroen@users.noreply.github.com> Date: Wed, 7 Oct 2020 12:18:41 +0200 Subject: [PATCH 3/9] Update service.js --- lib/OpenCatalog/service.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/OpenCatalog/service.js b/lib/OpenCatalog/service.js index 35f85f0..78ff09e 100644 --- a/lib/OpenCatalog/service.js +++ b/lib/OpenCatalog/service.js @@ -100,11 +100,15 @@ openCatalog.prototype._getBaseUrl = function(lang) { * @returns {Promise} */ openCatalog.prototype._requestProduct = function(httpRequestUrl) { + let options = { + url: httpRequestUrl, + headers: { + 'Authorization': 'Basic ' + new Buffer(this.icecat.username + ':' + this.icecat.password).toString('base64') + } + } return new Promise((resolve, reject) => { - const request = this.https.get(httpRequestUrl, { - 'Authorization': 'Basic ' + new Buffer(this.icecat.username + ':' + this.icecat.password).toString('base64') - }, (response) => { + const request = this.https.get(options, (response) => { let body = ''; response.on('data', (chunk) => { From 9bfd5b0435b8a5fb38e3533a103650e49c26a98f Mon Sep 17 00:00:00 2001 From: MarijnGroen <56343501+MarijnGroen@users.noreply.github.com> Date: Wed, 7 Oct 2020 12:22:28 +0200 Subject: [PATCH 4/9] Update service.js --- lib/OpenCatalog/service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OpenCatalog/service.js b/lib/OpenCatalog/service.js index 78ff09e..3b70f53 100644 --- a/lib/OpenCatalog/service.js +++ b/lib/OpenCatalog/service.js @@ -101,7 +101,7 @@ openCatalog.prototype._getBaseUrl = function(lang) { */ openCatalog.prototype._requestProduct = function(httpRequestUrl) { let options = { - url: httpRequestUrl, + host: httpRequestUrl, headers: { 'Authorization': 'Basic ' + new Buffer(this.icecat.username + ':' + this.icecat.password).toString('base64') } From d29a7dc0bda78bf7d8a54e90442f9f7d35e3c250 Mon Sep 17 00:00:00 2001 From: MarijnGroen <56343501+MarijnGroen@users.noreply.github.com> Date: Wed, 7 Oct 2020 12:38:53 +0200 Subject: [PATCH 5/9] added more config options --- index.js | 7 ++++--- lib/OpenCatalog/service.js | 13 +++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 56987b6..759790b 100644 --- a/index.js +++ b/index.js @@ -11,12 +11,13 @@ const openCatalog = require('./lib/OpenCatalog/service'); * @prototype * @class Icecat */ -const icecat = function (username, password, httpUrl = 'data.icecat.biz/xml_s3/xml_server3.cgi') { +const icecat = function (username, password, httpUri = 'data.icecat.biz', path = '/xml_s3/xml_server3.cgi') { this.VERSION = packagejson.version; - this.scheme = 'https://'; + this.port = 443; this.username = username; this.password = password; - this.httpUrl = httpUrl; + this.httpUri = httpUri; + this.path = path; this.openCatalog = new openCatalog(this); }; diff --git a/lib/OpenCatalog/service.js b/lib/OpenCatalog/service.js index 3b70f53..612ced2 100644 --- a/lib/OpenCatalog/service.js +++ b/lib/OpenCatalog/service.js @@ -21,7 +21,7 @@ const openCatalog = function(instance) { * @returns {Promise} */ openCatalog.prototype.getProduct = function(lang, GTIN) { - const httpRequestUrl = this._getBaseUrl(lang) + ';ean_upc=' + GTIN; + const httpRequestUrl = this.getPath(lang) + ';ean_upc=' + GTIN; return this._requestProduct(httpRequestUrl); }; @@ -33,7 +33,7 @@ openCatalog.prototype.getProduct = function(lang, GTIN) { * @param {integer} productId */ openCatalog.prototype.getProductById = function(lang, productId) { - const httpRequestUrl = this._getBaseUrl(lang) + ';product_id=' + productId; + const httpRequestUrl = this.getPath(lang) + ';product_id=' + productId; return this._requestProduct(httpRequestUrl); }; @@ -48,7 +48,7 @@ openCatalog.prototype.getProductById = function(lang, productId) { * @param {string} sku */ openCatalog.prototype.getProductBySKU = function(lang, brand, sku) { - const httpRequestUrl = this._getBaseUrl(lang) + ';prod_id=' + sku + ';vendor=' + brand; + const httpRequestUrl = this.getPath(lang) + ';prod_id=' + sku + ';vendor=' + brand; return this._requestProduct(httpRequestUrl); }; @@ -89,8 +89,8 @@ openCatalog.prototype._getProductByXMLdata = function(xmlData, httpRequestUrl) { * * @param {string} lang */ -openCatalog.prototype._getBaseUrl = function(lang) { - return `${this.icecat.scheme}${this.icecat.httpUrl}?lang=${lang};output=productxml`; +openCatalog.prototype._getPath = function(lang) { + return `${this.icecat.path}?lang=${lang};output=productxml`; }; /** @@ -101,7 +101,8 @@ openCatalog.prototype._getBaseUrl = function(lang) { */ openCatalog.prototype._requestProduct = function(httpRequestUrl) { let options = { - host: httpRequestUrl, + host: this.icecat.httpUri, + path: httpRequestUrl, headers: { 'Authorization': 'Basic ' + new Buffer(this.icecat.username + ':' + this.icecat.password).toString('base64') } From 8d5b578eb7caab49d96285f352d54564870a3789 Mon Sep 17 00:00:00 2001 From: MarijnGroen <56343501+MarijnGroen@users.noreply.github.com> Date: Wed, 7 Oct 2020 12:41:53 +0200 Subject: [PATCH 6/9] fixed typo --- lib/OpenCatalog/service.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/OpenCatalog/service.js b/lib/OpenCatalog/service.js index 612ced2..52ddf1a 100644 --- a/lib/OpenCatalog/service.js +++ b/lib/OpenCatalog/service.js @@ -21,7 +21,7 @@ const openCatalog = function(instance) { * @returns {Promise} */ openCatalog.prototype.getProduct = function(lang, GTIN) { - const httpRequestUrl = this.getPath(lang) + ';ean_upc=' + GTIN; + const httpRequestUrl = this._getPath(lang) + ';ean_upc=' + GTIN; return this._requestProduct(httpRequestUrl); }; @@ -33,7 +33,7 @@ openCatalog.prototype.getProduct = function(lang, GTIN) { * @param {integer} productId */ openCatalog.prototype.getProductById = function(lang, productId) { - const httpRequestUrl = this.getPath(lang) + ';product_id=' + productId; + const httpRequestUrl = this._getPath(lang) + ';product_id=' + productId; return this._requestProduct(httpRequestUrl); }; @@ -48,7 +48,7 @@ openCatalog.prototype.getProductById = function(lang, productId) { * @param {string} sku */ openCatalog.prototype.getProductBySKU = function(lang, brand, sku) { - const httpRequestUrl = this.getPath(lang) + ';prod_id=' + sku + ';vendor=' + brand; + const httpRequestUrl = this._getPath(lang) + ';prod_id=' + sku + ';vendor=' + brand; return this._requestProduct(httpRequestUrl); }; From 4a644e1d0305700e3fa7314cf15c2815322e87b2 Mon Sep 17 00:00:00 2001 From: MarijnGroen <56343501+MarijnGroen@users.noreply.github.com> Date: Wed, 7 Oct 2020 12:44:06 +0200 Subject: [PATCH 7/9] remove extra debug --- lib/OpenCatalog/service.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/OpenCatalog/service.js b/lib/OpenCatalog/service.js index 52ddf1a..8ff01b4 100644 --- a/lib/OpenCatalog/service.js +++ b/lib/OpenCatalog/service.js @@ -117,7 +117,6 @@ openCatalog.prototype._requestProduct = function(httpRequestUrl) { }); response.on('end', () => { - console.log(body); return resolve(this._getProductByXMLdata(body, httpRequestUrl)); }); }); From 63d33702a2465a08feb73bf101c53e401c8479ec Mon Sep 17 00:00:00 2001 From: MarijnGroen <56343501+MarijnGroen@users.noreply.github.com> Date: Wed, 7 Oct 2020 13:03:06 +0200 Subject: [PATCH 8/9] add check for extra description --- lib/OpenCatalog/product.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/OpenCatalog/product.js b/lib/OpenCatalog/product.js index 7782a9c..7fed0d0 100644 --- a/lib/OpenCatalog/product.js +++ b/lib/OpenCatalog/product.js @@ -84,7 +84,10 @@ icecat.prototype.getReleaseDate = function() { */ icecat.prototype.getLongDescription = function() { try { - return this.productData.ProductDescription[0].$.LongDesc; + if(!this.productData.ProductDescription[0].$.LongDesc){ + return this.productData.SummaryDescription[0].$.LongSummaryDescitpion + } + return this.productData.ProductDescription[0].$.LongDesc } catch (e) { return false; } @@ -96,6 +99,9 @@ icecat.prototype.getLongDescription = function() { */ icecat.prototype.getShortDescription = function() { try { + if(!this.productData.ProductDescription[0].$.ShortDesc){ + return this.productData.SummaryDescription[0].$.ShortSummaryDescitpion + } return this.productData.ProductDescription[0].$.ShortDesc; } catch (e) { return false; From f89c89d7d90b5b929c91fb7758d927b3b93c6f30 Mon Sep 17 00:00:00 2001 From: MarijnGroen <56343501+MarijnGroen@users.noreply.github.com> Date: Wed, 7 Oct 2020 13:25:57 +0200 Subject: [PATCH 9/9] fixed the llocationi of the extra description --- lib/OpenCatalog/product.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/OpenCatalog/product.js b/lib/OpenCatalog/product.js index 7fed0d0..28e2880 100644 --- a/lib/OpenCatalog/product.js +++ b/lib/OpenCatalog/product.js @@ -84,8 +84,8 @@ icecat.prototype.getReleaseDate = function() { */ icecat.prototype.getLongDescription = function() { try { - if(!this.productData.ProductDescription[0].$.LongDesc){ - return this.productData.SummaryDescription[0].$.LongSummaryDescitpion + if(typeof this.productData.ProductDescription[0].$ === 'undefined'){ + return this.productData.SummaryDescription[0].LongSummaryDescription[0]._ } return this.productData.ProductDescription[0].$.LongDesc } catch (e) { @@ -99,8 +99,8 @@ icecat.prototype.getLongDescription = function() { */ icecat.prototype.getShortDescription = function() { try { - if(!this.productData.ProductDescription[0].$.ShortDesc){ - return this.productData.SummaryDescription[0].$.ShortSummaryDescitpion + if(typeof this.productData.ProductDescription[0].$ === 'undefined'){ + return this.productData.SummaryDescription[0].ShortSummaryDescitpion[0]._ } return this.productData.ProductDescription[0].$.ShortDesc; } catch (e) { @@ -268,4 +268,4 @@ icecat.prototype.getCategoryFeatureGroups = function() { } }; -module.exports = icecat; +module.exports = icecat; \ No newline at end of file