Skip to content

Instantly share code, notes, and snippets.

@bendtherules
Last active March 19, 2019 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendtherules/27438fb610aef01df80554962722e97f to your computer and use it in GitHub Desktop.
Save bendtherules/27438fb610aef01df80554962722e97f to your computer and use it in GitHub Desktop.
Slow down getStatus request
// change to slow down get order status
module.exports = {
summary: 'slow down placing order (https)',
*beforeDealHttpsRequest(requestDetail) {
return true;
},
*beforeSendRequest(requestDetail) {
const newRequestOptions = requestDetail.requestOptions;
// set rejectUnauthorized as false
newRequestOptions.rejectUnauthorized = false;
return {
requestOptions: newRequestOptions
};
},
*beforeSendResponse(requestDetail, responseDetail) {
if (requestDetail.url.startsWith('https://api-dev-2.smallcase.com/user/sc/orders')) {
console.log('Handling url: ' +JSON.stringify(requestDetail.url));
const newResponse = responseDetail.response;
let timeoutMs = 0;
try {
const parsedJSON = JSON.parse(newResponse.body);
if (parsedJSON.data instanceof Array) {
const lastBatch = parsedJSON.data[0].lastBatch || parsedJSON.data[0].batches[0];
if (lastBatch.status !== "PLACED"){
timeoutMs = (5 + Math.round(Math.random()*30)) * 1000;
}
}
}catch (e) {
}
newResponse.header['DELAYED-BY-MS'] = timeoutMs.toString();
return new Promise((resolve, reject) => {
setTimeout(() => { // delay
resolve({ response: newResponse });
}, timeoutMs);
});
}
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment