使用 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()
我可以遍历
ApiResource
中的每个 ApiCollection
但它们不包括 OpenAPI 规范。可能需要生成它,并且应该有一种方法。我找不到这样的方法。我查看了ApiManagementServiceResource
,但我找不到任何可以做到这一点的东西。
关于如何使用 SDK 获取每个 API 的 OpenAPI 定义的任何想法? (不是 REST 或 CLI 或 PS)
谢谢!
关于如何使用 SDK 获取每个 API 的 OpenAPI 定义的任何想法? (不是 REST 或 CLI 或 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}`
您可以复制输出结果中的值(链接)并将其粘贴到浏览器中您将获得JSON格式。
浏览器: