mattrayner/podpointclient

Can't run example even in async call- timeout context manager error

pmcgoohan opened this issue · 1 comments

Hi

Thanks so much for developing this. It looks just perfect for my needs and is very helpful of you.

I may very well be doing something wrong, but I'm struggling to get started with the example.

I've put it in an async call as suggested, but it errors on async_credientials_verified with "Timeout context manager should be used inside a task".

import asyncio
from aioresponses import aioresponses
import aiohttp
from podpointclient.client import PodPointClient
from podpointclient.pod import Pod, Firmware
from podpointclient.charge import Charge
from podpointclient.charge_override import ChargeOverride
from podpointclient.user import User
from podpointclient.errors import ChargeOverrideValidationError
from podpointclient.endpoints import API_BASE_URL, AUTH, CHARGE_SCHEDULES, CHARGES, FIRMWARE, PODS, SESSIONS, UNITS, USERS, CHARGE_OVERRIDE

async def main():
    from podpointclient.client import PodPointClient

    # Create a client
    client = PodPointClient(username="USERNAME", password="PASSWORD")

    # Verify credentials work
    verified = await client.async_credentials_verified()
    print(verified)
    
    charges = await client.async_get_all_charges()
    print(charges)

asyncio.run(main())

I also tried creating a task within the loop, but ran into the same problem...

import asyncio
from aioresponses import aioresponses
import aiohttp
from podpointclient.client import PodPointClient
from podpointclient.pod import Pod, Firmware
from podpointclient.charge import Charge
from podpointclient.charge_override import ChargeOverride
from podpointclient.user import User
from podpointclient.errors import ChargeOverrideValidationError
from podpointclient.endpoints import API_BASE_URL, AUTH, CHARGE_SCHEDULES, CHARGES, FIRMWARE, PODS, SESSIONS, UNITS, USERS, CHARGE_OVERRIDE

async def main():
    loop = asyncio.get_running_loop()
    asyncio.run_coroutine_threadsafe(task(), loop)
    
async def task():
    from podpointclient.client import PodPointClient

    # Create a client
    client = PodPointClient(username="USERNAME", password="PASSWORD")

    # Verify credentials work
    verified = await client.async_credentials_verified()
    print(verified)
    
    charges = await client.async_get_all_charges()
    print(charges)    

asyncio.run(main())

Do you have any ideas how best to proceed, and would it perhaps be possible to update the readme to reflect this?

Many thanks

For anyone having the same problem, the issue was related to the fact that the asyncio.run() function sets up an event loop internally and runs the provided coroutine within that loop.

To resolve this issue, I set up the event loop manually using asyncio.get_event_loop() and loop.run_until_complete() instead of using asyncio.run().

Like this...

import asyncio
from aioresponses import aioresponses
import aiohttp
from podpointclient.client import PodPointClient
from podpointclient.pod import Pod, Firmware
from podpointclient.charge import Charge
from podpointclient.charge_override import ChargeOverride
from podpointclient.user import User
from podpointclient.errors import ChargeOverrideValidationError
from podpointclient.endpoints import API_BASE_URL, AUTH, CHARGE_SCHEDULES, CHARGES, FIRMWARE, PODS, SESSIONS, UNITS, USERS, CHARGE_OVERRIDE

async def main():
    from podpointclient.client import PodPointClient

    # Create a client
    client = PodPointClient(username="USERNAME", password="PASSWORD")

    # Verify credentials work
    verified = await client.async_credentials_verified()
    print(verified)
    
    charges = await client.async_get_all_charges()
    print(charges)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())