seer-pas-sdk 0.1.2__py3-none-any.whl → 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,77 +1 @@
1
- import requests
2
-
3
-
4
- class Auth:
5
- _instances = {
6
- "US": "https://api.pas.seer.software/",
7
- "EU": "https://api.pas-eu.seer.bio/",
8
- }
9
-
10
- def __init__(self, username, password, instance="US"):
11
- """
12
- Constructor for the Auth class. Uses the username, password and instance name to instantiate the class.
13
-
14
- Parameters
15
- ----------
16
- username: str
17
- The username of the account associated with the PAS instance.
18
- password: str
19
- The password of the account associated with the PAS instance.
20
- instance: str
21
- The instance name of the PAS instance (`US | EU`). Defaults to `US`.
22
- """
23
-
24
- self.username = username
25
- self.__password = password
26
-
27
- if instance not in Auth._instances:
28
- if instance.startswith("https://"):
29
- # Support arbitrary endpoint for testing
30
- self.url = instance
31
- else:
32
- raise ValueError("Invalid PAS instance.")
33
- else:
34
- self.url = Auth._instances[instance]
35
-
36
- self.instance = instance
37
-
38
- def login(self):
39
- """
40
- Logs into the PAS instance using the mapped URL and the login credentials (username and password) provided in the constructor.
41
-
42
- Returns
43
- -------
44
- dict
45
- A dictionary containing the login response from the PAS instance.
46
- """
47
- response = requests.post(
48
- f"{self.url}auth/login",
49
- json={"username": self.username, "password": self.__password},
50
- )
51
-
52
- if not response:
53
- raise ValueError(
54
- "Check if the credentials are correct or if the backend is running or not."
55
- )
56
-
57
- if response.status_code == 200:
58
- return response.json()
59
-
60
- def get_token(self):
61
- """
62
- Gets the token from the login response.
63
-
64
- Returns
65
- -------
66
- str
67
- The token from the login response.
68
- """
69
-
70
- res = self.login()
71
-
72
- if "id_token" not in res or "access_token" not in res:
73
- raise ValueError(
74
- "Check if the credentials are correct or if the backend is running or not."
75
- )
76
-
77
- return res["id_token"], res["access_token"]
1
+ from .auth import Auth
@@ -0,0 +1,99 @@
1
+ import requests
2
+ import jwt
3
+
4
+
5
+ class Auth:
6
+ _instances = {
7
+ "US": "https://api.pas.seer.software/",
8
+ "EU": "https://api.pas-eu.seer.bio/",
9
+ }
10
+
11
+ def __init__(self, username, password, instance="US"):
12
+ """
13
+ Constructor for the Auth class. Uses the username, password and instance name to instantiate the class.
14
+
15
+ Parameters
16
+ ----------
17
+ username: str
18
+ The username of the account associated with the PAS instance.
19
+ password: str
20
+ The password of the account associated with the PAS instance.
21
+ instance: str
22
+ The instance name of the PAS instance (`US | EU`). Defaults to `US`.
23
+ """
24
+
25
+ self.username = username
26
+ self.__password = password
27
+
28
+ if instance not in Auth._instances:
29
+ if instance.startswith("https://") or instance.startswith(
30
+ "http://"
31
+ ):
32
+ # Support arbitrary endpoint for testing
33
+ self.url = instance
34
+ else:
35
+ raise ValueError("Invalid PAS instance.")
36
+ else:
37
+ self.url = Auth._instances[instance]
38
+
39
+ self.instance = instance
40
+
41
+ # Null initialize multi tenant attributes
42
+ (
43
+ self.base_tenant_id,
44
+ self.active_tenant_id,
45
+ self.base_role,
46
+ self.active_role,
47
+ ) = [None] * 4
48
+
49
+ def login(self):
50
+ """
51
+ Logs into the PAS instance using the mapped URL and the login credentials (username and password) provided in the constructor.
52
+
53
+ Returns
54
+ -------
55
+ dict
56
+ A dictionary containing the login response from the PAS instance.
57
+ """
58
+ response = requests.post(
59
+ f"{self.url}auth/login",
60
+ json={"username": self.username, "password": self.__password},
61
+ )
62
+
63
+ if not response:
64
+ raise ValueError(
65
+ "Check if the credentials are correct or if the backend is running or not."
66
+ )
67
+
68
+ if response.status_code == 200:
69
+ return response.json()
70
+
71
+ def get_token(self):
72
+ """
73
+ Gets the token from the login response.
74
+
75
+ Returns
76
+ -------
77
+ str
78
+ The token from the login response.
79
+ """
80
+
81
+ res = self.login()
82
+
83
+ if "id_token" not in res or "access_token" not in res:
84
+ raise ValueError(
85
+ "Check if the credentials are correct or if the backend is running or not."
86
+ )
87
+ decoded_token = jwt.decode(
88
+ res["id_token"], options={"verify_signature": False}
89
+ )
90
+ self.base_tenant_id = decoded_token["custom:tenantId"]
91
+ self.base_role = decoded_token["custom:role"]
92
+
93
+ if not self.active_tenant_id:
94
+ self.active_tenant_id = self.base_tenant_id
95
+
96
+ if not self.active_role:
97
+ self.active_role = self.base_role
98
+
99
+ return res["id_token"], res["access_token"]