diff --git a/src/octosuite/core/github.py b/src/octosuite/core/github.py index 062e00c..83b8bb4 100644 --- a/src/octosuite/core/github.py +++ b/src/octosuite/core/github.py @@ -45,7 +45,7 @@ class GitHub: return response if response.status_code == 200: - sanitised = self._sanitise_response(response=response.json()) + sanitised = self.sanitise_response(response=response.json()) # Cache the successful response if use_cache: @@ -77,7 +77,7 @@ class GitHub: # Only cache if entity exists (status 200) if response.status_code == 200: - sanitised = self._sanitise_response(response.json()) + sanitised = self.sanitise_response(response.json()) self.cache.set(url, sanitised) return True @@ -85,11 +85,11 @@ class GitHub: except requests.RequestException: return False - def _sanitise_response(self, response: t.Union[dict, list]) -> t.Union[dict, list]: + def sanitise_response(self, response: t.Union[dict, list]) -> t.Union[dict, list]: pattern = re.compile(r"https://api\.github\.com") if isinstance(response, list): - return [self._sanitise_response(response=item) for item in response] + return [self.sanitise_response(response=item) for item in response] if isinstance(response, dict): keys_to_remove = [ @@ -103,6 +103,6 @@ class GitHub: # Recursively clean nested dicts/lists for key, value in response.items(): if isinstance(value, (dict, list)): - response[key] = self._sanitise_response(response=value) + response[key] = self.sanitise_response(response=value) return response diff --git a/src/octosuite/core/models.py b/src/octosuite/core/models.py index b32139c..d7aa6bf 100644 --- a/src/octosuite/core/models.py +++ b/src/octosuite/core/models.py @@ -1,3 +1,5 @@ +from requests import exceptions + from .github import GitHub, BASE_URL github = GitHub() @@ -14,28 +16,25 @@ class GitHubEntity: def exists(self) -> tuple[bool, dict]: """Check if the entity exists on GitHub.""" - _type = None - kwargs = {} + # Check cache first + cached = github.cache.get(self.endpoint) + if cached is not None: + return True, cached - if isinstance(self, User): - _type = "user" - kwargs = {"username": self.name} - elif isinstance(self, Org): - _type = "org" - kwargs = {"username": self.name} - elif isinstance(self, Repo): - _type = "repo" - kwargs = {"repo_owner": self.owner, "repo_name": self.name} + try: + response = github.get(url=self.endpoint, return_response=True) - # Use is_valid_entity which handles caching and sanitization - exists = github.is_valid_entity(_type=_type, **kwargs) + if response.status_code == 200: + data = response.json() + # Sanitise the data + sanitised = github.sanitise_response(data) + # Cache the sanitised response + github.cache.set(self.endpoint, sanitised) + return True, sanitised - if exists: - # Get the cached sanitized data - cached = github.cache.get(self.endpoint) - return True, cached if cached else {} - - return False, {} + return False, response.json() + except exceptions.RequestException: + return False, {} class User(GitHubEntity):