Skip to content

Commit

Permalink
Don't cache GraphQL errors
Browse files Browse the repository at this point in the history
Some of them are transient.
  • Loading branch information
dseomn committed Oct 19, 2023
1 parent f9c2c48 commit 692e63c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion rock_paper_sand/justwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ def _parse_datetime(raw_value: str | None) -> datetime.datetime | None:
return dateutil.parser.isoparse(raw_value)


def _response_is_ok(response: requests.Response) -> bool:
return "errors" not in response.json()


@contextlib.contextmanager
def requests_session() -> Generator[requests.Session, None, None]:
"""Returns a context manager for a session for the JustWatch API."""
with requests_cache.CachedSession(
**network.requests_cache_defaults(),
expire_after=datetime.timedelta(hours=20),
allowable_methods=("GET", "HEAD", "POST"),
filter_fn=_response_is_ok,
) as session:
network.configure_session(session, additional_retry_methods=("POST",))
yield session
Expand Down Expand Up @@ -100,7 +105,7 @@ def query(
with exceptions.add_note(f"Response body: {response.text}"):
response.raise_for_status()
response_json = response.json()
if "errors" in response_json:
if not _response_is_ok(response):
raise ValueError(f"GraphQL query failed: {response_json}")
return response_json

Expand Down

0 comments on commit 692e63c

Please sign in to comment.