# flake8: noqa
import asyncio
from netlab.async_client import AsyncClient


# Create a function that we can use `await` in
async def main():
    # Create and connect the AsyncClient
    client = AsyncClient()
    await client.connect()

    # List communities
    result = await client.user_community_list()
    print('>>>>', result)
    # List used pod ids
    result = await client.pod_list_used_ids()
    print('>>>>', result)

    # Iterate over user accounts
    async for user in client.user_account_search_iter(properties=['acc_id']):
        print(user)

    # Make 10 requests simultaneously
    result = await asyncio.gather(*[client.user_account_search(properties=['acc_id'], page=1) for i in range(10)])
    print(result)

    # Listen to RESERVATION.ADDED events
    queue = await client.subscribe('RESERVATION.ADDED')
    try:
        while True:
            # Wait 1 second for a RESERVATION.ADDED event
            result = await asyncio.wait_for(queue.get(), 1)
            print(result)
    except asyncio.TimeoutError:
        # A TimeoutError was raised by `asyncio.wait_for`
        print('No event in one second.')
    finally:
        # Unsubscribe to RESERVATION.ADDED, use the finally block to ensure it always happens
        await client.unsubscribe(queue)
        await client.disconnect()



if __name__ == '__main__':
    # Boilerplate for asyncio
    loop = asyncio.get_event_loop()
    # Run our asyncio application
    loop.run_until_complete(main())
    loop.close()
