关于 azure-mgmt-apicenter-py
azure-mgmt-apicenter-py 是用于管理 Azure API Center 中 API 目录、定义、部署和治理的 Python SDK,适合自动化 API 目录管理的开发者与运维人员。
主要功能
- 01创建和管理 API Center 服务
- 02按工作区组织 API
- 03注册 API 及其版本
- 04导入 API 规范
- 05跟踪环境和部署
- 06定义元数据架构
适用场景
- 01按工作区整理 API 目录
- 02导入规范以供分析
- 03跟踪 API 部署位置
- 04统一应用 API 元数据
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-mgmt-apicenter-py安装后在 Claude Code 中输入/azure-mgmt-apicenter-py确认已加载。
详细介绍
适用于 Python 的 Azure API Center 管理 SDK
在 Azure API Center 中管理 API 清单、元数据和治理。
安装
pip install azure-mgmt-apicenter
pip install azure-identity
环境变量
AZURE_SUBSCRIPTION_ID=your-subscription-id
身份验证
from azure.identity import DefaultAzureCredential
from azure.mgmt.apicenter import ApiCenterMgmtClient
import os
client = ApiCenterMgmtClient(
credential=DefaultAzureCredential(),
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"]
)
创建 API Center
from azure.mgmt.apicenter.models import Service
api_center = client.services.create_or_update(
resource_group_name="my-resource-group",
service_name="my-api-center",
resource=Service(
location="eastus",
tags={"environment": "production"}
)
)
print(f"Created API Center: {api_center.name}")
列出 API Center
api_centers = client.services.list_by_subscription()
for api_center in api_centers:
print(f"{api_center.name} - {api_center.location}")
注册 API
from azure.mgmt.apicenter.models import Api, ApiKind, LifecycleStage
api = client.apis.create_or_update(
resource_group_name="my-resource-group",
service_name="my-api-center",
workspace_name="default",
api_name="my-api",
resource=Api(
title="My API",
description="A sample API for demonstration",
kind=ApiKind.REST,
lifecycle_stage=LifecycleStage.PRODUCTION,
terms_of_service={"url": "https://example.com/terms"},
contacts=[{"name": "API Team", "email": "[email protected]"}]
)
)
print(f"Registered API: {api.title}")
创建 API 版本
from azure.mgmt.apicenter.models import ApiVersion, LifecycleStage
version = client.api_versions.create_or_update(
resource_group_name="my-resource-group",
service_name="my-api-center",
workspace_name="default",
api_name="my-api",
version_name="v1",
resource=ApiVersion(
title="Version 1.0",
lifecycle_stage=LifecycleStage.PRODUCTION
)
)
print(f"Created version: {version.title}")
添加 API 定义
from azure.mgmt.apicenter.models import ApiDefinition
definition = client.api_definitions.create_or_update(
resource_group_name="my-resource-group",
service_name="my-api-center",
workspace_name="default",
api_name="my-api",
version_name="v1",
definition_name="openapi",
resource=ApiDefinition(
title="OpenAPI Definition",
description="OpenAPI 3.0 specification"
)
)
导入 API 规范
from azure.mgmt.apicenter.models import ApiSpecImportRequest, ApiSpecImportSourceFormat
# Import from inline content
client.api_definitions.import_specification(
resource_group_name="my-resource-group",
service_name="my-api-center",
workspace_name="default",
api_name="my-api",
version_name="v1",
definition_name="openapi",
body=ApiSpecImportRequest(
format=ApiSpecImportSourceFormat.INLINE,
value='{"openapi": "3.0.0", "info": {"title": "My API", "version": "1.0"}, "paths": {}}'
)
)
列出 API
apis = client.apis.list(
resource_group_name="my-resource-group",
service_name="my-api-center",
workspace_name="default"
)
for api in apis:
print(f"{api.name}: {api.title} ({api.kind})")
创建环境
from azure.mgmt.apicenter.models import Environment, EnvironmentKind
environment = client.environments.create_or_update(
resource_group_name="my-resource-group",
service_name="my-api-center",
workspace_name="default",
environment_name="production",
resource=Environment(
title="Production",
description="Production environment",
kind=EnvironmentKind.PRODUCTION,
server={"type": "Azure API Management", "management_portal_uri": ["https://portal.azure.com"]}
)
)
创建部署
from azure.mgmt.apicenter.models import Deployment, DeploymentState
deployment = client.deployments.create_or_update(
resource_group_name="my-resource-group",
service_name="my-api-center",
workspace_name="default",
api_name="my-api",
deployment_name="prod-deployment",
resource=Deployment(
title="Production Deployment",
description="Deployed to production APIM",
environment_id="/workspaces/default/environments/production",
definition_id="/workspaces/default/apis/my-api/versions/v1/definitions/openapi",
state=DeploymentState.ACTIVE,
server={"runtime_uri": ["https://api.example.com"]}
)
)
定义自定义元数据
from azure.mgmt.apicenter.models import MetadataSchema
metadata = client.metadata_schemas.create_or_update(
resource_group_name="my-resource-group",
service_name="my-api-center",
metadata_schema_name="data-classification",
resource=MetadataSchema(
schema='{"type": "string", "title": "Data Classification", "enum": ["public", "internal", "confidential"]}'
)
)
客户端类型
| 客户端 | 用途 |
|---|---|
ApiCenterMgmtClient | 用于所有操作的主客户端 |
操作
| 操作组 | 用途 |
|---|---|
services | API Center 服务管理 |
workspaces | 工作区管理 |
apis | API 注册和管理 |
api_versions | API 版本管理 |
api_definitions | API 定义管理 |
deployments | 部署跟踪 |
environments | 环境管理 |
metadata_schemas | 自定义元数据定义 |
最佳实践
- 使用工作区按团队或域组织 API
- 定义元数据架构以实现一致的治理
- 跟踪部署以了解 API 的运行位置
- 导入规范以启用 API 分析和 linting
- 使用生命周期阶段跟踪 API 成熟度
- 添加联系人以明确 API 所有权和支持
何时使用
本技能适用于执行概述中描述的工作流或操作。
限制
- 仅当任务与上述范围明确匹配时使用本技能。
- 不要将输出视为特定环境验证、测试或专家评审的替代品。
- 如果缺少必需的输入、权限、安全边界或成功标准,请停止并请求澄清。
评论
支持 Markdown