Skip to content

Instantly share code, notes, and snippets.

@mksglu
Created December 12, 2019 17:38
Show Gist options
  • Save mksglu/3ad0ddbf2b9f1393430aaae6106846a8 to your computer and use it in GitHub Desktop.
Save mksglu/3ad0ddbf2b9f1393430aaae6106846a8 to your computer and use it in GitHub Desktop.
fetch("https://www.hepsiburada.com/siparislerim/api/orders?skip=0")
.then(response => response.json())
.then(result => {
var maxSkip = result.MaxSkip;
var increase = 10;
if (maxSkip === 0) {
var totalAmount = 0;
for (let index = 0; index < result.Orders.length; index++) {
totalAmount += parseInt(result.Orders[index].TotalAmount.Value);
}
console.log(
"Toplam Hepsiburada.com harcamaniz: ",
totalAmount.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, "$&,"),
"TL"
);
} else {
let apiRequestLoop = function(maxSkip, increase) {
let promiseArray = [];
for (let k = 0; k < maxSkip + increase; k += increase) {
promiseArray.push(
fetch(
`https://www.hepsiburada.com/siparislerim/api/orders?skip=${k}`
)
.then(_response => _response.json())
.then(_result => {
var currentPeriodTotalAmount = 0;
for (let j = 0; j < _result.Orders.length; j++) {
var currentPeriodAmount = parseInt(
_result.Orders[j].TotalAmount.Value
);
currentPeriodTotalAmount += currentPeriodAmount;
}
return currentPeriodTotalAmount;
})
);
}
return Promise.all(promiseArray);
};
apiRequestLoop(maxSkip, increase).then(r => {
var result = r
.reduce(function(a, b) {
return a + b;
}, 0)
.toFixed(2)
.replace(/\d(?=(\d{3})+\.)/g, "$&,");
console.log("Toplam Hepsiburada.com harcamaniz: ", result);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment