Added async entrypoints in the api module.
This commit is contained in:
parent
5052ad4b5e
commit
f302d96001
65
api/async_bot.py
Normal file
65
api/async_bot.py
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
Asyncio wrapper for AsyncOperator methods.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
from bot_z.async_operator import AsyncOperator
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def this_loop(loop):
|
||||||
|
_loop = asyncio.get_event_loop()
|
||||||
|
asyncio.set_event_loop(loop)
|
||||||
|
yield
|
||||||
|
asyncio.set_event_loop(_loop)
|
||||||
|
|
||||||
|
|
||||||
|
def init(base_url: str, name: str) -> AsyncOperator:
|
||||||
|
"""
|
||||||
|
Initialize the stateful object.
|
||||||
|
"""
|
||||||
|
return AsyncOperator(base_url, name)
|
||||||
|
|
||||||
|
|
||||||
|
async def login(op: AsyncOperator, username: str, password: str) -> bool:
|
||||||
|
"""
|
||||||
|
Executes the login asynchronously and returns
|
||||||
|
the AsyncOperator object.
|
||||||
|
"""
|
||||||
|
if not op.logged_in:
|
||||||
|
await op.login(username, password)
|
||||||
|
return op.logged_in
|
||||||
|
|
||||||
|
|
||||||
|
async def logout(op: AsyncOperator) -> bool:
|
||||||
|
"""
|
||||||
|
Executes the logout asynchronously and returns
|
||||||
|
a bool showing the success or failure of the operation.
|
||||||
|
"""
|
||||||
|
if op.logged_in:
|
||||||
|
await op.logout()
|
||||||
|
return op.logged_in
|
||||||
|
|
||||||
|
|
||||||
|
async def checkin(op: AsyncOperator) -> bool:
|
||||||
|
"""
|
||||||
|
Executes the check in asynchronously and returns
|
||||||
|
a bool showing the success or failure of the operation.
|
||||||
|
"""
|
||||||
|
if op.logged_in and not op.checked_in:
|
||||||
|
await op.checkin()
|
||||||
|
return op.checked_in
|
||||||
|
|
||||||
|
|
||||||
|
async def checkout(op: AsyncOperator) -> bool:
|
||||||
|
"""
|
||||||
|
Executes the checkout asynchronously and returns
|
||||||
|
a bool showing the success or failure of the operation.
|
||||||
|
"""
|
||||||
|
if op.logged_in and op.checked_in:
|
||||||
|
await op.checkout()
|
||||||
|
return not op.checked_in
|
Loading…
Reference in New Issue
Block a user