X-Git-Url: http://git.scottworley.com/paperdoorknob/blobdiff_plain/705973e749e5a9da0453508dbd23de434a0fdc65..cfd0b634e21851a0d9e8b4c8c258307baefcd931:/fetch.py?ds=sidebyside diff --git a/fetch.py b/fetch.py index 3c0c6a3..d526753 100644 --- a/fetch.py +++ b/fetch.py @@ -41,3 +41,19 @@ def DirectFetcher(timeout: int) -> Iterator[_SessionFetcher]: def CachingFetcher(cache_path: str, timeout: int) -> Iterator[_SessionFetcher]: with requests_cache.CachedSession(cache_path, cache_control=True) as session: yield _SessionFetcher(session, timeout) + + +class FakeFetcher(Fetcher): + + def __init__(self, resources: dict[str, bytes]) -> None: + self._resources = resources + self._fetch_count = 0 + + def fetch(self, url: str) -> bytes: + self._fetch_count += 1 + if url not in self._resources: + raise requests.HTTPError("URL not found") + return self._resources[url] + + def request_count(self) -> int: + return self._fetch_count