Azure DevOps Python API

repository·dev·Indexed 20 days ago

https://github.com/microsoft/azure-devops-python-api

A thin wrapper around the Azure DevOps REST APIs that provides Python clients for managing Azure DevOps resources. It powers the Azure DevOps Extension for Azure CLI and allows users to interact with projects, builds, and work items using a connection and client model authenticated via Personal Access Tokens (PAT).

Tokens
1.4K
Snippets
5
Records
7
Agent score
22%

What's inside azure-devops-python-api

  1. How the connection and client model works

    dev

    The library follows a hierarchical pattern to access Azure DevOps resources:

    1. Authentication: Use msrest.authentication.BasicAuthentication with your Personal Access Token (PAT).
    2. Connection: Create a azure.devops.connection.Connection object using the base_url (your organization URL) and the credentials.
    3. Clients: Access specific functional areas by requesting a client from the connection (e.g., connection.clients.get_core_client()). Each client provides methods to interact with specific Azure DevOps entities like projects, builds, or work items.
  2. Get started with Azure DevOps Python API

    dev

    To interact with Azure DevOps, you must establish a connection using a Personal Access Token (PAT) and your organization URL. Once connected, you can retrieve specific clients (such as the core_client) to perform operations like listing projects.

    Note that many API responses are paginated. You can iterate through all results by checking for a continuation_token in the response and passing it to the next API call.

    from azure.devops.connection import Connection
    from msrest.authentication import BasicAuthentication
    import pprint
    
    # Fill in with your personal access token and org URL
    personal_access_token = 'YOURPAT'
    organization_url = 'https://dev.azure.com/YOURORG'
    
    # Create a connection to the org
    credentials = BasicAuthentication('', personal_access_token)
    connection = Connection(base_url=organization_url, creds=credentials)
    
    # Get a client (the "core" client provides access to projects, teams, etc)
    core_client = connection.clients.get_core_client()
    
    # Get the first page of projects
    get_projects_response = core_client.get_projects()
    index = 0
    while get_projects_response is not None:
        for project in get_projects_response.value:
            pprint.pprint("[" + str(index) + "] " + project.name)
            index += 1
        if get_projects_response.continuation_token is not None and get_projects_response.continuation_token != "":
            # Get the next page of projects
            get_projects_response = core_client.get_projects(continuation_token=get_projects_response.continuation_token)
        else:
            # All projects have been retrieved
            get_projects_response = None
  3. Handle Azure DevOps errors

    dev

    The client raises specific exceptions based on the type of failure encountered during an API call:

    • AzureDevOpsAuthenticationError: Raised when a request fails with a 401 status code, indicating authentication is required or invalid.
    • AzureDevOpsServiceError: Raised when the server returns a structured error message (e.g., a WrappedException).
    • AzureDevOpsClientRequestError: Raised for other client-side errors, such as non-2xx status codes or system exceptions returned by the server.
  4. Initialize the Client class

    dev

    The Client class is the base class used to instantiate service-specific clients. It manages configuration, credentials, and the underlying ServiceClient.

    To use it, provide the base_url (your Azure DevOps organization URL) and creds (your authentication credentials, such as a Personal Access Token).

    Note: While you typically interact with specialized clients (like GitClient or WorkItemTrackingClient), they all inherit from this base Client logic.

    from azure.devops.client import Client
    # Note: In practice, you will likely use a specific service client,
    # but this is how the base Client is structured.
    client = Client(base_url="https://dev.azure.com/your_org", creds=your_credentials)