【问题标题】:Calling a REST API using Azure function App and store data in Azure container使用 Azure 函数 App 调用 REST API 并将数据存储在 Azure 容器中
【发布时间】: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


    【解决方案1】:

    你可以把逻辑放在函数体中。(函数只是设置触发器的条件。)

    比如你是基于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://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-output?tabs=python#example

    如果有任何问题,请告诉我。

    【讨论】:

    • 您好鲍曼,非常感谢您的回复。如果我卡在某个步骤,我会尝试并返回。再次感谢。
    • 嗨,是的,我在门户中运行 azure 函数时遇到错误。
    • 是的,我在门户中运行 azure 函数时遇到错误。 “结果:失败异常:TypeError:无法编码传出TypedData:Python类型“list”的不支持类型“”。似乎是因为我正在循环浏览多个页面并尝试附加结果到上面帖子中提到的数组 all_items[] 但 func def 是 func.Out[str] 你能告诉我这是否是分配给 bloboutput 的正确方法 - outputBlob.set(all_items) def main(mytimer: func. TimerRequest, outputBlob: func.Out[str]) -> None:
    • 嗨,简而言之,请让我知道我应该如何将数组输出传递给 blob 以及函数定义头在 python 代码中的外观,即 func.Out() 的东西..跨度>
    • 嗨,我是通过另一个博客知道的。我只需要在前面声明-“outputBlob.set(str(all_items))。我对我们在“localsettings.json”中提到的连接字符串还有另一个问题。我已经从“AzureWebJobsStorage”下的存储帐户复制粘贴了连接“。这是否意味着我们在开发时无法一次性部署所有资源(资源组、存储帐户、functionapp、函数),因为在该资源准备好并部署之前我们不知道存储帐户的连接字符串? 对不起,如果这是一个愚蠢的问题,学习的东西。
    猜你喜欢
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2019-09-03
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2021-08-02
    相关资源
    最近更新 更多