API Reference#
Main Interface#
There are three ways to use this library.
Use
activate_httpclient_pretty()to activate pretty headers for the whole codebase.Use
HttpClientPrettyHeadersas a context manager (seeHttpClientPrettyHeaders.__enter__()) to activate pretty headers for the designated part of the codebase.Use
httpclient_print_func_closure()to replace the print function manually.
- class HttpClientPrettyHeaders(output: AbstractOutput | None = None)#
Primary interface to get pretty classes in http.client.
Acts as a context manager (see
__enter__()) and provides low level closure print function (seehttpclient_print_func_closure()) to manually replace the print function ofhttp.client.- Parameters:
output (AbstractOutput, optional) – An instance of AbstractOutput to use for logging. If not provided,
default_output_classwill be used by default.
- __enter__() None#
Sets the print function of the
http.clientmodule.This isn’t called directly but used automatically when using this class within a with statement.
Usage:
>>> with HttpClientPrettyHeaders(): >>> # Your code that uses http.client >>> pass
See also
Alternative ways to set the print function:
activate_httpclient_pretty()when you want to activate pretty headers for large codebaseHttpClientPrettyHeaders.httpclient_print_func_closure()when you want to replace the print function manually
- __exit__(_not_used_exc_type: BaseException | None, _not_used_exc_value: BaseException | None, _not_used_traceback: TracebackType | None) None#
Removes the print function from
http.client.This is complementary to
HttpClientPrettyHeaders.__enter__(). This shouldn’t be called directly.
- __init__(output: AbstractOutput | None = None)#
- default_output_class#
The default output class to use if no output is provided.
alias of
PrintOutput
- httpclient_print_func_closure() Callable[[...], None]#
Closure function to replace the print function of http.client.
Usage:
>>> http.client.print = HttpClientPrettyHeaders().httpclient_print_func_closure()
See also
Alternative ways to set the print function:
activate_httpclient_pretty()when you want to activate pretty headers for large codebaseHttpClientPrettyHeaders.__enter__()when only designated part of your code should have pretty headers
- property output: AbstractOutput#
- activate_httpclient_pretty(output: AbstractOutput | None = None) None#
Simple way to activate or reactivate print function for
http.client.- Parameters:
output (AbstractOutput, optional) – An object that implements
AbstractOutputto use for logging. If not provided,HttpClientPrettyHeaders.default_output_classwill be used by default.
Usage:
>>> activate_httpclient_pretty() >>> # Your code that uses http.client >>> deactivate_httpclient_pretty()
See also
deactivate_httpclient_pretty()to deactivate it.HttpClientPrettyHeaders.__enter__()when only designated part of your code should have pretty headersHttpClientPrettyHeaders.httpclient_print_func_closure()when you want to replace the print function manually
- deactivate_httpclient_pretty() None#
Deactivate the print function for
http.client.This is complementary to
activate_httpclient_pretty().
Alternative Output#
- class LoggingOutput(logger_obj: Logger)#
Alterantive output to use with standard logging module.
Usage:
>>> # Usual initialization of logger >>> logger = logging.getLogger(__name__) >>> logger.setLevel(logging.DEBUG) >>> # Create an output object and pass it to the logger >>> output = LoggingOutput(logger) >>> htcph = HttpClientPrettyHeaders(output) >>> # a) use the pretty headers within a context manager: >>> with htcph: >>> # Your code that uses http.client >>> pass >>> # b) activate the code for the whole codebase: >>> activate_httpclient_pretty(output) >>> # Your code that uses http.client
- Parameters:
logger_obj (logging.Logger) – The logger object to use for logging.