API Class with intensive network requests
Posted
by
Marco Acierno
on Programmers
See other posts from Programmers
or by Marco Acierno
Published on 2014-08-18T14:58:11Z
Indexed on
2014/08/18
16:43 UTC
Read the original article
Hit count: 283
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 urlsDownload 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)
© Programmers or respective owner