mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 12:18:59 +00:00
Change login, start and run to be variadic.
This commit is contained in:
parent
222a89a653
commit
93edf88ee4
@ -442,47 +442,25 @@ class Client:
|
|||||||
# login state management
|
# login state management
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def login(self, email, password):
|
def _login_1(self, token):
|
||||||
"""|coro|
|
log.info('logging in using static token')
|
||||||
|
self.token = token
|
||||||
|
self.headers['authorization'] = 'Bot {}'.format(self.token)
|
||||||
|
resp = yield from self.session.get(endpoints.ME, headers=self.headers)
|
||||||
|
yield from resp.release()
|
||||||
|
log.debug(request_logging_format.format(method='GET', response=resp))
|
||||||
|
|
||||||
Logs in the client with the specified credentials.
|
if resp.status != 200:
|
||||||
|
if resp.status == 400:
|
||||||
|
raise LoginFailure('Improper token has been passed.')
|
||||||
|
else:
|
||||||
|
raise HTTPException(resp, None)
|
||||||
|
|
||||||
Parameters
|
log.info('token auth returned status code {}'.format(resp.status))
|
||||||
----------
|
self._is_logged_in.set()
|
||||||
email : str
|
|
||||||
The email used to login. The string 'token' if using
|
|
||||||
the Bot OAuth2 Token flow.
|
|
||||||
password : str
|
|
||||||
The password or token used to login.
|
|
||||||
|
|
||||||
Raises
|
|
||||||
------
|
|
||||||
LoginFailure
|
|
||||||
The wrong credentials are passed.
|
|
||||||
HTTPException
|
|
||||||
An unknown HTTP related error occurred,
|
|
||||||
usually when it isn't 200 or the known incorrect credentials
|
|
||||||
passing status code.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if email == "token":
|
|
||||||
log.info('logging in using static token')
|
|
||||||
self.token = password
|
|
||||||
self.headers['authorization'] = 'Bot {}'.format(self.token)
|
|
||||||
resp = yield from self.session.get(endpoints.ME, headers=self.headers)
|
|
||||||
yield from resp.release()
|
|
||||||
log.debug(request_logging_format.format(method='GET', response=resp))
|
|
||||||
|
|
||||||
if resp.status != 200:
|
|
||||||
if resp.status == 400:
|
|
||||||
raise LoginFailure('Improper token has been passed.')
|
|
||||||
else:
|
|
||||||
raise HTTPException(resp, None)
|
|
||||||
|
|
||||||
log.info('token auth returned status code {}'.format(resp.status))
|
|
||||||
self._is_logged_in.set()
|
|
||||||
return
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def _login_2(self, email, password):
|
||||||
# attempt to read the token from cache
|
# attempt to read the token from cache
|
||||||
if self.cache_auth:
|
if self.cache_auth:
|
||||||
yield from self._login_via_cache(email, password)
|
yield from self._login_via_cache(email, password)
|
||||||
@ -517,6 +495,44 @@ class Client:
|
|||||||
if self.cache_auth:
|
if self.cache_auth:
|
||||||
self._update_cache(email, password)
|
self._update_cache(email, password)
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def login(self, *args):
|
||||||
|
"""|coro|
|
||||||
|
|
||||||
|
Logs in the client with the specified credentials.
|
||||||
|
|
||||||
|
This function can be used in two different ways.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
await client.login('email', 'password')
|
||||||
|
|
||||||
|
# or
|
||||||
|
|
||||||
|
await client.login('token')
|
||||||
|
|
||||||
|
More than 2 parameters or less than 1 parameter raises a
|
||||||
|
:exc:`TypeError`.
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
LoginFailure
|
||||||
|
The wrong credentials are passed.
|
||||||
|
HTTPException
|
||||||
|
An unknown HTTP related error occurred,
|
||||||
|
usually when it isn't 200 or the known incorrect credentials
|
||||||
|
passing status code.
|
||||||
|
TypeError
|
||||||
|
The incorrect number of parameters is passed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
n = len(args)
|
||||||
|
if n in (2, 1):
|
||||||
|
yield from getattr(self, '_login_' + str(n))(*args)
|
||||||
|
else:
|
||||||
|
raise TypeError('login() takes 1 or 2 positional arguments but {} were given'.format(n))
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def logout(self):
|
def logout(self):
|
||||||
"""|coro|
|
"""|coro|
|
||||||
@ -584,15 +600,15 @@ class Client:
|
|||||||
self._is_ready.clear()
|
self._is_ready.clear()
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def start(self, email, password):
|
def start(self, *args):
|
||||||
"""|coro|
|
"""|coro|
|
||||||
|
|
||||||
A shorthand coroutine for :meth:`login` + :meth:`connect`.
|
A shorthand coroutine for :meth:`login` + :meth:`connect`.
|
||||||
"""
|
"""
|
||||||
yield from self.login(email, password)
|
yield from self.login(*args)
|
||||||
yield from self.connect()
|
yield from self.connect()
|
||||||
|
|
||||||
def run(self, email, password):
|
def run(self, *args):
|
||||||
"""A blocking call that abstracts away the `event loop`_
|
"""A blocking call that abstracts away the `event loop`_
|
||||||
initialisation from you.
|
initialisation from you.
|
||||||
|
|
||||||
@ -603,7 +619,7 @@ class Client:
|
|||||||
Roughly Equivalent to: ::
|
Roughly Equivalent to: ::
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loop.run_until_complete(start(email, password))
|
loop.run_until_complete(start(*args))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
loop.run_until_complete(logout())
|
loop.run_until_complete(logout())
|
||||||
# cancel all tasks lingering
|
# cancel all tasks lingering
|
||||||
@ -618,7 +634,7 @@ class Client:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.loop.run_until_complete(self.start(email, password))
|
self.loop.run_until_complete(self.start(*args))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self.loop.run_until_complete(self.logout())
|
self.loop.run_until_complete(self.logout())
|
||||||
pending = asyncio.Task.all_tasks()
|
pending = asyncio.Task.all_tasks()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user