Skip to content

Commit 01da449

Browse files
committed
refactor: removes cache layer from active sequence
1 parent 22842ea commit 01da449

File tree

2 files changed

+7
-65
lines changed

2 files changed

+7
-65
lines changed

src/posit/connect/resources.py

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Any, Generic, List, Optional, Sequence, TypeVar, overload
66

77
import requests
8-
from typing_extensions import Self
98

109
from .context import Context
1110
from .urls import Url
@@ -79,8 +78,6 @@ def __init__(self, ctx: Context, path: str, /, **attributes):
7978
class ActiveSequence(ABC, Generic[T], Sequence[T]):
8079
"""A sequence for any HTTP GET endpoint that returns a collection."""
8180

82-
_cache: Optional[List[T]]
83-
8481
def __init__(self, ctx: Context, path: str, uid: str = "guid"):
8582
"""A sequence abstraction for any HTTP GET endpoint that returns a collection.
8683
@@ -97,27 +94,16 @@ def __init__(self, ctx: Context, path: str, uid: str = "guid"):
9794
self._ctx = ctx
9895
self._path = path
9996
self._uid = uid
100-
self._cache = None
10197

10298
@abstractmethod
10399
def _create_instance(self, path: str, /, **kwargs: Any) -> T:
104100
"""Create an instance of 'T'."""
105101
raise NotImplementedError()
106102

107-
def reload(self) -> Self:
108-
"""Reloads the collection from Connect.
109-
110-
Returns
111-
-------
112-
Self
113-
"""
114-
self._cache = None
115-
return self
116-
117-
def _fetch(self) -> List[T]:
103+
def fetch(self) -> List[T]:
118104
"""Fetch the collection.
119105
120-
Fetches the collection directly from Connect. This operation does not effect the cache state.
106+
Fetches the collection from Connect.
121107
122108
Returns
123109
-------
@@ -134,42 +120,23 @@ def _to_instance(self, result: dict) -> T:
134120
path = posixpath.join(self._path, uid)
135121
return self._create_instance(path, **result)
136122

137-
@property
138-
def _data(self) -> List[T]:
139-
"""Get the collection.
140-
141-
Fetches the collection from Connect and caches the result. Subsequent invocations return the cached results unless the cache is explicitly reset.
142-
143-
Returns
144-
-------
145-
List[T]
146-
147-
See Also
148-
--------
149-
cached
150-
reload
151-
"""
152-
if self._cache is None:
153-
self._cache = self._fetch()
154-
return self._cache
155-
156123
@overload
157124
def __getitem__(self, index: int) -> T: ...
158125

159126
@overload
160127
def __getitem__(self, index: slice) -> Sequence[T]: ...
161128

162129
def __getitem__(self, index):
163-
return self._data[index]
130+
return self.fetch()[index]
164131

165132
def __len__(self) -> int:
166-
return len(self._data)
133+
return len(self.fetch())
167134

168135
def __str__(self) -> str:
169-
return str(self._data)
136+
return str(self.fetch())
170137

171138
def __repr__(self) -> str:
172-
return repr(self._data)
139+
return repr(self.fetch())
173140

174141

175142
class ActiveFinderMethods(ActiveSequence[T], ABC):
@@ -213,4 +180,4 @@ def find_by(self, **conditions: Any) -> Optional[T]:
213180
Optional[T]
214181
The first record matching the conditions, or `None` if no match is found.
215182
"""
216-
return next((v for v in self._data if v.items() >= conditions.items()), None)
183+
return next((v for v in self.fetch() if v.items() >= conditions.items()), None)

tests/posit/connect/test_jobs.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,6 @@ def test(self):
8787
assert job["key"] == "tHawGvHZTosJA2Dx"
8888

8989

90-
class TestJobsReload:
91-
@responses.activate
92-
def test(self):
93-
responses.get(
94-
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066",
95-
json=load_mock("v1/content/f2f37341-e21d-3d80-c698-a935ad614066.json"),
96-
)
97-
98-
mock_get = responses.get(
99-
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066/jobs",
100-
json=load_mock("v1/content/f2f37341-e21d-3d80-c698-a935ad614066/jobs.json"),
101-
)
102-
103-
c = Client("https://connect.example", "12345")
104-
content = c.content.get("f2f37341-e21d-3d80-c698-a935ad614066")
105-
106-
assert len(content.jobs) == 1
107-
assert mock_get.call_count == 1
108-
109-
content.jobs.reload()
110-
111-
assert len(content.jobs) == 1
112-
assert mock_get.call_count == 2
113-
114-
11590
class TestJobDestory:
11691
@responses.activate
11792
def test(self):

0 commit comments

Comments
 (0)