def _download_in_chunks(self, response: HTTPResponse, sink): if response.status != 200: raise urllib3.exceptions.HTTPError( f"Bad HTTP Response: {response.status}") text_content = isinstance(sink, io.TextIOBase) total_size = 0 if text_content: encoding = parse_charset_from_content_type( response.getheader('Content-Type'), 'utf-8') reader = codecs.getreader(encoding)(response) while True: chunk = reader.read(chars=2**16) if chunk: sink.write(chunk) total_size += len(chunk) else: break else: for chunk in response.stream(2**16, True): sink.write(chunk) total_size += len(chunk) sink.flush() _log.info( f"Downloaded Content's length ({'in chars' if text_content else 'in bytes'}: ({total_size}" )
def __init__(self, response: HTTPResponse = None, message: str = None): """Initialize the InfluxDBError handler.""" if response is not None: self.response = response self.message = self._get_message(response) self.retry_after = response.getheader('Retry-After') else: self.response = None self.message = message or 'no response' self.retry_after = None super().__init__(self.message)