from unicodedata import name import requests import urllib.request import urllib.parse import json from uuid import UUID DEVOPS_SCOPE = "https://app.vssps.visualstudio.com/.default" DEVOPS_API_VERSION = "7.1" class Organization: def __init__(self, org_url: str, token: str, api_version: str = DEVOPS_API_VERSION): self._org_url = org_url.rstrip("/") + "/" # Ensure trailing slash self._token = token self._api_version = api_version def get(self, path: str, params: dict = {}): request_parameters = { "api-version": self._api_version, **params } encoded_path = urllib.parse.quote(path.lstrip("/")) # Ensure single slash between base and path url = self._org_url + encoded_path r = requests.get(url=url, params=request_parameters, headers={ "Authorization": f"Bearer {self._token}" }) return r @property def projects(self): r = self.get("_apis/projects") r.raise_for_status() # Return a list of Project instances projects_data = r.json().get("value", []) return [ Project(self, id=proj.get("id"), name=proj.get("name"), url=proj.get("url"), description=proj.get("description"), ) for proj in projects_data ] class Project: def __init__(self, org: Organization, id: str, name: str | None = None, url: str | None = None, description: str | None = None): self._org = org # Check, if the id is a valid UUID try: self._id = str(UUID(id)) except ValueError: raise ValueError(f"Invalid project ID: {self._id}") if name is not None and url is not None and name != "" and url != "": self._name = name self._url = url self._description = description if description is not None else "" # Ensure description is a string else: # Fetch project details from the API r = org.get(f"_apis/projects/{urllib.parse.quote(self._id)}") r.raise_for_status() proj_data = r.json() self._id = proj_data.get("id", "") self._name = proj_data.get("name", "") self._url = proj_data.get("url", "") self._description = proj_data.get("description", "") @property def name(self): return self._name def __str__(self): return f"Project(name={self._name}, id={self._id})"