Skip to content

Commit

Permalink
refactor: removes cache layer from active sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
tdstein committed Nov 4, 2024
1 parent 22842ea commit 01da449
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 65 deletions.
47 changes: 7 additions & 40 deletions src/posit/connect/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
-------
Expand All @@ -134,42 +120,23 @@ 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: ...

@overload
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):
Expand Down Expand Up @@ -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)
25 changes: 0 additions & 25 deletions tests/posit/connect/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 01da449

Please sign in to comment.