使用 Python 请求过滤 Azure DevOps Services REST API 代理池

Use Python requests to filter Azure DevOps Services REST API agent pools

为了使用 Python 请求模块通过属性 nameisHosted 成功过滤代理池列表,需要对以下代码进行哪些具体更改,以及Azure DevOps 服务 REST API 端点记录 at this link?

当按 {name=Default,isHosted=false} 过滤列表时,应仅返回 1 个代理池,如下所示,但问题是下面的代码返回所有代理池,包括名称不同的代理池和托管的代理池.

import requests
import os
import base64
import json 

personal_access_token = ":"+os.environ["AZ_PERSONAL_ACCESS_TOKEN"]
headers = {}
headers['Content-type'] = "application/json"
headers['Authorization'] = b'Basic ' + base64.b64encode(personal_access_token.encode('utf-8'))

#Get a list of agent pools.
instance = "dev.azure.com/MyOrganization"
propVals = "{name=Default,isHosted=false}"
api_version = "5.1"
url = ("https://%s/_apis/distributedtask/pools?properties=%s?api-version=%s" % (instance, propVals, api_version))

r = requests.get(url, headers=headers)  

print("r.status_code is: ", r.status_code)
print("r.json() is: ", r.json())

请注意,关于如何使用 Python 与 Azure DevOps 服务 API 集成的文档似乎很少。另外,请注意我们使用的是 Python 3.7.6.

根据您提供的 docs 如果您只想获得 1 个池,您可以使用以下 URL 参数(而不是 properties):

poolName={poolName}

而且不仅仅是 name= 就像您的代码中那样:propVals = "{name=Default,isHosted=false}".

此外,我没有看到 isHosted 的任何参数,我认为您不需要它,因为如果您使用 poolName,您将只能获得一个池。

因此,将您的代码更新为:propVals = "poolName=Default" 因为您的最终 URL 应该是:

https://dev.azure.com/{organization}/_apis/distributedtask/pools?poolName={poolName}&api-version=5.1

注意:&api-version=5.1 而不是 ?api-version=5.1