Skip to content

Commit

Permalink
Change login, start and run to be variadic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapptz committed Apr 9, 2016
1 parent 222a89a commit 93edf88
Showing 1 changed file with 59 additions and 43 deletions.
102 changes: 59 additions & 43 deletions discord/client.py
Expand Up @@ -442,47 +442,25 @@ def _reconnect_ws(self):
# login state management

@asyncio.coroutine
def login(self, email, password):
"""|coro|
Logs in the client with the specified credentials.
Parameters
----------
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))
def _login_1(self, token):
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))

if resp.status != 200:
if resp.status == 400:
raise LoginFailure('Improper token has been passed.')
else:
raise HTTPException(resp, None)
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
log.info('token auth returned status code {}'.format(resp.status))
self._is_logged_in.set()

@asyncio.coroutine
def _login_2(self, email, password):
# attempt to read the token from cache
if self.cache_auth:
yield from self._login_via_cache(email, password)
Expand Down Expand Up @@ -517,6 +495,44 @@ def login(self, email, password):
if self.cache_auth:
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
def logout(self):
"""|coro|
Expand Down Expand Up @@ -584,15 +600,15 @@ def close(self):
self._is_ready.clear()

@asyncio.coroutine
def start(self, email, password):
def start(self, *args):
"""|coro|
A shorthand coroutine for :meth:`login` + :meth:`connect`.
"""
yield from self.login(email, password)
yield from self.login(*args)
yield from self.connect()

def run(self, email, password):
def run(self, *args):
"""A blocking call that abstracts away the `event loop`_
initialisation from you.
Expand All @@ -603,7 +619,7 @@ def run(self, email, password):
Roughly Equivalent to: ::
try:
loop.run_until_complete(start(email, password))
loop.run_until_complete(start(*args))
except KeyboardInterrupt:
loop.run_until_complete(logout())
# cancel all tasks lingering
Expand All @@ -618,7 +634,7 @@ def run(self, email, password):
"""

try:
self.loop.run_until_complete(self.start(email, password))
self.loop.run_until_complete(self.start(*args))
except KeyboardInterrupt:
self.loop.run_until_complete(self.logout())
pending = asyncio.Task.all_tasks()
Expand Down

0 comments on commit 93edf88

Please sign in to comment.