0

Currently I am trying to fetch from an API which has 2 endpoints:

GET /AllUsers 
GET /user_detail/{id}

In order to get the details of all the users, I would have to call GET /AllUsers, and loop through the IDs to call the GET /user_detail/{id} endpoint 1 by 1. I wonder if it's possible to have multiple GET /user_detail/{id} calls running at the same time? Or perhaps there is a better approach?

Pig
  • 2,002
  • 5
  • 26
  • 42
  • You could use threading as described [here](http://stackoverflow.com/questions/2846653/how-to-use-threading-in-python) – fedterzi Apr 07 '17 at 23:21
  • It seems like the structure depends greatly upon what you wish to do with each ID that you retrieve. What happens to each user once they are retrieved? – JacobIRR Apr 07 '17 at 23:22
  • @JacobIRR: I am trying to sync those data with my local DB, so for each user I would check if the data is changed and then update my local DB accordingly. – Pig Apr 07 '17 at 23:49

1 Answers1

0

This sounds like a great use case for grequests

import grequests

urls = [f'http://example.com/user_detail/{id}' for id in range(10)]

rs = (grequests.get(u) for u in urls)
responses = grequests.map(rs)

Edit: As an example for processing responses to retrieve json you could:

data = []
for response in responses:
    data.append(response.json())
brennan
  • 3,392
  • 24
  • 42
  • Thanks for the recommendation, however the documentation doesn't specify how to process the response from each requests, is that possible with grequests? – Pig Apr 10 '17 at 15:43
  • What do you intend to do with each response? `grequests.map(rs)` returns a list of the responses – brennan Apr 10 '17 at 16:43