Browse Source

Change login, start and run to be variadic.

pull/161/merge
Rapptz 9 years ago
parent
commit
93edf88ee4
  1. 102
      discord/client.py

102
discord/client.py

@ -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
Logs in the client with the specified credentials. self.headers['authorization'] = 'Bot {}'.format(self.token)
resp = yield from self.session.get(endpoints.ME, headers=self.headers)
Parameters yield from resp.release()
---------- log.debug(request_logging_format.format(method='GET', response=resp))
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 != 200:
if resp.status == 400: if resp.status == 400:
raise LoginFailure('Improper token has been passed.') raise LoginFailure('Improper token has been passed.')
else: else:
raise HTTPException(resp, None) raise HTTPException(resp, None)
log.info('token auth returned status code {}'.format(resp.status)) log.info('token auth returned status code {}'.format(resp.status))
self._is_logged_in.set() 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…
Cancel
Save