API Class with intensive network requests
- by Marco Acierno
I'm working an API which works as "intermediary" between a REST API and the developer.
In this way, when the programmer do something like this:
User user = client.getUser(nickname);
it will execute a network request to download from the service the data about the user and then the programmer can use the data by doing things like
user.getLocation();
user.getDisplayName();
and so on.
Now there are some methods like getFollowers() which execute another network request and i could do it in two ways:
Download all the data in the getUser method (and not only the most important) but in this way the request time could be very long since it should execute the request to various urls
Download the data when the user calls the method, it looks like the best way and to improve it i could cache the result so the next call to getFollowers returns immediately with the data already download instead of execute again the request.
What is the best way? And i should let methods like getUser and getFollowers stop the code execution until the data is ready or i should implement a callback so when the data is ready the callback gets fired? (this looks like Javascript)