def long_request(self, method, url_resource, **kwargs): headers = kwargs.get("headers", self.headers).copy() headers["Eyes-Expect"] = "202+location" headers["Eyes-Date"] = datetime_utils.current_time_in_rfc1123() kwargs["headers"] = headers response = self.request(method, url_resource, **kwargs) return self._long_request_check_status(response)
def _long_request_check_status(self, response, request_id): if ( response.status_code == requests.codes.ok or "Location" not in response.headers ): # request ends successful or it doesn't support Long request return response elif response.status_code == requests.codes.accepted: # long request here; calling received url to know that request was processed url = response.headers["Location"] response = self._long_request_loop(url, request_id) return self._long_request_check_status(response, request_id) elif response.status_code == requests.codes.created: # delete url that was used before url = response.headers["Location"] return self.request( "delete", url, request_id=request_id, headers={"Eyes-Date": datetime_utils.current_time_in_rfc1123()}, ) elif response.status_code == requests.codes.gone: raise EyesError("The server task has gone.") else: raise EyesError("Unknown error during long request: {}".format(response))
def long_request(self, method, url_resource, **kwargs): # type: (Text, Text, **Any) -> Response headers = kwargs.get("headers", self.headers).copy() headers["Eyes-Expect"] = "202+location" headers["Eyes-Date"] = datetime_utils.current_time_in_rfc1123() kwargs["headers"] = headers response = self.request(method, url_resource, **kwargs) logger.debug("Long request `{}` for {}".format(method, response.url)) return self._long_request_check_status(response)
def _long_request_loop(self, url, delay=LONG_REQUEST_DELAY_MS): delay = min( self.MAX_LONG_REQUEST_DELAY_MS, math.floor(delay * self.LONG_REQUEST_DELAY_MULTIPLICATIVE_INCREASE_FACTOR), ) logger.debug("Long request. Still running... Retrying in {} ms".format(delay)) datetime_utils.sleep(delay) response = self.request( "get", url, headers={"Eyes-Date": datetime_utils.current_time_in_rfc1123()}, ) if response.status_code != requests.codes.ok: return response return self._long_request_loop(url, delay)
def _long_request_loop(self, url, request_id): delays = delays_gen(500, 5, 2, 5000) delay = next(delays) while True: datetime_utils.sleep( delay, "Long request {} still running".format(request_id) ) response = self.request( "get", url, request_id=request_id, headers={"Eyes-Date": datetime_utils.current_time_in_rfc1123()}, ) if response.status_code == requests.codes.ok: url = response.headers.get("Location", url) if "Retry-After" in response.headers: delay = int(response.headers["Retry-After"]) * 1000 else: delay = next(delays) else: return response