def download_response(response: httpx.Response) -> None: console = rich.console.Console() syntax = rich.syntax.Syntax("", "http", theme="ansi_dark", word_wrap=True) console.print(syntax) filename = get_download_filename(response) content_length = response.headers.get("Content-Length") kwargs = {"total": int(content_length)} if content_length else {} with open(filename, mode="bw") as download_file: with rich.progress.Progress( "[progress.description]{task.description}", "[progress.percentage]{task.percentage:>3.0f}%", rich.progress.BarColumn(bar_width=None), rich.progress.DownloadColumn(), rich.progress.TransferSpeedColumn(), ) as progress: description = f"Downloading [bold]{filename}" download_task = progress.add_task(description, **kwargs) # type: ignore for chunk in response.iter_bytes(): download_file.write(chunk) progress.update(download_task, completed=response.num_bytes_downloaded)
def _do_raise_for_status(self, response: httpx.Response) -> None: try: response.raise_for_status() except httpx.HTTPError as error: content: ty.List[object] = [] try: decoder: encoding.Json = encoding.get_encoding("json") for chunk in response.iter_bytes(): content += list(decoder.parse_partial(chunk)) content += list(decoder.parse_finalize()) except exceptions.DecodingError: pass # If we have decoded an error response from the server, # use that as the exception message; otherwise, just pass # the exception on to the caller. if len(content) == 1 \ and isinstance(content[0], dict) \ and "Message" in content[0]: msg: str = content[0]["Message"] raise exceptions.ErrorResponse(msg, error) from error else: raise exceptions.StatusError(error) from error