def request(self, method, url, **kwargs): if self.timeout and "timeout" not in kwargs: kwargs["timeout"] = self.timeout if "headers" not in kwargs: kwargs['headers'] = {} kwargs['headers']['X-Request-Group'] = uuid.uuid4().hex resp = super(WrappedSession, self).request( method, self.base_url + url, **kwargs ) if resp.status_code >= 200 and resp.status_code < 400: return resp elif resp.status_code == 400: raise BadRequestError(resp.text) elif resp.status_code == 404: text = resp.text if not text: text = "404 {} {}".format(method, url) raise NotFoundError(text) elif resp.status_code == 409: raise ConflictError(resp.text) elif resp.status_code == 429: raise RateLimitError(resp.text) elif resp.status_code == 504: raise GatewayTimeoutError( "Your request timed out on the server. " "Consider reducing the complexity of your request." ) else: raise ServerError(resp.text)
def read_blosc_array(metadata, data): output = np.empty(metadata['shape'], dtype=np.dtype(metadata['dtype'])) ptr = output.__array_interface__['data'][0] for _ in metadata['chunks']: raw_size, buffer = read_blosc_buffer(data) blosc.decompress_ptr(buffer, ptr) ptr += raw_size bytes_received = ptr - output.__array_interface__['data'][0] if bytes_received != output.nbytes: raise ServerError("Did not receive complete array (got {}, expected {})".format( bytes_received, output.nbytes)) return output
def request(self, method, url, **kwargs): if self.timeout and 'timeout' not in kwargs: kwargs['timeout'] = self.timeout resp = super(WrappedSession, self).request(method, self.base_url + url, **kwargs) if resp.status_code >= 200 and resp.status_code < 400: return resp elif resp.status_code == 400: raise BadRequestError(resp.text) elif resp.status_code == 404: raise NotFoundError("404 %s %s" % (method, url)) elif resp.status_code == 409: raise ConflictError(resp.text) elif resp.status_code == 429: raise RateLimitError(resp.text) elif resp.status_code == 504: raise GatewayTimeoutError( "Your request timed out on the server. " "Consider reducing the complexity of your request.") else: raise ServerError(resp.text)
def request(self, method, url, **kwargs): if self.timeout and self.ATTR_TIMEOUT not in kwargs: kwargs[self.ATTR_TIMEOUT] = self.timeout if self.ATTR_HEADERS not in kwargs: kwargs[self.ATTR_HEADERS] = {} kwargs[self.ATTR_HEADERS][HttpHeaderKeys.RequestGroup] = uuid.uuid4().hex resp = super(WrappedSession, self).request( method, self.base_url + url, **kwargs ) if resp.status_code >= 200 and resp.status_code < 400: return resp elif resp.status_code == 400: raise BadRequestError(resp.text) elif resp.status_code == 404: text = resp.text if not text: text = "404 {} {}".format(method, url) raise NotFoundError(text) elif resp.status_code == 409: raise ConflictError(resp.text) elif resp.status_code == 422: raise BadRequestError(resp.text) elif resp.status_code == 429: raise RateLimitError( resp.text, retry_after=resp.headers.get(HttpHeaderKeys.RetryAfter) ) elif resp.status_code == 504: raise GatewayTimeoutError( "Your request timed out on the server. " "Consider reducing the complexity of your request." ) else: raise ServerError(resp.text)
def request(self, method, url, **kwargs): """Sends an HTTP request and emits Descartes Labs specific errors. Parameters ---------- method: str The HTTP method to use. url: str The URL to send the request to. kwargs: dict Additional arguments. See `requests.request <https://requests.readthedocs.io/en/master/api/#requests.request>`_. Returns ------- Response A :py:class:`request.Response` object. Raises ------ BadRequestError Either a 400 or 422 HTTP response status code was encountered. NotFoundError A 404 HTTP response status code was encountered. ProxyAuthenticationRequiredError A 407 HTTP response status code was encountered and the resulting :py:meth:`handle_proxy_authentication` did not indicate that the proxy authentication was handled. ConflictError A 409 HTTP response status code was encountered. RateLimitError A 429 HTTP response status code was encountered. GatewayTimeoutError A 504 HTTP response status code was encountered. ServerError Any HTTP response status code larger than 400 that was not covered above is returned as a ServerError. The original HTTP response status code can be found in the attribute :py:attr:`original_status`. """ if self.timeout and self.ATTR_TIMEOUT not in kwargs: kwargs[self.ATTR_TIMEOUT] = self.timeout if self.ATTR_HEADERS not in kwargs: kwargs[self.ATTR_HEADERS] = {} kwargs[self.ATTR_HEADERS][ HttpHeaderKeys.RequestGroup] = uuid.uuid4().hex resp = super(Session, self).request(method, self.base_url + url, **kwargs) if (resp.status_code >= HttpStatusCode.Ok and resp.status_code < HttpStatusCode.BadRequest): return resp elif resp.status_code == HttpStatusCode.BadRequest: raise BadRequestError(resp.text) elif resp.status_code == HttpStatusCode.NotFound: text = resp.text if not text: text = "{} {} {}".format(HttpStatusCode.NotFound, method, url) raise NotFoundError(text) elif resp.status_code == HttpStatusCode.ProxyAuthenticationRequired: if not self.handle_proxy_authentication(method, url, **kwargs): raise ProxyAuthenticationRequiredError() elif resp.status_code == HttpStatusCode.Conflict: raise ConflictError(resp.text) elif resp.status_code == HttpStatusCode.UnprocessableEntity: raise BadRequestError(resp.text) elif resp.status_code == HttpStatusCode.TooManyRequests: raise RateLimitError(resp.text, retry_after=resp.headers.get( HttpHeaderKeys.RetryAfter)) elif resp.status_code == HttpStatusCode.GatewayTimeout: raise GatewayTimeoutError( "Your request timed out on the server. " "Consider reducing the complexity of your request.") else: # The whole error hierarchy has some problems. Originally a ClientError # could be thrown by our client libraries, but any HTTP error was a # ServerError. That changed and HTTP errors below 500 became ClientErrors. # That means that this actually should be split in ClientError for # status < 500 and ServerError for status >= 500, but that might break # things. So instead, we'll add the original status. server_error = ServerError(resp.text) server_error.original_status = resp.status_code raise server_error