Repository navigation
Expand file tree
/
Copy pathpflinkClient.py
More file actions
65 lines (57 loc) · 2.32 KB
/
Copy pathpflinkClient.py
File metadata and controls
65 lines (57 loc) · 2.32 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
This class contains useful methods to authenticate and re/generate auth tokens for `pflink`
"""
import httpx
class PflinkClient(object):
"""
A `pflink` client
"""
def __init__(self, url, auth_token):
self.auth_token = None
self.url = url
self.set_auth_token(auth_token)
def set_auth_token(self, auth_token):
if not auth_token:
raise PflinkRequestInvalidTokenException(f'Invalid auth token: {auth_token}')
self.auth_token = str(auth_token)
async def post(self, data: dict):
"""
"""
headers = {'Authorization': 'Bearer ' + self.auth_token}
async with httpx.AsyncClient() as client:
try:
response: httpx.Response = await client.post(
self.url,
data=data,
headers=headers
)
except Exception as e:
raise PflinkRequestException(f"Error occurred while connecting to {self.url}: {str(e)}")
if response.status_code == 403:
raise PflinkRequestInvalidTokenException(f'Invalid auth token: {self.auth_token}')
return response
@staticmethod
async def get_auth_token(pflink_auth_url: str, pflink_user: str, pflink_password: str):
"""
Make a POST request to obtain an auth token.
Args:
pflink_auth_url: API endpoint of `pflink` to get new auth token
pflink_user: Authorized pflink username
pflink_password: Authorized pflink password
Returns: A new authentication token
"""
async with httpx.AsyncClient() as client:
try:
response: httpx.Response = await client.post(
pflink_auth_url,
data={'username': pflink_user, 'password': pflink_password}
)
except Exception as e:
raise PflinkRequestException(f"Error occurred while getting token from {pflink_auth_url}: {str(e)}")
if not response.json().get('access_token'):
raise Exception(f"Could not create token from URL: {pflink_auth_url}")
return response.json().get('access_token')
class PflinkRequestException(Exception):
pass
class PflinkRequestInvalidTokenException(PflinkRequestException):
pass