Skip to content

Commit

Permalink
refactor: remove cache layer from finder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tdstein committed Nov 4, 2024
1 parent 22842ea commit 0360b6a
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/posit/connect/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,7 @@ def _create_instance(self, path: str, /, **kwargs: Any) -> T:
"""Create an instance of 'T'."""
raise NotImplementedError()

def reload(self) -> Self:
"""Reloads the collection from Connect.
Returns
-------
Self
"""
self._cache = None
return self

def _fetch(self) -> List[T]:
def fetch(self) -> List[T]:
"""Fetch the collection.
Fetches the collection directly from Connect. This operation does not effect the cache state.
Expand All @@ -128,6 +118,16 @@ def _fetch(self) -> List[T]:
results = response.json()
return [self._to_instance(result) for result in results]

def reload(self) -> Self:
"""Reloads the collection from Connect.
Returns
-------
Self
"""
self._cache = None
return self

def _to_instance(self, result: dict) -> T:
"""Converts a result into an instance of T."""
uid = result[self._uid]
Expand All @@ -150,7 +150,7 @@ def _data(self) -> List[T]:
reload
"""
if self._cache is None:
self._cache = self._fetch()
self._cache = self.fetch()
return self._cache

@overload
Expand Down Expand Up @@ -213,4 +213,5 @@ def find_by(self, **conditions: Any) -> Optional[T]:
Optional[T]
The first record matching the conditions, or `None` if no match is found.
"""
return next((v for v in self._data if v.items() >= conditions.items()), None)
collection = self.fetch()
return next((v for v in collection if v.items() >= conditions.items()), None)

0 comments on commit 0360b6a

Please sign in to comment.