Skip to content

Instantly share code, notes, and snippets.

@dmahugh
Created February 8, 2019 06:37
Show Gist options
  • Save dmahugh/ef7bccd4ec39e7266df88c9e8815cc48 to your computer and use it in GitHub Desktop.
Save dmahugh/ef7bccd4ec39e7266df88c9e8815cc48 to your computer and use it in GitHub Desktop.
Azure Devops REST API example: list projects in an instance
"""Azure Devops REST API example: list the projects in an instance
To run this example, create an ..\_private folder with an azuredevops.json file
in it that contains a PAT:
{
"bugs-read-only": "YOUR-PERSONAL-ACCESS-TOKEN-HERE"
}
"""
import json
# prerequisite: pip install vsts
from vsts.vss_connection import VssConnection
from msrest.authentication import BasicAuthentication
def vsts_client(instance, pat_type, client_type):
"""Returns an authenticated Azure Devops client
"""
with open(r"..\_private\azuredevops.json") as settings_file:
pat = json.loads(settings_file.read())[pat_type]
connection = VssConnection(
base_url=f"https://dev.azure.com/{instance}", creds=BasicAuthentication("", pat)
)
return connection.get_client(client_type)
def main():
"""List the projects in the office org
"""
core_client = vsts_client(
"office", "bugs-read-only", "vsts.core.v4_0.core_client.CoreClient"
)
projects = core_client.get_projects()
for project in projects:
project_id = project.__dict__["id"]
project_name = project.__dict__["name"]
print(f"{project_id}: {project_name}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment