Browse Source

Make is_logged_in and is_closed into Events internally.

pull/60/head
Rapptz 9 years ago
parent
commit
a1693a8c9d
  1. 24
      discord/client.py

24
discord/client.py

@ -123,8 +123,8 @@ class Client:
'user-agent': user_agent.format(library_version, sys.version_info, aiohttp.__version__) 'user-agent': user_agent.format(library_version, sys.version_info, aiohttp.__version__)
} }
self._closed = False self._closed = asyncio.Event(loop=self.loop)
self._is_logged_in = False self._is_logged_in = asyncio.Event(loop=self.loop)
# These two events correspond to the two events necessary # These two events correspond to the two events necessary
# for a connection to be made # for a connection to be made
@ -152,7 +152,7 @@ class Client:
log.info('login cache token check succeeded') log.info('login cache token check succeeded')
data = yield from check.json() data = yield from check.json()
self.gateway = data.get('url') self.gateway = data.get('url')
self._is_logged_in = True self._is_logged_in.set()
return return
else: else:
# failed auth check # failed auth check
@ -283,7 +283,7 @@ class Client:
@asyncio.coroutine @asyncio.coroutine
def keep_alive_handler(self, interval): def keep_alive_handler(self, interval):
try: try:
while not self._closed: while not self.is_closed:
payload = { payload = {
'op': 1, 'op': 1,
'd': int(time.time()) 'd': int(time.time())
@ -424,12 +424,12 @@ class Client:
@property @property
def is_logged_in(self): def is_logged_in(self):
"""bool: Indicates if the client has logged in successfully.""" """bool: Indicates if the client has logged in successfully."""
return self._is_logged_in return self._is_logged_in.is_set()
@property @property
def is_closed(self): def is_closed(self):
"""bool: Indicates if the websocket connection is closed.""" """bool: Indicates if the websocket connection is closed."""
return self._closed return self._closed.is_set()
# helpers/getters # helpers/getters
@ -614,7 +614,7 @@ class Client:
# 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)
if self._is_logged_in: if self.is_logged_in:
return return
payload = { payload = {
@ -638,7 +638,7 @@ class Client:
body = yield from resp.json() body = yield from resp.json()
self.token = body['token'] self.token = body['token']
self.headers['authorization'] = self.token self.headers['authorization'] = self.token
self._is_logged_in = True self._is_logged_in.set()
self.gateway = yield from self._get_gateway() self.gateway = yield from self._get_gateway()
# since we went through all this trouble # since we went through all this trouble
@ -654,7 +654,7 @@ class Client:
response = yield from aiohttp.post(endpoints.LOGOUT, headers=self.headers, loop=self.loop) response = yield from aiohttp.post(endpoints.LOGOUT, headers=self.headers, loop=self.loop)
yield from response.release() yield from response.release()
yield from self.close() yield from self.close()
self._is_logged_in = False self._is_logged_in.clear()
log.debug(request_logging_format.format(method='POST', response=response)) log.debug(request_logging_format.format(method='POST', response=response))
@asyncio.coroutine @asyncio.coroutine
@ -676,7 +676,7 @@ class Client:
""" """
yield from self._make_websocket() yield from self._make_websocket()
while not self._closed: while not self.is_closed:
msg = yield from self.ws.recv() msg = yield from self.ws.recv()
if msg is None: if msg is None:
if self.ws.close_code == 1012: if self.ws.close_code == 1012:
@ -694,7 +694,7 @@ class Client:
To reconnect the websocket connection, :meth:`connect` must be used. To reconnect the websocket connection, :meth:`connect` must be used.
""" """
if self._closed: if self.is_closed:
return return
if self.is_voice_connected(): if self.is_voice_connected():
@ -705,7 +705,7 @@ class Client:
yield from self.ws.close() yield from self.ws.close()
self.keep_alive.cancel() self.keep_alive.cancel()
self._closed = True self._closed.set()
@asyncio.coroutine @asyncio.coroutine
def start(self, email, password): def start(self, email, password):

Loading…
Cancel
Save