Simplified object initialization and renamed Get Path function.
This commit is contained in:
45
devops.py
45
devops.py
@@ -55,12 +55,12 @@ def auto_properties(mapping: dict[str,str] | None = None):
|
|||||||
class DevOps():
|
class DevOps():
|
||||||
"""Base class for DevOps entities."""
|
"""Base class for DevOps entities."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, org_url: str, token: str, api_version: str = DEVOPS_API_VERSION):
|
||||||
self._org_url = None
|
self._org_url = org_url.rstrip("/") + "/" # Ensure trailing slash
|
||||||
self._token = None
|
self._token = token
|
||||||
self._api_version = None
|
self._api_version = api_version
|
||||||
|
|
||||||
def get(self, path: str, params: dict = {}) -> requests.Response:
|
def _get_url_path(self, path: str, params: dict = {}) -> requests.Response:
|
||||||
if not self._org_url or not self._token or not self._api_version:
|
if not self._org_url or not self._token or not self._api_version:
|
||||||
raise ValueError("Organization URL, token, and API version must be set before making requests.")
|
raise ValueError("Organization URL, token, and API version must be set before making requests.")
|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ class DevOps():
|
|||||||
|
|
||||||
def _entities(self, entity_class: type, key_name: str, list_url: str, params: dict = {}) -> list[object]:
|
def _entities(self, entity_class: type, key_name: str, list_url: str, params: dict = {}) -> list[object]:
|
||||||
"""A generic method to retrieve a list of entities."""
|
"""A generic method to retrieve a list of entities."""
|
||||||
r = self.get(list_url, params=params)
|
r = self._get_url_path(list_url, params=params)
|
||||||
entities_data = r.json().get("value", [])
|
entities_data = r.json().get("value", [])
|
||||||
|
|
||||||
entities_list = []
|
entities_list = []
|
||||||
@@ -92,13 +92,8 @@ class DevOps():
|
|||||||
|
|
||||||
class Organization(DevOps):
|
class Organization(DevOps):
|
||||||
def __init__(self, org_url: str, token: str, api_version: str = DEVOPS_API_VERSION):
|
def __init__(self, org_url: str, token: str, api_version: str = DEVOPS_API_VERSION):
|
||||||
self._org_url = org_url.rstrip("/") + "/" # Ensure trailing slash
|
super().__init__(org_url, token, api_version)
|
||||||
self._token = token
|
|
||||||
self._api_version = api_version
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def organization(org_url: str, token: str, api_version: str = DEVOPS_API_VERSION):
|
|
||||||
return Organization(org_url, token, api_version)
|
|
||||||
@property
|
@property
|
||||||
def projects(self):
|
def projects(self):
|
||||||
return self._entities(
|
return self._entities(
|
||||||
@@ -113,13 +108,11 @@ class Organization(DevOps):
|
|||||||
})
|
})
|
||||||
class Project(DevOps):
|
class Project(DevOps):
|
||||||
def _get(self, id: str):
|
def _get(self, id: str):
|
||||||
r = self.get(f"_apis/projects/{id}")
|
r = self._get_url_path(f"_apis/projects/{id}")
|
||||||
self.from_json(r.json())
|
self.from_json(r.json())
|
||||||
|
|
||||||
def __init__(self, parent: Organization, id: str, **kwargs):
|
def __init__(self, org: Organization, id: str, **kwargs):
|
||||||
self._org_url = parent._org_url
|
super().__init__(org._org_url, org._token, org._api_version)
|
||||||
self._token = parent._token
|
|
||||||
self._api_version = parent._api_version
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._id = str(UUID(id))
|
self._id = str(UUID(id))
|
||||||
@@ -132,7 +125,7 @@ class Project(DevOps):
|
|||||||
return f"Project(name={self._name}, id={self._id})"
|
return f"Project(name={self._name}, id={self._id})"
|
||||||
|
|
||||||
def get_path(self, path: str, params: dict = {}):
|
def get_path(self, path: str, params: dict = {}):
|
||||||
return self.get(f"{self._id}/{path.lstrip('/')}", params=params)
|
return self._get_url_path(f"{self._id}/{path.lstrip('/')}", params=params)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
@@ -161,18 +154,15 @@ class Repository(DevOps):
|
|||||||
self._id = r.json().get("id", None)
|
self._id = r.json().get("id", None)
|
||||||
self.from_json(r.json())
|
self.from_json(r.json())
|
||||||
|
|
||||||
def __init__(self,_project: Project, id_or_name: str, **kwargs):
|
def __init__(self, project: Project, id_or_name: str, **kwargs):
|
||||||
|
super().__init__(project._org_url, project._token, project._api_version)
|
||||||
self._project = _project
|
self._project = project
|
||||||
self._org_url = _project._org_url
|
|
||||||
self._token = _project._token
|
|
||||||
self._api_version = _project._api_version
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._id = str(UUID(id_or_name))
|
self._id = str(UUID(id_or_name))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# Id not available, use API to get the repository object
|
# Id not available, use API to get the repository object
|
||||||
r = self.get(f"{self._project.id}/_apis/git/repositories/{urllib.parse.quote(id_or_name)}")
|
r = self._get_url_path(f"{self._project.id}/_apis/git/repositories/{urllib.parse.quote(id_or_name)}")
|
||||||
self._id = r.json().get("id", None)
|
self._id = r.json().get("id", None)
|
||||||
self.from_json(r.json())
|
self.from_json(r.json())
|
||||||
# Successfully retrieved the repository by name,
|
# Successfully retrieved the repository by name,
|
||||||
@@ -223,11 +213,8 @@ class Item(DevOps):
|
|||||||
setattr(self, f"_{name}", r.json().get(name, None))
|
setattr(self, f"_{name}", r.json().get(name, None))
|
||||||
|
|
||||||
def __init__(self, repository: Repository, path: str, **kwargs):
|
def __init__(self, repository: Repository, path: str, **kwargs):
|
||||||
|
super().__init__(repository._org_url, repository._token, repository._api_version)
|
||||||
self._repository = repository
|
self._repository = repository
|
||||||
self._org_url = repository._org_url
|
|
||||||
self._token = repository._token
|
|
||||||
self._api_version = repository._api_version
|
|
||||||
self._path = path
|
self._path = path
|
||||||
self.set_auto_properties(**kwargs) # set properties defined in decorator
|
self.set_auto_properties(**kwargs) # set properties defined in decorator
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user