-1

I am querying an URI to get some data from a web service. That is all fine. But I notice that my json hase page_count of multiple pages but only 1st page data is returned.

Here is how json could look like:

{
  "last_item": null,
  "total_items": "201",
  "first_item": null,
  "page_number": "1", **<-- THIS IS 1ST PAGE**
  "page_size": "10", 
  "page_items": null,
  "search_time": "0.045",
  "page_count": "21",  **<-- THERE IS 21 PAGES IN TOTAL**
  "cars": {
    "car": [
      { ... },
      { ... },
      ...
      ...
      ]
   }
}

How do I return all pages instead of returning only 1st page? I am aware that this might not be good idea but how do I do that?

UPDATE: The URI to web-service is something like:

http://<api_address>.com/json/cars/search?app_key=<api_key>&c=music&location=32.263569,-111.9847682

Thanks,

Ghasem
  • 14,455
  • 21
  • 138
  • 171
pixel
  • 9,653
  • 16
  • 82
  • 149
  • 1
    Can you give us some more information about how exactly you are querying? – Michael Schnerring Dec 20 '15 at 05:16
  • @ebeeb Added UPDATE section. Thanks ebeeb – pixel Dec 20 '15 at 05:21
  • 2
    Without knowing which API you are targeting it is difficult to guess what params you would send along with your request to adjust your results. This question might help http://stackoverflow.com/questions/12168624/pagination-response-payload-from-a-restful-api. – Fraser Crosbie Dec 20 '15 at 05:21
  • See in the documentation of the API, if this is even possible ... – mybirthname Dec 20 '15 at 05:47
  • It will be great if you give the name of site or you have to see their documentation. Without looking proper documentation how can someone answer your question. – Anik Saha Dec 20 '15 at 06:04

1 Answers1

1

I see you are using an app key.

So i guess the API you are using somehow limits the amount of queries and the amount of data per query.

I could imagine the query you were using always returns only one page of data, and additionally the total amount of pages available, to let you know how many more pages are available.

I guess if you don't specify the page you want the query returns the first page by default.

UPDATE

So if you can't set the page size per query at something greater than 50 this is an API-side restriction you (probably) cannot change.

This means on the client you would need to query multiple times. There is various ways to do this but you should ask yourself if you want this.

Do you really need all this data at your fingertips at once? The clients bandwidth usage might increase drastically. What happens if suddenly the amount of cars is so high that you run out of memory? If the API queries per app key are somehow restricted by data amount, amount of requests (again various ways, I don't know what API you are using) the workload onto the API is unnecessarily increased, as well.

Maybe dig into the API's documentation if there is one. There could be other queries that do exactly what you want, but maybe don't contain all the detailed data on your "cars" just yet, but some metadata.

Michael Schnerring
  • 3,584
  • 4
  • 23
  • 53
  • Correct, I use api-key and the query always return 1st page. I know I can set number of items per page by using page_size=50 (to set to 50 items per page) and I know how to move to say page 3 (page_number=3). But how do I return all at once? Thanks ebeeb – pixel Dec 20 '15 at 05:36
  • I can set page_size and I can set page_number. So, If I set page_size to 10 and total number of results is 23, I can set page_number=1 to get first 10 results, then page_number=2 to get next 10 results and page_number=3 to get last 3 results (since 23 in total). I can alse set page_size to 30 to get all 23 results but I realize what you are saying and that that is not a good way to go. So, you are right, I should I guess issue multiple requests to get 1st page 1, then page 2 and then finally last page. I guess that is the correct way to do this kind of stuff? – pixel Dec 20 '15 at 06:19
  • 1
    Well this depends on the API. Imagine if you have 2300 cars. You could request page_number=1 and page_size=1, so you would only receive very little data on the first request. Then you would see there is a total of 2300 pages which in this case equals 2300 cars. Now you could query with page_number=1 and page_size=2300. – Michael Schnerring Dec 20 '15 at 06:28
  • I see, so bettter than returning all 2300 cars, a better approach would be to set page_size to say 10 (cars) and then in 1st page, get page_count (which would be in this case 23) and then loop 23 times to get all 23 pages. Would this be correct approach? And perhaps do it asynchronously as well so data starts showing once it is available? – pixel Dec 20 '15 at 06:43
  • The correct approach depends again very much on your needs. To be more specific imagine scrolling through cars on a phone, which is able to display maybe 7 cars at once, you might want to implement it in such a way that you request 20 cars at once. As soon as the the 10th car comes into view you request the next 20 cars to keep users not waiting. Doing this async keeps everything from freezing while the request and scrolling is in progress. Keeping the requested data small is important to the customers to keep their phone bills from going through the roof. – Michael Schnerring Dec 20 '15 at 07:28
  • OK, but I need some code or good links. Btw, I am doing this on desktop app, C#, .NETFramework 4.5 – pixel Dec 20 '15 at 09:41