|
31 | 31 | import json |
32 | 32 | import logging |
33 | 33 | import re |
| 34 | +import sys |
| 35 | +from datetime import datetime |
| 36 | +from datetime import timezone |
34 | 37 | from re import Match |
35 | 38 | from typing import TYPE_CHECKING |
36 | 39 | from typing import Any |
| 40 | +from typing import Callable |
37 | 41 |
|
38 | 42 | import requests # type: ignore[import-untyped] |
39 | 43 | from requests.compat import urljoin # type: ignore[import-untyped] |
40 | 44 |
|
41 | | -from .utils.version import get_version |
| 45 | +from mailjet_rest.utils.version import get_version |
42 | 46 |
|
43 | 47 |
|
44 | 48 | if TYPE_CHECKING: |
@@ -548,28 +552,71 @@ def build_url( |
548 | 552 | return url |
549 | 553 |
|
550 | 554 |
|
551 | | -def parse_response(response: Response, debug: bool = False) -> Any: |
552 | | - """Parse the response from an API request. |
| 555 | +def logging_handler( |
| 556 | + to_file: bool = False, |
| 557 | +) -> logging.Logger: |
| 558 | + """Create and configure a logger for logging API requests. |
553 | 559 |
|
554 | | - This function extracts the JSON data from the response and logs debug information if the `debug` flag is set to True. |
| 560 | + This function creates a logger object and configures it to handle both |
| 561 | + standard output (stdout) and a file if the `to_file` parameter is set to True. |
| 562 | + The logger is set to log at the DEBUG level and uses a custom formatter to |
| 563 | + include the log level and message. |
555 | 564 |
|
556 | 565 | Parameters: |
557 | | - response (requests.models.Response): The response object from the API request. |
558 | | - debug (bool, optional): A flag indicating whether debug information should be logged. Defaults to False. |
| 566 | + to_file (bool): A flag indicating whether to log to a file. If True, logs will be written to a file. |
| 567 | + Defaults to False. |
559 | 568 |
|
560 | 569 | Returns: |
561 | | - Any: The JSON data extracted from the response. |
| 570 | + logging.Logger: A configured logger object for logging API requests. |
| 571 | + """ |
| 572 | + logger = logging.getLogger() |
| 573 | + logger.setLevel(logging.DEBUG) |
| 574 | + formatter = logging.Formatter("%(levelname)s | %(message)s") |
| 575 | + |
| 576 | + if to_file: |
| 577 | + now = datetime.now(tz=timezone.utc) |
| 578 | + date_time = now.strftime("%Y%m%d_%H%M%S") |
| 579 | + |
| 580 | + log_file = f"{date_time}.log" |
| 581 | + file_handler = logging.FileHandler(log_file) |
| 582 | + file_handler.setFormatter(formatter) |
| 583 | + logger.addHandler(file_handler) |
| 584 | + |
| 585 | + stdout_handler = logging.StreamHandler(sys.stdout) |
| 586 | + stdout_handler.setFormatter(formatter) |
| 587 | + logger.addHandler(stdout_handler) |
| 588 | + |
| 589 | + return logger |
| 590 | + |
| 591 | + |
| 592 | +def parse_response( |
| 593 | + response: Response, |
| 594 | + log: Callable, |
| 595 | + debug: bool = False, |
| 596 | +) -> Any: |
| 597 | + """Parse the response from an API request and return the JSON data. |
| 598 | +
|
| 599 | + Parameters: |
| 600 | + response (Response): The response object from the API request. |
| 601 | + log (Callable): A function or method that logs debug information. |
| 602 | + debug (bool): A flag indicating whether debug mode is enabled. Defaults to False. |
| 603 | +
|
| 604 | + Returns: |
| 605 | + Any: The JSON data from the API response. |
562 | 606 | """ |
563 | 607 | data = response.json() |
564 | 608 |
|
565 | 609 | if debug: |
566 | | - logging.debug("REQUEST: %s", response.request.url) |
567 | | - logging.debug("REQUEST_HEADERS: %s", response.request.headers) |
568 | | - logging.debug("REQUEST_CONTENT: %s", response.request.body) |
569 | | - |
570 | | - logging.debug("RESPONSE: %s", response.content) |
571 | | - logging.debug("RESP_HEADERS: %s", response.headers) |
572 | | - logging.debug("RESP_CODE: %s", response.status_code) |
| 610 | + lgr = log() |
| 611 | + lgr.debug("REQUEST: %s", response.request.url) |
| 612 | + lgr.debug("REQUEST_HEADERS: %s", response.request.headers) |
| 613 | + lgr.debug("REQUEST_CONTENT: %s", response.request.body) |
| 614 | + |
| 615 | + lgr.debug("RESPONSE: %s", response.content) |
| 616 | + lgr.debug("RESP_HEADERS: %s", response.headers) |
| 617 | + lgr.debug("RESP_CODE: %s", response.status_code) |
| 618 | + # Clear logger handlers to prevent making log duplications |
| 619 | + logging.getLogger().handlers.clear() |
573 | 620 |
|
574 | 621 | return data |
575 | 622 |
|
|
0 commit comments