【问题标题】:Api call: Call for key & value to a list and then Loop for value in another API callApi 调用:调用列表的键和值,然后在另一个 API 调用中循环获取值
【发布时间】:2021-10-27 14:50:42
【问题描述】:

我使用 Canvas api 来调用诸如用户 ID、登录和注销时间之类的信息......但是 API 一次只能调用一个用户,所以我制作了这段代码来首先调用一个列表用户 ID:

import requests
import json
def get_all_time_entries():
    url_address = "mywebsite.com/api/v1/courses/1111/users?per_page=50"  
    headers = {"Authorization": "Bearer " + "this is my bearer"
}
all_time_entries = []
for page in range(1,15):
    url = "mywebsite.com/api/v1/courses/1111/users?per_page=50&page="+str(page)
    response = requests.get(url=url, headers=headers).json()        
    all_time_entries.append(response)       
    
return all_time_entries

print(get_all_time_entries())

我已经设法征集了这样的用户列表:

{
    "created_at": "time",
    "email": "email",
    "id": ID number, (***)
    "integration_id": null,
    "login_id": "email",
    "name": "Name",
    "short_name": "Name ",
    "sis_import_id": 111,
    "sis_user_id": "Name ",
    "sortable_name": ", Name "
},

现在我想使用一个新的循环来调用这个链接的 API:mywebsite.com/api/v1/audit/authentication/users/:user_id (:user_id 是我最后一次调用 api 时得到的 ID,标有 (***)) 并将所有信息放到一个列表中。我应该如何对所有这些 ID 使用循环?

【问题讨论】:

    标签: json api loops python-requests


    【解决方案1】:

    我已经设法根据我从上一个列表中使用以下代码获得的值找到调用下一个 API 的解决方案:

    import requests
    import json
    def get_all_time_entries():
    
        url = "mywebsite.com/api/v1/courses/1111/users?per_page=50"  
        headers = {
        "Authorization": "Bearer " + "this is my bearer",}
        uri = "mywebsite.com/api/v1/audit/authentication/users/" 
    
        result = []
        all_time_entries = []
    
        # loop through all pages and return list of users of certain pages
        for page in range(1,15):
    
            url = "mywebsite.com/api/v1/courses/1111/users?per_page=50&page="+str(page)
            response = requests.get(url=url, headers=headers).json()        
        all_time_entries.append(response)       
    
        return all_time_entries
    
    # Search for id value in all_time_entries
    
        value = [res['id'] for res in all_time_entries]
    
    # Loop through user log in api call by 'id'
    
        for x in value:
            url = "mywebsite.com/api/v1/audit/authentication/users/"+str(x)
            callback = requests.get(url=url, headers=headers).json()        
        result.append(callback)
        return result
    print(get_all_time_entries())
    

    现在的问题是这段代码一直返回“all_time_entries”的值,而不是“result”的值。我还尝试为列表“all_time_entries”添加某个值(没有循环和调用):

    all_time_entries = [{'id': 11, 'name': 'name', 'created_at': '2021-01-23T22:34:30+07:00', 'sortable_name': ', name', 'short_name': 'name', 'sis_user_id': 'name@email.com', 'integration_id': None, 'sis_import_id': 1, 'login_id': 'name@email.com', 'email': 'name@email.com'}, {'id': 22, 'name': 'name2', 'created_at': '2021-01-23T22:34:19+07:00', 'sortable_name': ', name2', 'short_name': 'name2', 'sis_user_id': 'name2@email.com', 'integration_id': None, 'sis_import_id': 1, 'login_id': 'name2@email.com', 'email': 'name2@email.com'}]
    

    ...它奏效了 所以我不知道这种方法是否不能处理很长的列表或其他东西,有没有办法处理长列表?

    【讨论】:

      猜你喜欢
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2015-03-04
      • 2021-10-15
      相关资源
      最近更新 更多