Skip to content

Commit

Permalink
Merge pull request #10 from u-ways/2.32.3.4
Browse files Browse the repository at this point in the history
2.32.3.4
  • Loading branch information
u-ways authored Aug 26, 2024
2 parents ab8ca5e + 4a3bd7a commit a5d680e
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 117 deletions.
29 changes: 11 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,12 @@ function to the client, and it will automatically set the `x-correlation-id` hea
```python
import uuid
import logging_http_client
import logging_http_client_config
import logging_http_client

def correlation_id_provider() -> str:
return str(uuid.uuid4())

logging_http_client_config.set_correlation_id_provider(correlation_id_provider)
logging_http_client.set_correlation_id_provider(correlation_id_provider)

logging_http_client.create().get('https://www.python.org')
# => The client will append the `x-correlation-id` header to the request
Expand Down Expand Up @@ -227,14 +226,13 @@ import logging
from requests import PreparedRequest

import logging_http_client
import logging_http_client_config


def custom_request_logging_hook(logger: logging.Logger, request: PreparedRequest):
logger.debug("Custom request logging for %s", request.url)


logging_http_client_config.set_custom_request_logging_hook(custom_request_logging_hook)
logging_http_client.set_custom_request_logging_hook(custom_request_logging_hook)

logging_http_client.create().get('https://www.python.org')

Expand All @@ -254,14 +252,13 @@ import logging
from requests import Response

import logging_http_client
import logging_http_client_config


def custom_response_logging_hook(logger: logging.Logger, response: Response):
logger.debug("Custom response logging for %s", response.url)


logging_http_client_config.set_custom_response_logging_hook(custom_response_logging_hook)
logging_http_client.set_custom_response_logging_hook(custom_response_logging_hook)

logging_http_client.create().get('https://www.python.org')

Expand All @@ -281,10 +278,9 @@ have custom logging hooks set.

```python
import logging_http_client
import logging_http_client_config

logging_http_client_config.disable_request_logging()
logging_http_client_config.disable_response_logging()
logging_http_client.disable_request_logging()
logging_http_client.disable_response_logging()

logging_http_client.create().get('https://www.python.org')
# => No request log record will be generated
Expand All @@ -298,10 +294,9 @@ or `enable_response_body_logging` methods respectively. This will log the reques

```python
import logging_http_client
import logging_http_client_config

logging_http_client_config.enable_request_body_logging()
logging_http_client_config.enable_response_body_logging()
logging_http_client.enable_request_body_logging()
logging_http_client.enable_response_body_logging()

logging_http_client.create().get('https://www.python.org')
# => Log record will include the request or response body (if present)
Expand All @@ -320,7 +315,6 @@ function will be called JUST BEFORE the request is logged.

```python
import logging_http_client
import logging_http_client_config
from logging_http_client import HttpLogRecord


Expand All @@ -331,7 +325,7 @@ def request_log_record_obscurer(record: HttpLogRecord) -> HttpLogRecord:
return record


logging_http_client_config.set_request_log_record_obscurer(request_log_record_obscurer)
logging_http_client.set_request_log_record_obscurer(request_log_record_obscurer)

