如何始终识别并使用Azure DevOps Services最新稳定REST API版本?
很高兴能帮你解决这个API版本维护的问题!下面是几个实用的方案,能让你不用每次手动修改代码就能用上Azure DevOps REST API的最新稳定版本:
自动使用Azure DevOps REST API最新稳定版本的方案
1. 调用API版本清单端点动态获取最新版本
Azure DevOps提供了专门的端点来返回所有服务的可用API版本,你可以先请求这个端点,解析出packaging服务的最新稳定版,再用这个版本去调用创建Feed的接口。
Python示例代码
import requests # 替换为你的组织名称 ORG_NAME = "your-organization-name" BASE_API_URL = f"https://dev.azure.com/{ORG_NAME}/_apis/" # 第一步:获取所有服务的API版本清单 version_response = requests.get( f"{BASE_API_URL}api-version", headers={"Accept": "application/json"} ) version_response.raise_for_status() version_data = version_response.json() # 第二步:筛选出packaging服务的最新稳定版本 packaging_versions = version_data["apiVersions"]["packaging"] # 过滤掉预览版(包含preview、beta、rc标识的版本) stable_versions = [ v for v in packaging_versions if not any(tag in v.lower() for tag in ["preview", "beta", "rc"]) ] # 版本清单按从旧到新排序,取最后一个就是最新稳定版 latest_stable_version = stable_versions[-1] # 第三步:用最新稳定版调用创建Feed的API create_feed_url = ( f"https://feeds.dev.azure.com/{ORG_NAME}/_apis/packaging/feeds" f"?api-version={latest_stable_version}" ) # 替换为你的认证信息和Feed配置 auth_headers = {"Authorization": "Basic your-personal-access-token"} feed_payload = { "name": "auto-created-feed", "description": "Feed created with latest stable API version" } feed_response = requests.post( create_feed_url, json=feed_payload, headers=auth_headers ) feed_response.raise_for_status() print("Feed created successfully:", feed_response.json()["name"])
2. 使用官方Azure DevOps Python SDK
微软官方的Azure DevOps Python SDK会自动维护API版本,默认使用对应服务的最新稳定版。你只需要调用SDK的封装方法,不用手动指定API版本号。
步骤:
- 安装SDK:
pip install azure-devops
- 使用SDK创建Feed的示例代码:
from azure.devops.connection import Connection from msrest.authentication import BasicAuthentication from azure.devops.v7_1.packaging.models import Feed # 替换为你的认证信息和组织地址 PAT_TOKEN = "your-personal-access-token" ORG_URL = "https://dev.azure.com/your-organization-name" # 创建连接 credentials = BasicAuthentication("", PAT_TOKEN) connection = Connection(base_url=ORG_URL, creds=credentials) # 获取Packaging客户端 packaging_client = connection.clients.get_packaging_client() # 创建Feed new_feed = Feed( name="sdk-created-feed", description="Feed created via Azure DevOps Python SDK" ) created_feed = packaging_client.create_feed(feed=new_feed, organization=ORG_NAME.split("/")[-1]) print("Feed created successfully:", created_feed.name)
后续只需要定期执行pip install --upgrade azure-devops更新SDK,就能自动用上最新的API版本,无需修改业务代码。
3. 配置版本变量+定期检查更新
如果不想依赖额外的API请求或SDK,可以把API版本抽离成单独的配置变量,比如在项目中创建config.py:
# config.py AZURE_ARTIFACTS_STABLE_API_VERSION = "7.1"
然后在业务代码中引用这个变量。同时设置一个定期提醒(比如每月一次),去Azure DevOps的REST API文档页面确认是否有新的稳定版本发布,更新配置变量即可。这种方法最简单,适合API版本更新不频繁的场景。
内容的提问来源于stack exchange,提问作者Niraimathi
相关产品推荐
相关产品推荐

