【发布时间】:2020-11-18 13:37:58
【问题描述】:
我需要调用一个 rest api 并将生成的 json 存储在 azure 存储容器中。我已经尝试过独立的 python 编码来从 rest api 中提取数据,并能够成功地从具有分页的 api 接收数据。现在我需要在 Azure Function 中集成/修改这个 python 编码,并最终将生成的 json 数据存储在一个 azure 存储容器中。我对 Azure 还很陌生,因此需要您指导如何调整此代码以适应 Azure 功能,最终将 json 推送到 azure 容器。
response = requests.post(base_url,
auth=(client_id, client_secret), data={'grant_type':grant_type,'client_id':client_id,'client_secret':client_secret,'resource':resource})
acc_token_json = response.json()
access_token = json.loads(response.text)
token = access_token['access_token']
#call API to know total pages
API_Key = 'xxxxx'
api_url='https://api.example.com?pageSize=10&page=1&sortBy=orderid&sortDirection=asc'
headers = {
'Authorization': token,
'API-Key': API_Key,
}
r = requests.get(url=api_url, headers=headers).json()
total_record=int(r['pagination']['total'])
total_page=round(total_record/500)+1
#loop through all pages
all_items = []
for page in range(0, total_page):
url = "https://api.example.com?pageSize=500&sortBy=orderid&sortDirection=asc&page="+str(page)
response = requests.get(url=url, headers=headers).json()
response_data=response['data']
all_items.append(response_data)
非常感谢您的意见/指导。
【问题讨论】:
标签: python-requests azure-functions