skodaconnect / myskoda

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

Remove async-with #71

Closed WebSpider closed 1 month ago

WebSpider commented 1 month ago

Fixes #46

TODO: Needs testing

Prior99 commented 1 month ago

I took the liberty to rebase this

dvx76 commented 1 month ago

This broke RestApi for me. Looking into it.

❯ poetry run myskoda --user me@gmail.com --password x --verbose info TMxxx
2024-10-11 20:37:09 WALLY myskoda.myskoda[28722] DEBUG IDK Authorization was successful.
2024-10-11 20:37:09 WALLY myskoda.myskoda[28722] DEBUG Myskoda ready.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/asyncclick/core.py", line 1205, in __call__
    return anyio.run(self._main, main, args, kwargs, **opts)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/anyio/_core/_eventloop.py", line 74, in run
    return async_backend.run(func, args, {}, backend_options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2248, in run
    return runner.run(wrapper())
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/.pyenv/versions/3.12.2/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/.pyenv/versions/3.12.2/lib/python3.12/asyncio/base_events.py", line 685, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2236, in wrapper
    return await func(*args)
           ^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/asyncclick/core.py", line 1208, in _main
    return await main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/asyncclick/core.py", line 1120, in main
    rv = await self.invoke(ctx)
         ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/asyncclick/core.py", line 1739, in invoke
    return await _process_result(await sub_ctx.command.invoke(sub_ctx))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/asyncclick/core.py", line 1485, in invoke
    return await ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/asyncclick/core.py", line 824, in invoke
    rv = await rv
         ^^^^^^^^
  File "/home/fdevaux/dev/github/myskoda/myskoda/cli.py", line 172, in info
    await handle_request(ctx, ctx.obj["myskoda"].get_info, vin)
  File "/home/fdevaux/dev/github/myskoda/myskoda/cli.py", line 51, in handle_request
    result = await func(*args)
             ^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/myskoda/myskoda/myskoda.py", line 185, in get_info
    return await self.rest_api.get_info(vin)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/myskoda/myskoda/rest_api.py", line 81, in get_info
    return await self._make_get_request(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/myskoda/myskoda/rest_api.py", line 58, in _make_get_request
    response_text = await response.text()
                    ^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/aiohttp/client_reqrep.py", line 1220, in text
    await self.read()
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/aiohttp/client_reqrep.py", line 1178, in read
    self._body = await self.content.read()
                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fdevaux/dev/github/homeassistant-core/venv/lib/python3.12/site-packages/aiohttp/streams.py", line 360, in read
    raise self._exception
aiohttp.client_exceptions.ClientConnectionError: Connection closed
2024-10-11 20:37:09 WALLY asyncio[28722] ERROR Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fcbc4444c20>
2024-10-11 20:37:09 WALLY asyncio[28722] ERROR Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7fcbc4960770>, 13218.492045487)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7fcbc44699d0>, 13218.814665695)]']
connector: <aiohttp.connector.TCPConnector object at 0x7fcbc418e2d0>
dvx76 commented 1 month ago

It's only the final commit btw, checking out the version before that it works fine.

WebSpider commented 1 month ago

Why was this merged? It was a draft PR?

Edit: Ah, there were commits after mine 😆

dvx76 commented 1 month ago

Got it. await response.text() is called after the self.session.request context manager has closed. One line fix.