Created
July 10, 2018 20:06
-
-
Save jg75/4ca97e6fcd3403b76ffff96ca8120ce9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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