From 3df5ba1c3247b38bc8fa0414b070296fc309a5ef Mon Sep 17 00:00:00 2001 From: Heinz-Alexander Fuetterer <35225576+afuetterer@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:35:53 +0100 Subject: [PATCH] feat: set up logging of http requests (#250) Fixes: #156 --- src/oaipmh_scythe/client.py | 4 ++-- src/oaipmh_scythe/utils.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/oaipmh_scythe/client.py b/src/oaipmh_scythe/client.py index 0f9cc81..8ad86d7 100644 --- a/src/oaipmh_scythe/client.py +++ b/src/oaipmh_scythe/client.py @@ -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 @@ -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: diff --git a/src/oaipmh_scythe/utils.py b/src/oaipmh_scythe/utils.py index 3693c75..26ba399 100644 --- a/src/oaipmh_scythe/utils.py +++ b/src/oaipmh_scythe/utils.py @@ -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. @@ -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`.