MeV
MeV

Reputation: 3968

How to fetch all the data from RESTful APIs?

I have implemented a RESTful API with few resources, for example:

/products/
/products/1
/products/2
/categories/
/categories/1
/categories/2
etc.

Now, I have been told that the app should mainly work offline, therefore I need to get all the data from the APIs and store it locally.

Since I am not providing a single chunk of data but there are different resources URI that needs to be called in order to get all the data I was wondering if this could be a problem.

How does this work? will there be many HTTP calls or one call will do everything?

What is the best approach in this case?

Upvotes: 0

Views: 2376

Answers (3)

Mike
Mike

Reputation: 11565

Setting up your endpoints in this way will allow for a user of your app to retrieve a single product/category or a list of products/categories.

Here's what each of these API endpoints should do when they are called.

/products - returns a list of products
/categories - returns a list of categories
/products/:id - returns the product with the specified id
/categories/:id - returns the category with the specific id

As far as allowing the app to work mostly offline. The best approach is to do some caching on the client (app) side. Every time a call is made to one of these endpoints for the first time, the result should be stored somewhere on the client side. The next time that same request is made, the data has already been retrieved previously, so therefore no network call needs to be made and the app will work offline. However, the first call needs to have a network connection to be made.

This can be implemented with a dictionary, where the key is the request (/products, /categories/1, etc.) and the value is the result that is returned from the API request. Every time a request is made, your app should first check if the data exists already on the client side. If it does it does not need to make a network call to get it and can just return the data that's present on the cleint.

Upvotes: 1

Doro
Doro

Reputation: 2413

Each URI represents single peace of data. The main idea of REST, instead of having randomly named setter and getter URLs and using GET for all the getters and POST for all the setters, we try to have the URLs identify resources, and then use the HTTP actions GET, POST, PUT and DELETE to do stuff to them.

So, using AFNetworking, for example, you get all benefits of this architecture. Download model could look like:

  1. Ask server for specified resource by get request
  2. save response in background thread
  3. ask for new peace of data

Of course, if you do not have ability to make new endpoint, that will download all stub, you must download it separately for each:

/products/
/products/1
/products/2
/categories/
/categories/1
/categories/2

Upvotes: 1

Robert Moskal
Robert Moskal

Reputation: 22553

Are these endpoints in themselves?

/products
/categories

It's a pretty well established convention for those to return the entire collection. You could even add some request parameters for filtering etc.

Upvotes: 1

Related Questions