gfchqz
gfchqz

Reputation: 75

How to iterate a list of string parameters with API calls?

I am trying to lookup a list of applications result thru API calls.

I ran into issue that I need to pass the application id as a string parameter and instead of passing it one at a time, I would like to iterate all IDs at once.

This is sample code on how to lookup one application_id

result = lookup_api.list(application_id = "{{APP_ID}}"" 

if result['status'] == 200:
    print(result['data'])
else:
    print("An error occurred." + str(result['status']))
    print(result['data'])

If I have a list of Application IDs, i.e.,

app_id=['10000001','10000002','10000003','10000004']

I'd like to iterate all application ids (as string) in the lookup_api.list, any idea on how to achieve this?

I tried

index = 0
while index < len(app_id):
        result = [lookup_api.list(application_id=app_id[i]) for i in range(len(app_id))]
        index += 1
    
    if result['status'] == 200:
        print(result['data'])
    else:
        print("An error occurred." + str(result['status']))
        print(result['data'])

But it isn't iterating my application list. And I got "TypeError: list indices must be integers or slices, not str" on if result['status'] == 200.

Thank you in advance for your help here!

Upvotes: 0

Views: 313

Answers (1)

Aziret Satybaldiev
Aziret Satybaldiev

Reputation: 430

You can iterate over the list and use list comprehension to collect the results from API calls as follows:

results = [lookup_api.list(application_id=x) for x in app_id]

for result in results:
   if result['status'] == 200:
      print(result['data'])
   else:
      print("An error occurred." + str(result['status']))
   print(result['data'])

Upvotes: 1

Related Questions