Skip to content

Commit f7e3daf

Browse files
authored
Fix cache interaction with non 200 responses
1 parent 26c2aba commit f7e3daf

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

coc/http.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,23 @@ async def request(self, route, **kwargs):
264264
# the cache will be cleaned once it becomes stale / a new object is available from the api.
265265
if isinstance(cache, FIFO) and not self.client.realtime and not kwargs.get("ignore_cache", False):
266266
try:
267-
return cache[cache_control_key]
267+
data = cache[cache_control_key]
268+
status_code = data.get("status_code")
269+
if data.get("timestamp") and data.get("timestamp") + data.get("_response_retry", 0) < datetime.utcnow().timestamp():
270+
pass
271+
elif not status_code or 200 <= status_code < 300:
272+
return data
273+
elif status_code == 400:
274+
raise InvalidArgument(400, data)
275+
276+
elif status_code == 403:
277+
raise Forbidden(403, data)
278+
279+
elif status_code == 404:
280+
raise NotFound(404, data)
281+
282+
elif status_code == 503:
283+
raise Maintenance(503, data)
268284
except KeyError:
269285
pass
270286

@@ -281,7 +297,8 @@ async def request(self, route, **kwargs):
281297

282298
LOG.debug("API HTTP Request: %s", str(log_info))
283299
data = await json_or_text(response)
284-
300+
data["status_code"] = response.status
301+
data["timestamp"] = datetime.utcnow().timestamp()
285302
try:
286303
# set a callback to remove the item from cache once it's stale.
287304
delta = int(response.headers["Cache-Control"].strip("max-age=").strip("public max-age="))

0 commit comments

Comments
 (0)