logging_http_client.create().get(
url='https://www.python.org',
Expand All @@ -349,7 +343,6 @@ The obscurer function should take a `HttpLogRecord` object and expects to return

```python
import logging_http_client
import logging_http_client_config
from logging_http_client import HttpLogRecord


Expand All @@ -360,8 +353,8 @@ def response_log_record_obscurer(record: HttpLogRecord) -> HttpLogRecord:
return record


logging_http_client_config.set_response_log_record_obscurer(response_log_record_obscurer)
logging_http_client_config.enable_response_body_logging()
logging_http_client.set_response_log_record_obscurer(response_log_record_obscurer)
logging_http_client.enable_response_body_logging()

logging_http_client.create().get('https://www.python.org')
# Assume the response body contains "some response body with SENSITIVE information"
Expand Down
12 changes: 11 additions & 1 deletion logging_http_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,19 @@
# noinspection PyUnresolvedReferences
from requests.status_codes import codes # noqa: F401

# noinspection PyUnresolvedReferences
from .http_log_record import HttpLogRecord # noqa: F401
from .logging_http_client_class import LoggingHttpClient
from .logging_http_client_config import ( # noqa: F401
set_correlation_id_provider,
set_request_log_record_obscurer,
set_response_log_record_obscurer,
set_custom_request_logging_hook,
set_custom_response_logging_hook,
disable_request_logging,
disable_response_logging,
enable_request_body_logging,
enable_response_body_logging,
)


def create(
Expand Down
15 changes: 5 additions & 10 deletions logging_http_client/http_log_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

from requests.models import PreparedRequest, Response

from logging_http_client_config_globals import (
is_request_body_logging_enabled,
is_response_body_logging_enabled,
get_request_log_record_obscurer,
get_response_log_record_obscurer,
)
import logging_http_client.logging_http_client_config_globals as config

# Define Primitive type
Primitive = Union[int, float, str, bool]
Expand Down Expand Up @@ -51,13 +46,13 @@ def request_processor(source_system: str, request_id: str, request: PreparedRequ
record.request_query_params = request.params if hasattr(request, "params") else {}
record.request_headers = dict(request.headers) if request.headers else {}

if request.body and is_request_body_logging_enabled():
if request.body and config.is_request_body_logging_enabled():
if isinstance(request.body, bytes):
record.request_body = request.body.decode()
else:
record.request_body = request.body

obscurer = get_request_log_record_obscurer()
obscurer = config.get_request_log_record_obscurer()
record = obscurer(record) if obscurer is not None else record

return {"http": record.to_dict()}
Expand All @@ -71,10 +66,10 @@ def response_processor(request_id: str, response: Response) -> Dict[str, Any]:
record.response_headers = dict(response.headers) if response.headers else {}
record.response_duration_ms = int(response.elapsed.microseconds // 1000)

if response.content and is_response_body_logging_enabled():
if response.content and config.is_response_body_logging_enabled():
record.response_body = response.content.decode()

obscurer = get_response_log_record_obscurer()
obscurer = config.get_response_log_record_obscurer()
record = obscurer(record) if obscurer is not None else record

return {"http": record.to_dict()}
25 changes: 12 additions & 13 deletions logging_http_client/http_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

from requests import Session, Response, Request, PreparedRequest

from http_headers import X_SOURCE_HEADER, X_REQUEST_ID_HEADER, HEADERS_KWARG, X_CORRELATION_ID_HEADER
from http_log_record import HttpLogRecord
from logging_http_client_config_globals import (
is_request_logging_enabled,
is_response_logging_enabled,
get_custom_request_logging_hook,
get_custom_response_logging_hook,
get_correlation_id_provider,
import logging_http_client.logging_http_client_config_globals as config
from logging_http_client.http_headers import (
X_SOURCE_HEADER,
X_REQUEST_ID_HEADER,
HEADERS_KWARG,
X_CORRELATION_ID_HEADER,
)
from logging_http_client.http_log_record import HttpLogRecord


class LoggingSession(Session):
Expand Down Expand Up @@ -54,7 +53,7 @@ def _apply(**kwargs) -> Response:
request_id = kwargs[HEADERS_KWARG][X_REQUEST_ID_HEADER]
prepared.headers.update({X_REQUEST_ID_HEADER: request_id})

correlation_id_provider = get_correlation_id_provider()
correlation_id_provider = config.get_correlation_id_provider()
if correlation_id is None and correlation_id_provider is not None:
kwargs[HEADERS_KWARG][X_CORRELATION_ID_HEADER] = correlation_id_provider()
correlation_id = kwargs[HEADERS_KWARG][X_CORRELATION_ID_HEADER]
Expand All @@ -65,11 +64,11 @@ def _apply(**kwargs) -> Response:
source_system = kwargs[HEADERS_KWARG][X_SOURCE_HEADER]
prepared.headers.update({X_SOURCE_HEADER: source_system})

request_logging_hook = get_custom_request_logging_hook()
request_logging_hook = config.get_custom_request_logging_hook()
if request_logging_hook is not None:
request_logging_hook(logger, prepared)
else:
if is_request_logging_enabled():
if config.is_request_logging_enabled():
logger.info(
msg="REQUEST",
extra=HttpLogRecord.request_processor(
Expand All @@ -80,11 +79,11 @@ def _apply(**kwargs) -> Response:
# Call the original method... (with modified **kwargs)
response: Response = original_method(**kwargs)

response_logging_hook = get_custom_response_logging_hook()
response_logging_hook = config.get_custom_response_logging_hook()
if response_logging_hook is not None:
response_logging_hook(logger, response)
else:
if is_response_logging_enabled():
if config.is_response_logging_enabled():
logger.info(
msg="RESPONSE", extra=HttpLogRecord.response_processor(request_id=request_id, response=response)
)
Expand Down
6 changes: 3 additions & 3 deletions logging_http_client/logging_http_client_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import logging
from typing import Mapping

from http_headers import with_source_header
from http_methods import HttpMethod
from http_session import LoggingSession
from logging_http_client.http_headers import with_source_header
from logging_http_client.http_methods import HttpMethod
from logging_http_client.http_session import LoggingSession


class LoggingHttpClient:
Expand Down
22 changes: 11 additions & 11 deletions logging_http_client/logging_http_client_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

from requests import Response, PreparedRequest

import logging_http_client_config_globals
from logging_http_client import HttpLogRecord
import logging_http_client.logging_http_client_config_globals as config
from logging_http_client.http_log_record import HttpLogRecord

CorrelationIdProviderType = Optional[Callable[[], str]]

Expand All @@ -31,7 +31,7 @@ def set_correlation_id_provider(provider: CorrelationIdProviderType) -> None:
The provider should be a callable that returns a string.
"""
logging_http_client_config_globals.set_correlation_id_provider(provider)
config.set_correlation_id_provider(provider)


def set_request_log_record_obscurer(obscurer: RequestLogRecordObscurerType) -> None:
Expand All @@ -44,7 +44,7 @@ def set_request_log_record_obscurer(obscurer: RequestLogRecordObscurerType) -> N
request logger. When using the request obscurer, you are also responsible
for returning the log record in the correct data structure.
"""
logging_http_client_config_globals.set_request_log_record_obscurer(obscurer)
config.set_request_log_record_obscurer(obscurer)


def set_response_log_record_obscurer(obscurer: ResponseLogRecordObscurerType) -> None:
Expand All @@ -57,21 +57,21 @@ def set_response_log_record_obscurer(obscurer: ResponseLogRecordObscurerType) ->
response logger. When using the response obscurer, you are also responsible
for returning the log record in the correct data structure.
"""
logging_http_client_config_globals.set_response_log_record_obscurer(obscurer)
config.set_response_log_record_obscurer(obscurer)


def set_custom_request_logging_hook(hook: RequestHookType) -> None:
"""
Set a custom hook for logging all requests.
"""
logging_http_client_config_globals.set_custom_request_logging_hook(hook)
config.set_custom_request_logging_hook(hook)


def set_custom_response_logging_hook(hook: ResponseHookType) -> None:
"""
Set a custom hook for logging all responses.
"""
logging_http_client_config_globals.set_custom_response_logging_hook(hook)
config.set_custom_response_logging_hook(hook)


def disable_request_logging(disabled: bool = True) -> None:
Expand All @@ -82,7 +82,7 @@ def disable_request_logging(disabled: bool = True) -> None:
These has no effect if you have set up custom logging hooks. (i.e.
these are for modifying the default logging setup)
"""
logging_http_client_config_globals.set_request_logging_enabled(not disabled)
config.set_request_logging_enabled(not disabled)


def disable_response_logging(disabled: bool = True) -> None:
Expand All @@ -93,7 +93,7 @@ def disable_response_logging(disabled: bool = True) -> None:
These has no effect if you have set up custom logging hooks. (i.e.
these are for modifying the default logging setup)
"""
logging_http_client_config_globals.set_response_logging_enabled(not disabled)
config.set_response_logging_enabled(not disabled)


def enable_request_body_logging(enable: bool = True) -> None:
Expand All @@ -104,7 +104,7 @@ def enable_request_body_logging(enable: bool = True) -> None:
These has no effect if you have set up custom logging hooks. (i.e.
these are for modifying the default logging setup)
"""
logging_http_client_config_globals.set_request_body_logging_enabled(enable)
config.set_request_body_logging_enabled(enable)


def enable_response_body_logging(enable: bool = True) -> None:
Expand All @@ -115,4 +115,4 @@ def enable_response_body_logging(enable: bool = True) -> None:
These has no effect if you have set up custom logging hooks. (i.e.
these are for modifying the default logging setup)
"""
logging_http_client_config_globals.set_response_body_logging_enabled(enable)
config.set_response_body_logging_enabled(enable)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "logging-http-client"
version = "2.32.3.3"
version = "2.32.3.4"
description = "A logging library built on top of the requests library to provide a familiar interface for sending HTTP requests."
authors = ["u-ways <work@u-ways.info>"]
packages = [{ include = "logging_http_client", from = "." }]
Expand Down
Loading

0 comments on commit a5d680e

Please sign in to comment.