cree-py / pynite

Async python wrapper for the Fortnite API.
MIT License
10 stars 3 forks source link

Fixes #5 #6

Closed boo-dev closed 6 years ago

boo-dev commented 6 years ago

Fixes the problem with issue #5 in which it errors saying the client session is unclosed

ravener commented 6 years ago

Why would you close the session after a player request, and if we wanted more than one requests?

boo-dev commented 6 years ago

Completely overlooked that part, kind of new to async functions and was just trying to remedy the error. Will look more into it before submitting another request

boo-dev commented 6 years ago

Should work now, just call await client.close() when you are ready to close the session. Example:

async def main():
    client = pynite.Client(token, timeout=5)

    platform = 'pc'
    usernames = ['drlupo', 'tsm_myth']
    for usr in usernames:
        player = await client.get_player(platform, usr)
        solos = await player.get_solos()

        print(player.account_id)  # prints account id
        print(solos.kills.value)  # prints the number of kills you have in solos

    await client.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
ravener commented 6 years ago

Is client.close() a coroutine that your awaiting it? It didn't mention it in docs

boo-dev commented 6 years ago

Reading through the asyncio docs it seems like we should be able to just close the loop once it's done running with loop.close() https://docs.python.org/3/library/asyncio-task.html#example-chain-coroutines

I just added my own function in the Client class that closes the session when called but it may not be the ideal way to do things. Wondering why loop.close() doesn't work in this case?

SharpBit commented 6 years ago

@Elemxnt just add what you sent in that comment to example.py in the examples folder and i'll merge the pr :)

boo-dev commented 6 years ago

@SharpBit found what i believe to be a better way to do it from here https://github.com/aio-libs/aiohttp/issues/2800 should be the most ideal way to do this as you do not need to call any new functions in your own code, as once your loop is done running it knows to close the session

SharpBit commented 6 years ago

Ok, nice.

SharpBit commented 6 years ago

Woops, closed instead of merge by accident