Skip to content

Commit

Permalink
Add API response tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
dvx76 committed Sep 21, 2024
1 parent a9ec3b6 commit 987b23f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion myskoda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
user,
)
from .mqtt import Mqtt
from .myskoda import MySkoda
from .myskoda import TRACE_CONFIG, MySkoda
from .rest_api import RestApi
from .vehicle import Vehicle

Expand All @@ -44,4 +44,5 @@
"Mqtt",
"Vehicle",
"user",
"TRACE_CONFIG",
]
7 changes: 5 additions & 2 deletions myskoda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
OpenState,
)
from .models.operation_request import OperationName, OperationStatus
from .myskoda import MySkoda
from .myskoda import TRACE_CONFIG, MySkoda


@click.group()
Expand All @@ -40,7 +40,10 @@ async def cli(ctx: Context, username: str, password: str, verbose: bool) -> None
ctx.obj["username"] = username
ctx.obj["password"] = password

session = ClientSession()
trace_configs = []
if verbose:
trace_configs.append(TRACE_CONFIG)
session = ClientSession(trace_configs=trace_configs)
myskoda = MySkoda(session)
await myskoda.connect(username, password)

Expand Down
20 changes: 19 additions & 1 deletion myskoda/myskoda.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from asyncio import gather
from collections.abc import Awaitable, Callable
from ssl import SSLContext
from types import SimpleNamespace

from aiohttp import ClientSession
from aiohttp import ClientSession, TraceConfig, TraceRequestEndParams

from .event import Event
from .models.air_conditioning import AirConditioning
Expand All @@ -29,6 +30,23 @@
_LOGGER = logging.getLogger(__name__)


async def trace_response(
_session: ClientSession,
_trace_config_ctx: SimpleNamespace,
params: TraceRequestEndParams,
) -> None:
"""Log response details. Used in aiohttp.TraceConfig."""
_LOGGER.debug(
f"Trace: {params.method} {str(params.url)[:60]} - "
f"response: {params.response.status} ({params.response.content_length} bytes) "
f"{(await params.response.text())[:1000]}"
)


TRACE_CONFIG = TraceConfig()
TRACE_CONFIG.on_request_end.append(trace_response)


class MySkoda:
session: ClientSession
rest_api: RestApi
Expand Down

0 comments on commit 987b23f

Please sign in to comment.