Skip to content

Commit 7eeeafc

Browse files
committed
Do not fetch all data immediately. Instead, use generator
1 parent 7922f93 commit 7eeeafc

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/posit/connect/_api.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ def __repr__(self) -> str:
160160

161161

162162
class ApiListEndpoint(ApiCallMixin, Generic[T], ABC, object):
163-
"""A tuple for any HTTP GET endpoint that returns a collection."""
164-
165-
_data: tuple[T, ...]
163+
"""A HTTP GET endpoint that can fetch a collection."""
166164

167165
def _get_api(self, *, extra_endpoint: str = "") -> tuple[JsonifiableDict, ...]:
168166
vals: Jsonifiable = super()._get_api(extra_endpoint=extra_endpoint)
@@ -184,15 +182,13 @@ def __init__(self, *, ctx: Context, path: str, uid_key: str = "guid") -> None:
184182
self._ctx = ctx
185183
self._path = path
186184
self._uid_key = uid_key
187-
# # TODO-barret; Key component! Fetch the data immediately?
188-
# self._data = self._fetch_data()
189185

190186
@abstractmethod
191187
def _create_instance(self, path: str, /, **kwargs: Any) -> T:
192188
"""Create an instance of 'T'."""
193189
raise NotImplementedError()
194190

195-
def fetch(self) -> tuple[T, ...]:
191+
def fetch(self) -> Generator[T, None, None]:
196192
"""Fetch the collection.
197193
198194
Fetches the collection directly from Connect. This operation does not effect the cache state.
@@ -202,10 +198,10 @@ def fetch(self) -> tuple[T, ...]:
202198
List[T]
203199
"""
204200
results = self._get_api()
205-
return tuple(self._to_instance(result) for result in results)
201+
for result in results:
202+
yield self._to_instance(result)
206203

207-
# Return a `tuple` to provide immutability to hint that this is a read-only collection.
208-
def __iter__(self) -> tuple[T, ...]:
204+
def __iter__(self) -> Generator[T, None, None]:
209205
return self.fetch()
210206

211207
def _to_instance(self, result: dict) -> T:
@@ -214,14 +210,19 @@ def _to_instance(self, result: dict) -> T:
214210
path = posixpath.join(self._path, uid)
215211
return self._create_instance(path, **result)
216212

217-
# @overload
218-
# def __getitem__(self, index: int) -> T: ...
213+
@overload
214+
def __getitem__(self, index: int) -> T: ...
219215

220-
# @overload
221-
# def __getitem__(self, index: slice) -> Sequence[T]: ...
216+
@overload
217+
def __getitem__(self, index: slice) -> Generator[T, None, None]: ...
222218

223-
# def __getitem__(self, index: int | slice) -> T | Sequence[T]:
224-
# return self.fetch()[index]
219+
def __getitem__(self, index: int | slice) -> T | Generator[T, None, None]:
220+
if isinstance(index, slice):
221+
results = itertools.islice(self.fetch(), index.start, index.stop, index.step)
222+
for result in results:
223+
yield result
224+
else:
225+
return list(itertools.islice(self.fetch(), index, index + 1))[0]
225226

226227
# def __len__(self) -> int:
227228
# return len(self.fetch())

0 commit comments

Comments
 (0)