关于 azure-mgmt-apimanagement-py
使用管理 SDK 管理 Azure API Management 的 Python 技能,涵盖 API 导入、产品、订阅、策略、后端和服务配置。
主要功能
- 01创建和管理 APIM 服务
- 02从规范或 URL 导入 API
- 03列出 API 并配置产品
- 04管理订阅和用户
- 05设置 API 策略与命名值
- 06配置后端服务
适用场景
- 01将 API 导入 APIM
- 02将 API 组合为访问产品
- 03更新策略和后端设置
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-mgmt-apimanagement-py安装后在 Claude Code 中输入/azure-mgmt-apimanagement-py确认已加载。
详细介绍
Azure API Management SDK for Python
管理 Azure API Management 服务、API、产品和策略。
安装
pip install azure-mgmt-apimanagement
pip install azure-identity
环境变量
AZURE_SUBSCRIPTION_ID=your-subscription-id
身份验证
from azure.identity import DefaultAzureCredential
from azure.mgmt.apimanagement import ApiManagementClient
import os
client = ApiManagementClient(
credential=DefaultAzureCredential(),
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"]
)
写入和策略边界
示例会创建付费基础设施、发布访问产品并替换 API 策略。执行前,请确认订阅、资源和 API,以及经授权的影响。保留现有策略 XML 和继承的基策略,检查确切的差异,并在之后验证身份验证和路由;示例中的完整策略替换可能会移除现有保护措施。请勿使用占位符密钥值,也不要暴露订阅密钥。
创建 APIM 服务
from azure.mgmt.apimanagement.models import (
ApiManagementServiceResource,
ApiManagementServiceSkuProperties,
SkuType
)
service = client.api_management_service.begin_create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
parameters=ApiManagementServiceResource(
location="eastus",
publisher_email="[email protected]",
publisher_name="My Organization",
sku=ApiManagementServiceSkuProperties(
name=SkuType.DEVELOPER,
capacity=1
)
)
).result()
print(f"Created APIM: {service.name}")
从 OpenAPI 导入 API
from azure.mgmt.apimanagement.models import (
ApiCreateOrUpdateParameter,
ContentFormat,
Protocol
)
api = client.api.begin_create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
api_id="my-api",
parameters=ApiCreateOrUpdateParameter(
display_name="My API",
path="myapi",
protocols=[Protocol.HTTPS],
format=ContentFormat.OPENAPI_JSON,
value='{"openapi": "3.0.0", "info": {"title": "My API", "version": "1.0"}, "paths": {"/health": {"get": {"responses": {"200": {"description": "OK"}}}}}}'
)
).result()
print(f"Imported API: {api.display_name}")
从 URL 导入 API
api = client.api.begin_create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
api_id="petstore",
parameters=ApiCreateOrUpdateParameter(
display_name="Petstore API",
path="petstore",
protocols=[Protocol.HTTPS],
format=ContentFormat.OPENAPI_LINK,
value="https://petstore.swagger.io/v2/swagger.json"
)
).result()
列出 API
apis = client.api.list_by_service(
resource_group_name="my-resource-group",
service_name="my-apim"
)
for api in apis:
print(f"{api.name}: {api.display_name} - {api.path}")
创建产品
from azure.mgmt.apimanagement.models import ProductContract
product = client.product.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
product_id="premium",
parameters=ProductContract(
display_name="Premium",
description="Premium tier with unlimited access",
subscription_required=True,
approval_required=False,
state="published"
)
)
print(f"Created product: {product.display_name}")
将 API 添加到产品
client.product_api.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
product_id="premium",
api_id="my-api"
)
创建订阅
from azure.mgmt.apimanagement.models import SubscriptionCreateParameters
subscription = client.subscription.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
sid="my-subscription",
parameters=SubscriptionCreateParameters(
display_name="My Subscription",
scope=f"/products/premium",
state="active"
)
)
print(f"Created subscription: {subscription.name}") # Never log subscription keys.
设置 API 策略
from azure.mgmt.apimanagement.models import PolicyContract
policy_xml = """
<policies>
<inbound>
<rate-limit calls="100" renewal-period="60" />
<set-header name="X-Custom-Header" exists-action="override">
<value>CustomValue</value>
</set-header>
</inbound>
<backend>
<forward-request />
</backend>
<outbound />
<on-error />
</policies>
"""
client.api_policy.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
api_id="my-api",
policy_id="policy",
parameters=PolicyContract(
value=policy_xml,
format="xml"
)
)
创建命名值(机密)
from azure.mgmt.apimanagement.models import NamedValueCreateContract
named_value = client.named_value.begin_create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
named_value_id="backend-api-key",
parameters=NamedValueCreateContract(
display_name="Backend API Key",
value="secret-key-value",
secret=True
)
).result()
创建后端
from azure.mgmt.apimanagement.models import BackendContract
backend = client.backend.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
backend_id="my-backend",
parameters=BackendContract(
url="https://api.backend.example.com",
protocol="http",
description="My backend service"
)
)
创建用户
from azure.mgmt.apimanagement.models import UserCreateParameters
user = client.user.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
user_id="newuser",
parameters=UserCreateParameters(
email="[email protected]",
first_name="John",
last_name="Doe"
)
)
操作组
| 组 | 用途 |
|---|---|
api_management_service | APIM 实例管理 |
api | API 操作 |
api_operation | API 操作详细信息 |
api_policy | API 级策略 |
product | 产品管理 |
product_api | 产品与 API 的关联 |
subscription | 订阅管理 |
user | 用户管理 |
named_value | 命名值/机密 |
backend | 后端服务 |
certificate | 证书 |
gateway | 自托管网关 |
最佳实践
- 使用命名值存储机密和配置
- 在适当的范围(全局、产品、API、操作)应用策略
- 使用产品捆绑 API 并管理访问权限
- 启用 Application Insights 进行监控
- 使用后端抽象后端服务
- 使用 APIM 的版本控制功能管理 API 版本
适用场景
当需要执行概述中描述的工作流或操作时,可使用此技能。
限制
- 仅当任务明确符合上述范围时,才使用此技能。
- 不要将输出视为环境特定验证、测试或专家审查的替代品。
- 如果缺少必要的输入、权限、安全边界或成功标准,请暂停并请求澄清。
评论
支持 Markdown