我需要调用rest api并将生成的json存储在azure存储容器中。我已经尝试了独立的python编码来从rest api中提取数据,并且能够成功地从具有分页的api中接收数据。现在我需要在Azure函数中集成/修改这个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) 非常感谢您的意见/指导。
发布于 2020-11-19 09:42:30
您可以将逻辑放在函数的主体中。(函数只是设置触发器的条件。)
例如,如果您基于HttpTrigger:
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
'''
#Put the your logic code here.
'''
return func.HttpResponse(
"This is a test.",
status_code=200
)你也可以使用blob输出来实现你的要求,它更容易,看看这个官方文档:
如果有任何问题,请告诉我。
https://stackoverflow.com/questions/64894191
复制相似问题