-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.py
More file actions
42 lines (33 loc) · 1.25 KB
/
auth.py
File metadata and controls
42 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import asyncio
class AuthResource:
def __init__(self, client):
self._client = client
def authenticate(self):
"""
A call to GET /auth/profile to initially authenticate the user.
"""
response = self._client._get("/auth/profile")
# Set the user_id if successful
if response and isinstance(response, dict) and 'user_id' in response:
self._client._user = response['user_id']
return response
class AsyncAuthResource:
def __init__(self, client):
self._client = client
def authenticate(self):
"""
A synchronous wrapper for an async auth call to be used during initialization.
"""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
# Create the task
task = loop.create_task(self._client._get("/auth/profile"))
# Run the task to completion
result = loop.run_until_complete(task)
# Set the user_id if successful
if result and isinstance(result, dict) and 'user_id' in result:
self._client._user = result['user_id']
return result
finally:
loop.close()