Skip to content

Instantly share code, notes, and snippets.

@jg75
Created July 10, 2018 20:06
Show Gist options
  • Save jg75/4ca97e6fcd3403b76ffff96ca8120ce9 to your computer and use it in GitHub Desktop.
Save jg75/4ca97e6fcd3403b76ffff96ca8120ce9 to your computer and use it in GitHub Desktop.
const https = require('https');
function getProductsByBrand(brand, limit, offset) {
let params = '?limit=' + limit.toString() + '&offset=' + offset.toString();
let path = '/brands/' + brand + '/products' + params;
let options = {host: host, path: path, auth: auth}
https.get(options, (response) => {
data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
products = JSON.parse(data)
if (products.length > 0) {
console.log(products)
getProductsByBrand(brand, limit, offset + limit)
}
});
}).on('error', (error) => {
console.log(error.message)
});
}
function getProducts(host, auth) {
let options = {host: host, path: '/brands', auth: auth}
https.get(options, (response) => {
data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
JSON.parse(data).forEach((value, index, array) =>{
brand = value
getProductsByBrand(brand, 1000, 0);
});
});
}).on('error', (error) => {
console.log(error.message)
});
}
const host = 'api.amberengine.com';
const auth = '<your email>:<your password>';
getProducts(host, auth);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment