Renamed _get_attributes to _get and implemented for Item.

This commit is contained in:
2025-11-03 00:00:38 +01:00
parent d8674462df
commit 53e1ae186e
2 changed files with 32 additions and 10 deletions

View File

@@ -20,7 +20,7 @@ def auto_properties(mapping: dict[str,str] | None = None):
pass
# Fetch repository details from the API if it is set to None or not existing
self._get_attributes()
self._get()
return getattr(self, private_var)
return property(fget=getter)
@@ -76,10 +76,10 @@ class DevOps():
r.raise_for_status() # Ensure we raise an error for bad responses
return r
def _get_entity_attributes(self, key_name: str, get_url: str, params: dict = {}) -> object:
def _get_entity(self, key_name: str, get_url: str, params: dict = {}) -> object:
"""
Each entity class can use this method to populate its attributes, by defining
its own _get_attributes method that calls this one with the key name,
its own _get method that calls this one with the key name,
and the URL.
"""
r = self._get_url_path(get_url, params=params) # Fetch the object data from the URL
@@ -121,8 +121,8 @@ class Organization(DevOps):
"description": "description"
})
class Project(DevOps):
def _get_attributes(self):
self._get_entity_attributes(
def _get(self):
self._get_entity(
key_name="id",
get_url=f"_apis/projects/{self._id}"
)
@@ -162,8 +162,8 @@ class Project(DevOps):
"web_url": "webUrl"
})
class Repository(DevOps):
def _get_attributes(self):
self._get_entity_attributes(
def _get(self):
self._get_entity(
key_name="id",
get_url=f"{self._project.id}/_apis/git/repositories/{self._id}"
)
@@ -177,7 +177,7 @@ class Repository(DevOps):
UUID(id) # Check if it's a valid UUID
except ValueError:
# Called with a repository name, fetch by name
self._get_attributes()
self._get()
if kwargs:
raise ValueError("Automatic properties cannot be set when retrieving by name.")
return
@@ -189,6 +189,10 @@ class Repository(DevOps):
def id(self):
return self._id
@property
def project(self):
return self._project
def __str__(self):
return f"Repository(name={self.name}, id={self._id})"
@@ -219,6 +223,16 @@ class Item(DevOps):
self._path = path
self.set_auto_properties(**kwargs) # set properties defined in decorator
def _get(self):
self._get_entity(
key_name="path",
get_url=f"{self._repository._project.id}/_apis/git/repositories/{self._repository.id}/items",
params={
"path": self._path,
"$format": "json"
}
)
@property
def path(self):
return self._path

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env python3
from devops import Organization, Project, Repository, DEVOPS_SCOPE
from devops import Organization, Project, Repository, Item, DEVOPS_SCOPE
from azure.identity import DefaultAzureCredential
from json import dumps
@@ -30,3 +30,11 @@ print(f"Listing items in the {repo.name} repository:")
for item in repo.items:
print(f"{item.path} ({item.git_object_type}): {item.commit_id}")
print()
print("Getting specific item details:")
item = Item(repo, path="/generate-pat.py")
print(f"Item path: {item.path}")
print(f"Item type: {item.git_object_type}")
print(f"Item commit ID: {item.commit_id}")
print(f"Item URL: {item.url}")
print()