Skip to content

Commit

Permalink
feat: set up logging of http requests (#250)
Browse files Browse the repository at this point in the history
Fixes: #156
  • Loading branch information
afuetterer authored Jan 25, 2024
1 parent 81366d8 commit 3df5ba1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/oaipmh_scythe/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from oaipmh_scythe.iterator import BaseOAIIterator, OAIItemIterator
from oaipmh_scythe.models import Header, Identify, MetadataFormat, OAIItem, Record, Set
from oaipmh_scythe.response import OAIResponse
from oaipmh_scythe.utils import filter_dict_except_resumption_token, remove_none_values
from oaipmh_scythe.utils import filter_dict_except_resumption_token, log_response, remove_none_values

if TYPE_CHECKING:
from collections.abc import Iterable, Iterator
Expand Down Expand Up @@ -113,7 +113,7 @@ def client(self) -> httpx.Client:
"""
if self._client is None or self._client.is_closed:
headers = {"Accept": "text/xml; charset=utf-8", "user-agent": USER_AGENT}
self._client = httpx.Client(headers=headers, timeout=self.timeout)
self._client = httpx.Client(headers=headers, timeout=self.timeout, event_hooks={"response": [log_response]})
return self._client

def close(self) -> None:
Expand Down
19 changes: 19 additions & 0 deletions src/oaipmh_scythe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
more accessible data structures.
Functions:
log_response: Log the details of an HTTP response.
remove_none_values: Remove keys from the dictionary where the value is `None`.
filter_dict_except_resumption_token: Filter keys from the dictionary, if resumption token is not `None`.
get_namespace: Extracts the namespace from an XML element.
Expand All @@ -26,11 +27,29 @@
if TYPE_CHECKING:
from typing import Any

import httpx
from lxml import etree

logger = logging.getLogger(__name__)


def log_response(response: httpx.Response) -> None:
"""Log the details of an HTTP response.
This function logs the HTTP method, URL, and status code of the response for debugging purposes.
It uses the 'debug' logging level to provide detailed diagnostic information.
Args:
response: The response object received from an HTTP request.
Returns:
None
"""
logger.debug(
"[http] Response: %s %s - Status %s", response.request.method, response.request.url, response.status_code
)


def remove_none_values(d: dict[str, Any | None]) -> dict[str, Any]:
"""Remove keys from the dictionary where the value is `None`.
Expand Down

0 comments on commit 3df5ba1

Please sign in to comment.