Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve requests #9

Closed
glethuillier opened this issue Feb 25, 2021 · 0 comments
Closed

Improve requests #9

glethuillier opened this issue Feb 25, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@glethuillier
Copy link
Contributor

Currently, the requests are synchronous and the underlying implementation does not properly handle exceptions (no retry, a network error harshly throws an un-recoverable error, no JSON parsing exceptions handling, etc.).

xpub-scan/src/helpers.ts

Lines 13 to 35 in 3b64b7f

function getJSON(url: string, APIKey?: string) {
let headers = {};
if (APIKey !== undefined) {
headers = {
'X-API-Key': APIKey
}
}
const res = request('GET', url, {headers: headers} );
if (res.statusCode !== 200) {
console.log(chalk.red('GET request error'));
throw new Error(
"GET REQUEST ERROR: "
.concat(url)
.concat(", Status Code: ")
.concat(String(res.statusCode))
);
}
return JSON.parse(res.getBody('utf-8'));
}

This basic implementation could be greatly improved by:

  • Performing async requests using a proper module (e.g. axios)
  • Adding a retry mechanism
  • Making the headers more flexible (to handle different external providers)
  • Taking into account the limitations from external APIs (e.g. by ensuring some delay between each request)

From the user perspective, the new implementation should:

  • Not be noticeable when operating optimally (that is: when no network error and no exception): the derived addresses should be sorted by a) address type (legacy, Segwit), then, b) by account number and c) index. They also should be displayed one by one rather than at once, at the end of the whole analysis.
  • When recoverable exceptions occur, a transient error message should be displayed (e.g. "Network issue, retrying...") and be removed once the exception had been properly handled.
  • When an un-recoverable exception occurs, the reason should be clearly explained to the user (e.g. "Network error: authentication issue", "JSON parsing error: {raw response body}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant