@@ -264,7 +264,20 @@ 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+ self ._cache_remove (cache_control_key )
271+ elif not status_code or 200 <= status_code < 300 :
272+ return data
273+ elif status_code == 400 :
274+ raise InvalidArgument (400 , data )
275+ elif status_code == 403 :
276+ raise Forbidden (403 , data )
277+ elif status_code == 404 :
278+ raise NotFound (404 , data )
279+ elif status_code == 503 :
280+ raise Maintenance (503 , data )
268281 except KeyError :
269282 pass
270283
@@ -281,7 +294,8 @@ async def request(self, route, **kwargs):
281294
282295 LOG .debug ("API HTTP Request: %s" , str (log_info ))
283296 data = await json_or_text (response )
284-
297+ data ["status_code" ] = response .status
298+ data ["timestamp" ] = datetime .utcnow ().timestamp ()
285299 try :
286300 # set a callback to remove the item from cache once it's stale.
287301 delta = int (response .headers ["Cache-Control" ].strip ("max-age=" ).strip ("public max-age=" ))
0 commit comments