Call Api For Each Element In List
I have a list with over 1000 IDs and I want to call an API with different endpoints for every element of the list. Example: customerlist = [803818, 803808, 803803,803738,803730] I
Solution 1:
The moment a function hits a return
statement, it immediately finishes. Since your return
statement is in the loop, the other iterations never actually get called.
To fix, you can create a list
outside the loop, append to it every loop iteration, and then return the DataFrame created with that list:
defget_data(endpoint):
responses = []
for i in customerlist:
api_endpoint = endpoint
params = {'customerid' : i}
response = requests.get(f"{API_BASEURL}/{api_endpoint}",
params = params,
headers = HEADERS)
if response.status_code == 200:
res = json.loads(response.text)
else:
raise Exception(f'API error with status code {response.status_code}')
responses.append(res)
return pd.DataFrame(responses)
A much cleaner solution would be to use list comprehension:
def get_data(endpoint, i):
api_endpoint = endpoint
params = {'customerid' : i}
response = requests.get(f"{API_BASEURL}/{api_endpoint}",
params = params,
headers = HEADERS)
if response.status_code == 200:
res = json.loads(response.text)
else:
raise Exception(f'API error with status code {response.status_code}')
return res
responses = pd.DataFrame([get_data(endpoint, i) for i in customerlist])
Post a Comment for "Call Api For Each Element In List"