API Reference#

Main Interface#

There are three ways to use this library.

  1. Use activate_httpclient_pretty() to activate pretty headers for the whole codebase.

  2. Use HttpClientPrettyHeaders as a context manager (see HttpClientPrettyHeaders.__enter__()) to activate pretty headers for the designated part of the codebase.

  3. 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 (see httpclient_print_func_closure()) to manually replace the print function of http.client.

Parameters:

output (AbstractOutput, optional) – An instance of AbstractOutput to use for logging. If not provided, default_output_class will be used by default.

__enter__() None#

Sets the print function of the http.client module.

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 codebase HttpClientPrettyHeaders.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 codebase HttpClientPrettyHeaders.__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 AbstractOutput to use for logging. If not provided, HttpClientPrettyHeaders.default_output_class will 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 headers HttpClientPrettyHeaders.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.

error(message: str) None#

Log an error message.

Parameters:

message – The error message to log.

log(message: str) None#

Log a message.

Parameters:

message – The message to log.

Lower level Output#

class AbstractOutput#

Common interface for output classes.

Primarily for typing and to show what methods are required.

abstract error(message: str) None#

Log an error message.

Parameters:

message – The error message to log.

abstract log(message: str) None#

Log a message.

Parameters:

message – The message to log.

class PrintOutput#

Standard output of headers using print function.

error(message: str) None#

Log an error message.

Parameters:

message – The error message to log.

log(message: str) None#

Log a message.

Parameters:

message – The message to log.