c# - 使用 c# SDK Azure.ResourceManager.ApiManagement 导出 OpenAPI 定义

标签 c# azure sdk openapi azure-resource-manager

使用 PowerShell,我可以像这样从 APIM 导出 OpenAPI 定义:

Export-AzApiManagementApi 
    -Context $apimContext 
    -ApiId $apiId 
    -SpecificationFormat OpenApiJson 
| Out-File -FilePath ($openApiExportDirectory + "$apiId.json")

我该如何使用 Azure SDK for .NET 来做到这一点?

我可以从以下位置获取 ApiCollection:

  ArmClient >
    GetDefaultSubscriptionAsync() >
        GetResourceGroupAsync() >
            GetApiManagementServiceAsync() >
                GetApis()

我可以迭代 ApiCollection 中的每个 ApiResource,但它们不包含 OpenAPI 规范。可能需要生成它,并且应该有一个方法。我找不到这样的方法。我查看了 ApiManagementServiceResource,但找不到任何可以做到这一点的东西。

关于如何使用 SDK 获取每个 API 的 OpenAPI 定义有什么想法吗? (不是 REST、CLI 或 PS)

谢谢!

最佳答案

Any ideas on how to get the OpenAPI definition for each API using SDK? (Not REST or CLI or PS)

您可以使用 Python SDK 使用 azure-mgmt-apimanagement · PyPI 导出开放 API 定义包。

代码:

from azure.identity import DefaultAzureCredential
from azure.mgmt.apimanagement import ApiManagementClient


client = ApiManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id="Your-subscription-id",
    )

response = client.api_export.get(
        resource_group_name="Your-resource-grp",
        service_name="Your-service name",
        api_id="Your-api-id",
        format="openapi+json-link",
        export="true",
    )
print(response)

输出:

{'`additional_properties': {'name': 'xxxx', 'type': 'Microsoft.ApiManagement/service/apis', 'properties': {'format': 'openapi+json-link', 'value': {'link': 'https://<accountname>.blob.core.windows.net/api-export/xxx.json?sv=2017-04-17&sr=b&sig=Ae%2FQudIEzLvT5h6vL%2B2enacYS9YyWbxxxx&se=2023-04-29T04:53:08Z&sp=r'}}}, 'id': '/subscriptions/xxxxxxxx/resourceGroups/xxxxxxx/providers/Microsoft.ApiManagement/service/xxxx/apis/xxx', 'export_result_format': None, 'value': None}`

enter image description here

您可以复制输出结果中的值(链接)并将其粘贴到浏览器中,您将得到JSON格式。

浏览器:

enter image description here

关于c# - 使用 c# SDK Azure.ResourceManager.ApiManagement 导出 OpenAPI 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76128426/

相关文章:

android - 为什么 SDK 管理器中没有显示 Google Play 结算库?

javascript - Facebook 内容分享

c# - 等待 N 个异步方法完成时是否有标准模式可遵循?

javascript - Highchart使用javascript使用div元素在gridview的每一行上显示

winforms - 将 Winform 发布到 Azure

azure - Azure 上的 Redis 连接断开

ios - 回到前台引用对plist文件的写入操作时,Adobe iOS SDK崩溃

c# - xml转数据结构

c# - Rx 分组节流

entity-framework - Azure 应用服务上的 EF Core (1.0.0) 迁移