如何使用 API 调用迭代字符串参数列表?

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

我正在尝试通过 API 个调用查找应用结果列表。

我 运行 遇到一个问题,我需要将应用程序 ID 作为字符串参数传递,而不是一次传递一个,我想 一次迭代所有 ID.

这是关于如何查找一个的示例代码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'])

如果我有一个应用程序 ID 列表,即

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

我想遍历 lookup_api.list 中的所有应用程序 ID(作为字符串),知道如何实现吗?

我试过了

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'])

但它没有迭代我的应用程序列表。如果结果 ['status'] == 200.

,我得到 "TypeError: list indices must be integers or slice, not str"

在此先感谢您的帮助!

您可以遍历列表并使用列表理解从 API 调用中收集结果,如下所示:

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'])