From 0360b6a4369154b21676ce117f1e76971daf2db0 Mon Sep 17 00:00:00 2001 From: tdstein Date: Mon, 4 Nov 2024 14:30:10 -0500 Subject: [PATCH] refactor: remove cache layer from finder methods --- src/posit/connect/resources.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/posit/connect/resources.py b/src/posit/connect/resources.py index 4119b3c5..6cdb628f 100644 --- a/src/posit/connect/resources.py +++ b/src/posit/connect/resources.py @@ -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. @@ -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] @@ -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 @@ -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)