diff --git a/src/posit/connect/resources.py b/src/posit/connect/resources.py index 4119b3c5..6b21bdb6 100644 --- a/src/posit/connect/resources.py +++ b/src/posit/connect/resources.py @@ -5,7 +5,6 @@ from typing import Any, Generic, List, Optional, Sequence, TypeVar, overload import requests -from typing_extensions import Self from .context import Context from .urls import Url @@ -79,8 +78,6 @@ def __init__(self, ctx: Context, path: str, /, **attributes): class ActiveSequence(ABC, Generic[T], Sequence[T]): """A sequence for any HTTP GET endpoint that returns a collection.""" - _cache: Optional[List[T]] - def __init__(self, ctx: Context, path: str, uid: str = "guid"): """A sequence abstraction for any HTTP GET endpoint that returns a collection. @@ -97,27 +94,16 @@ def __init__(self, ctx: Context, path: str, uid: str = "guid"): self._ctx = ctx self._path = path self._uid = uid - self._cache = None @abstractmethod 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. + Fetches the collection from Connect. Returns ------- @@ -134,25 +120,6 @@ def _to_instance(self, result: dict) -> T: path = posixpath.join(self._path, uid) return self._create_instance(path, **result) - @property - def _data(self) -> List[T]: - """Get the collection. - - Fetches the collection from Connect and caches the result. Subsequent invocations return the cached results unless the cache is explicitly reset. - - Returns - ------- - List[T] - - See Also - -------- - cached - reload - """ - if self._cache is None: - self._cache = self._fetch() - return self._cache - @overload def __getitem__(self, index: int) -> T: ... @@ -160,16 +127,16 @@ def __getitem__(self, index: int) -> T: ... def __getitem__(self, index: slice) -> Sequence[T]: ... def __getitem__(self, index): - return self._data[index] + return self.fetch()[index] def __len__(self) -> int: - return len(self._data) + return len(self.fetch()) def __str__(self) -> str: - return str(self._data) + return str(self.fetch()) def __repr__(self) -> str: - return repr(self._data) + return repr(self.fetch()) class ActiveFinderMethods(ActiveSequence[T], ABC): @@ -213,4 +180,4 @@ 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) + return next((v for v in self.fetch() if v.items() >= conditions.items()), None) diff --git a/tests/posit/connect/test_jobs.py b/tests/posit/connect/test_jobs.py index 252bd2e8..6e6bc0c5 100644 --- a/tests/posit/connect/test_jobs.py +++ b/tests/posit/connect/test_jobs.py @@ -87,31 +87,6 @@ def test(self): assert job["key"] == "tHawGvHZTosJA2Dx" -class TestJobsReload: - @responses.activate - def test(self): - responses.get( - "https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066", - json=load_mock("v1/content/f2f37341-e21d-3d80-c698-a935ad614066.json"), - ) - - mock_get = responses.get( - "https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066/jobs", - json=load_mock("v1/content/f2f37341-e21d-3d80-c698-a935ad614066/jobs.json"), - ) - - c = Client("https://connect.example", "12345") - content = c.content.get("f2f37341-e21d-3d80-c698-a935ad614066") - - assert len(content.jobs) == 1 - assert mock_get.call_count == 1 - - content.jobs.reload() - - assert len(content.jobs) == 1 - assert mock_get.call_count == 2 - - class TestJobDestory: @responses.activate def test(self):