使用 Azure 逻辑应用调用 REST 终结点Call REST endpoints by using Azure Logic Apps
借助 Azure 逻辑应用和内置的 HTTP + Swagger 连接器,可以通过生成逻辑应用,使用一个 Swagger 文件自动执行定期调用任何 REST 终结点的工作流。With Azure Logic Apps and the built-in HTTP + Swagger connector, you can automate workflows that regularly call any REST endpoint through a Swagger file by building logic apps. HTTP + Swagger 触发器和操作的工作方式与 HTTP 触发器和操作相同,但通过公开 Swagger 文件描述的 API 结构和输出,在逻辑应用设计器中提供更好的体验。The HTTP + Swagger trigger and action work the same as the HTTP trigger and action but provide a better experience in the Logic App Designer by exposing the API structure and outputs described by the Swagger file. 如果要实现轮询触发器,请遵循创建用于调用逻辑应用的其他 API、服务和系统的自定义 API 中所述的轮询模式。To implement a polling trigger, follow the polling pattern that's described in Create custom APIs to call other APIs, services, and systems from logic apps.
先决条件Prerequisites
Azure 订阅。An Azure subscription. 如果没有 Azure 订阅,请注册试用订阅。If you don't have an Azure subscription, sign up for a Trial Subscription.
用于描述目标 REST 终结点的 Swagger(非 OpenAPI)文件的 URLThe URL for the Swagger (not OpenAPI) file that describes the target REST endpoint
通常,REST 终结点必须满足此条件,连接器才能工作:Typically, the REST endpoint must meet this criteria for the connector to work:
Swagger 文件必须托管在可公开访问的 HTTPS URL 上。The Swagger file must be hosted on an HTTPS URL that's publicly accessible.
Swagger 文件必须包含定义中每个操作的
operationID
。The Swagger file must contain anoperationID
for each operation in the definition. 如果未包含,则连接器只显示 Swagger 文件中的最后一个操作。If not, the connector only shows the last operation in the Swagger file.必须为 Swagger 文件启用跨域资源共享 (CORS)。The Swagger file must have Cross-Origin Resource Sharing (CORS) enabled.
若要引用非托管的或者不符合安全要求和跨域要求的 Swagger 文件,可将 Swagger 文件上传到 Azure 存储帐户中的某个 Blob 容器,并在该存储帐户中启用 CORS,以便可以引用该文件。To reference a Swagger file that's not hosted or that doesn't meet the security and cross-origin requirements, you can upload the Swagger file to a blob container in an Azure storage account, and enable CORS on that storage account so that you can reference the file.
本主题中的示例使用认知服务人脸 API,这需要获取认知服务帐户和访问密钥。The examples in this topic use the Cognitive Services Face API, which requires a Cognitive Services account and access key.
有关如何创建逻辑应用的基本知识。Basic knowledge about how to create logic apps. 如果你不熟悉逻辑应用,请查看什么是 Azure 逻辑应用?If you're new to logic apps, review What is Azure Logic Apps?
要从中调用目标终结点的逻辑应用。The logic app from where you want to call the target endpoint. 若要开始使用 HTTP + Swagger 触发器,请创建一个空白逻辑应用。To start with the HTTP + Swagger trigger, create a blank logic app. 若要使用 HTTP + Swagger 操作,请使用所需的任何触发器启动逻辑应用。To use the HTTP + Swagger action, start your logic app with any trigger that you want. 此示例的第一步是使用 HTTP + Swagger 触发器。This example uses the HTTP + Swagger trigger as the first step.
添加 HTTP + Swagger 触发器Add an HTTP + Swagger trigger
此内置触发器将一个 HTTP 请求发送到用于描述 REST API 的 Swagger 文件的 URL,并返回包含该文件的内容的响应。This built-in trigger sends an HTTP request to a URL for a Swagger file that describes a REST API and returns a response that contains that file's content.
登录 Azure 门户。Sign in to the Azure portal. 在逻辑应用设计器中打开空白逻辑应用。Open your blank logic app in Logic App Designer.
在设计器的搜索框中,输入“swagger”作为筛选器。On the designer, in the search box, enter "swagger" as your filter. 在“触发器”列表中选择“HTTP + Swagger”触发器。 From the Triggers list, select the HTTP + Swagger trigger.
在“SWAGGER 终结点 URL”框中,输入 Swagger 文件的 URL,然后选择“下一步”。 In the SWAGGER ENDPOINT URL box, enter the URL for the Swagger file, and select Next.
对于认知服务人脸 API,此示例使用位于“中国东部 2”区域的 Swagger URL:This example uses the Swagger URL that is located in the China East 2 region for the Cognitive Services Face API:
https://chinaeast2.dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face%20API%20-%20V1.0
当设计器显示了 Swagger 文件描述的操作时,请选择要使用的操作。When the designer shows the operations described by the Swagger file, select the operation that you want to use.
提供要包含在终结点调用中的触发器参数的值(根据所选的操作而异)。Provide the values for the trigger parameters, which vary based on the selected operation, that you want to include in the endpoint call. 设置重复周期,以确定触发器调用终结点的频率。Set up the recurrence for how often you want the trigger to call the endpoint.
此示例将触发器重命名为“HTTP + Swagger trigger:Face - Detect”,使步骤名称更具描述性。This example renames the trigger to "HTTP + Swagger trigger: Face - Detect" so that the step has a more descriptive name.
要添加其他可用参数,请打开“添加新参数”列表,然后选择所需参数。To add other available parameters, open the Add new parameter list, and select the parameters that you want.
有关 HTTP + Swagger 可用的身份验证类型的详细信息,请参阅向出站调用添加身份验证。For more information about authentication types available for HTTP + Swagger, see Add authentication to outbound calls.
继续使用触发器激发时运行的操作生成逻辑应用的工作流。Continue building your logic app's workflow with actions that run when the trigger fires.
完成后,请记得保存逻辑应用。When you're finished, remember to save your logic app. 在设计器工具栏上选择“保存”。On the designer toolbar, select Save.
添加 HTTP + Swagger 操作Add an HTTP + Swagger action
此内置操作对用于描述 REST API 的 Swagger 文件的 URL 发出一个 HTTP 请求,并返回包含该文件的内容的响应。This built-in action makes an HTTP request to the URL for the Swagger file that describes a REST API and returns a response that contains that file's content.
登录 Azure 门户。Sign in to the Azure portal. 在逻辑应用设计器中打开逻辑应用。Open your logic app in Logic App Designer.
在要添加 HTTP + Swagger 操作的步骤下,选择“新建步骤”。Under the step where you want to add the HTTP + Swagger action, select New step.
若要在步骤之间添加操作,请将鼠标指针移到步骤之间的箭头上。To add an action between steps, move your pointer over the arrow between steps. 选择出现的加号 ( + ),然后选择“添加操作”。Select the plus sign (+) that appears, and then select Add an action.
在设计器的搜索框中,输入“swagger”作为筛选器。On the designer, in the search box, enter "swagger" as your filter. 在“操作”列表中选择“HTTP + Swagger”操作。 From the Actions list, select the HTTP + Swagger action.
在“SWAGGER 终结点 URL”框中,输入 Swagger 文件的 URL,然后选择“下一步”。 In the SWAGGER ENDPOINT URL box, enter the URL for the Swagger file, and select Next.
对于认知服务人脸 API,此示例使用位于“中国东部 2”区域的 Swagger URL:This example uses the Swagger URL that is located in the China East 2 region for the Cognitive Services Face API:
https://chinaeast2.dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face%20API%20-%20V1.0
当设计器显示了 Swagger 文件描述的操作时,请选择要使用的操作。When the designer shows the operations described by the Swagger file, select the operation that you want to use.
提供要包含在终结点调用中的操作参数的值(根据所选的操作而异)。Provide the values for the action parameters, which vary based on the selected operation, that you want to include in the endpoint call.
此示例未使用参数,而是将操作重命名为“HTTP + Swagger action:Face - Identify”,使步骤名称更具描述性。This example has no parameters, but renames the action to "HTTP + Swagger action: Face - Identify" so that the step has a more descriptive name.
要添加其他可用参数,请打开“添加新参数”列表,然后选择所需参数。To add other available parameters, open the Add new parameter list, and select the parameters that you want.
有关 HTTP + Swagger 可用的身份验证类型的详细信息,请参阅向出站调用添加身份验证。For more information about authentication types available for HTTP + Swagger, see Add authentication to outbound calls.
完成后,请记得保存逻辑应用。When you're finished, remember to save your logic app. 在设计器工具栏上选择“保存”。On the designer toolbar, select Save.
在 Azure 存储中托管 SwaggerHost Swagger in Azure Storage
可以引用非托管的或者不符合安全要求和跨域要求的 Swagger 文件,方法是将该文件上传到 Azure 存储帐户中的 Blob 容器,并在该存储帐户中启用 CORS。You can reference a Swagger file that's not hosted or that doesn't meet the security and cross-origin requirements by uploading that file to blob container in an Azure storage account and enabling CORS on that storage account. 若要在 Azure 存储中创建、设置和存储 Swagger 文件,请执行以下步骤:To create, set up, and store Swagger files in Azure Storage, follow these steps:
现在,为 Blob 启用 CORS。Now enable CORS for the blob. 在存储帐户的菜单中选择“CORS”。On your storage account's menu, select CORS. 在“Blob 服务”选项卡上指定值,然后选择“保存”。 On the Blob service tab, specify these values, and then select Save.
属性Property 值Value 允许的源Allowed origins *
允许的方法Allowed methods GET
,HEAD
,PUT
GET
,HEAD
,PUT
允许的标头Allowed headers *
公开的标头Exposed headers *
最大期限(以秒为单位)Max age (in seconds) 200
此示例使用 Azure 门户,不过,你也可以使用 Azure 存储资源管理器之类的工具,或使用此示例 PowerShell 脚本自动配置此设置。Although this example uses the Azure portal, you can use a tool such as Azure Storage Explorer, or automatically configure this setting by using this sample PowerShell script.
创建 Blob 容器。Create a blob container. 在容器的“概述”窗格中,选择“更改访问级别”。 On the container's Overview pane, select Change access level. 在“公共访问级别”列表中,选择“Blob (仅限对 Blob 进行匿名读取访问)”,然后选择“确定”。 From the Public access level list, select Blob (anonymous read access for blobs only), and select OK.
通过 Azure 门户或 Azure 存储资源管理器将 Swagger 文件上传到 Blob 容器。Upload the Swagger file to the blob container, either through the Azure portal or Azure Storage Explorer.
若要在 Blob 容器中引用该文件,请从 Azure 存储资源管理器获取采用以下格式(区分大小写)的 HTTPS URL:To reference the file in the blob container, get the HTTPS URL that follows this format, which is case-sensitive, from Azure Storage Explorer:
https://<storage-account-name>.blob.core.chinacloudapi.cn/<blob-container-name>/<complete-swagger-file-name>?<query-parameters>
连接器参考Connector reference
下面是有关 HTTP + Swagger 触发器或操作的输出的详细信息。Here is more information about the outputs from an HTTP + Swagger trigger or action. HTTP + Swagger 调用返回以下信息:The HTTP + Swagger call returns this information:
属性名称Property name | 类型Type | 说明Description | |
---|---|---|---|
headersheaders | 对象object | 请求中的标头The headers from the request | |
bodybody | 对象object | JSON 对象JSON object | 包含请求中正文内容的对象The object with the body content from the request |
状态代码status code | intint | 请求中的状态代码The status code from the request | |
状态代码Status code | 说明Description |
---|---|
200200 | 确定OK |
202202 | 已接受Accepted |
400400 | 错误的请求Bad request |
401401 | 未授权Unauthorized |
403403 | 禁止Forbidden |
404404 | 未找到Not Found |
500500 | 内部服务器错误。Internal server error. 发生未知错误。Unknown error occurred. |
后续步骤Next steps
- 了解其他逻辑应用连接器Learn about other Logic Apps connectors