skodaconnect / myskoda

Python library for interacting with MySkoda APIs.
MIT License
9 stars 22 forks source link

Wrap validations in try/except so we can emit some logging #27

Closed WebSpider closed 2 months ago

WebSpider commented 2 months ago

Fixes #22

WebSpider commented 2 months ago

Conflicts with #31, let's see how we can catch validationerrors in mashu

dvx76 commented 2 months ago

Conflicts with #31, let's see how we can catch validationerrors in mashu

We may not need to since mashu spits out failures like in skodaconnect/homeassistant-myskoda#34

Prior99 commented 2 months ago

In the meantime I had added this:

    async def _make_get_request[T](self, url: str, deserialize: Callable[[str], T]) -> T:
        async with self.session.get(
            f"{BASE_URL_SKODA}/api{url}",
            headers=await self._headers(),
        ) as response:
            response_text = await response.text()
            try:
                data = deserialize(response_text)
            except Exception:
                _LOGGER.exception(
                    "Failed to load data from url %s. Return value was '%s'", url, response_text
                )
                raise
            else:
                return data

Which already wraps all get requests:

    async def get_charging(self, vin: str) -> Charging:
        """Retrieve information related to charging for the specified vehicle."""
        return await self._make_get_request(f"/v1/charging/{vin}", Charging.from_json)

Is this PR still needed?

If yes, can we find a more generic way?

dvx76 commented 2 months ago

I think we can close this. If we still need more/better logs it will bubble up again while looking at issues.