diff --git a/packages/abstractions/kiota_abstractions/request_response_logger.py b/packages/abstractions/kiota_abstractions/request_response_logger.py new file mode 100644 index 0000000..40b01d6 --- /dev/null +++ b/packages/abstractions/kiota_abstractions/request_response_logger.py @@ -0,0 +1,11 @@ +from abc import ABC, abstractmethod +from typing import Any, Dict + +class RequestResponseLogger(ABC): + @abstractmethod + def log_request(self, request_data: Dict[str, Any]) -> None: + pass + + @abstractmethod + def log_response(self, response_data: Dict[str, Any]) -> None: + pass \ No newline at end of file diff --git a/packages/http/httpx/kiota_http/httpx_request_adapter.py b/packages/http/httpx/kiota_http/httpx_request_adapter.py index 533d085..712421d 100644 --- a/packages/http/httpx/kiota_http/httpx_request_adapter.py +++ b/packages/http/httpx/kiota_http/httpx_request_adapter.py @@ -11,6 +11,7 @@ ) from kiota_abstractions.api_error import APIError from kiota_abstractions.authentication import AuthenticationProvider +from kiota_abstractions.response_handler import RespondseHandler from kiota_abstractions.request_adapter import RequestAdapter, ResponseType from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.serialization import ( @@ -42,6 +43,7 @@ from kiota_http.middleware.parameters_name_decoding_handler import ParametersNameDecodingHandler from ._version import VERSION +from .httpx_request_response_logger import HttpxRequestResponseLogger from .kiota_client_factory import KiotaClientFactory from .middleware import ParametersNameDecodingHandler from .middleware.options import ParametersNameDecodingHandlerOption, ResponseHandlerOption @@ -91,6 +93,7 @@ def __init__( if not observability_options: observability_options = ObservabilityOptions() self.observability_options = observability_options + self.logger = HttpxRequestResponseLogger() @property def base_url(self) -> str: @@ -525,6 +528,20 @@ async def get_http_response_message( self.set_base_url_for_request_information(request_info) + self.logger.log_request({ + "url": url, + "method": request.method, + "headers": headers, + "query_params": query_params, + "content": content, + }) + + self.logger.log_response({ + "status_code": response.status_code, + "headers": dict(response.headers), + "content": response.text, + }) + additional_authentication_context = {} if claims: additional_authentication_context[self.CLAIMS_KEY] = claims diff --git a/packages/http/httpx/kiota_http/httpx_request_response_logger.py b/packages/http/httpx/kiota_http/httpx_request_response_logger.py new file mode 100644 index 0000000..884044c --- /dev/null +++ b/packages/http/httpx/kiota_http/httpx_request_response_logger.py @@ -0,0 +1,13 @@ +from kiota_abstractions import RequestResponseLogger +from typing import Any, Dict +import logging + +class HttpxRequestResponseLogger(RequestResponseLogger): + def __init__(self): + self.logger = logging.getLogger(__name__) + + def log_request(self, request_data: Dict[str, Any]) -> None: + self.logger.info(f"HTTP Request: {request_data}") + + def log_response(self, response_data: Dict[str, Any]) -> None: + self.logger.info(f"HTTP Response: {response_data}") \ No newline at end of file diff --git a/packages/serialization/json/kiota_serialization_json/json_serialization_writer.py b/packages/serialization/json/kiota_serialization_json/json_serialization_writer.py index 00e66bc..0d0ef44 100644 --- a/packages/serialization/json/kiota_serialization_json/json_serialization_writer.py +++ b/packages/serialization/json/kiota_serialization_json/json_serialization_writer.py @@ -387,6 +387,10 @@ def get_serialized_content(self) -> bytes: stream = json_string.encode('utf-8') return stream + @staticmethod + def serialize_object_to_json_string(obj: Dict[str, Any]) -> str: + return json.dumps(obj, indent=2) + @property def on_before_object_serialization(self) -> Optional[Callable[[Parsable], None]]: """Gets the callback called before the object gets serialized.