algosec-appviz 0.0.9__tar.gz → 0.0.10__tar.gz

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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: algosec_appviz
3
- Version: 0.0.9
3
+ Version: 0.0.10
4
4
  Summary: AlgoSec AppViz Library
5
5
  Home-page: https://github.com/bogdan-iot/algosec-appviz
6
6
  License: LICENSE
@@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api"
6
6
  name = "algosec_appviz"
7
7
  repository = "https://github.com/bogdan-iot/algosec-appviz"
8
8
  documentation = "https://github.com/bogdan-iot/algosec-appviz/blob/master/README.md"
9
- version = "0.0.9"
9
+ version = "0.0.10"
10
10
  license = "LICENSE"
11
11
  readme = "README.md"
12
12
  description = "AlgoSec AppViz Library"
@@ -1,5 +1,6 @@
1
1
  import requests
2
2
  from algosec_appviz import environment
3
+ from datetime import datetime, timedelta
3
4
  from mydict import MyDict
4
5
 
5
6
  regions = {
@@ -19,12 +20,19 @@ class AppViz:
19
20
  raise ValueError(f"Invalid region, must be one of: {', '.join(regions.keys())}")
20
21
 
21
22
  self.proxies = proxies
23
+ self.region = region
24
+ self.tenant_id = tenant_id or environment.get_tenant_id()
25
+ self._client_id = client_id or environment.get_client_id()
26
+ self._client_secret = client_secret or environment.get_client_secret()
22
27
 
23
- login_url = f"https://{regions[region]}/api/algosaas/auth/v1/access-keys/login"
28
+ self._init_token()
29
+
30
+ def _init_token(self):
31
+ login_url = f"https://{regions[self.region]}/api/algosaas/auth/v1/access-keys/login"
24
32
  data = {
25
- "tenantId": tenant_id or environment.get_tenant_id(),
26
- "clientId": client_id or environment.get_client_id(),
27
- "clientSecret": client_secret or environment.get_client_secret()
33
+ "tenantId": self.tenant_id,
34
+ "clientId": self._client_id,
35
+ "clientSecret": self._client_secret
28
36
  }
29
37
 
30
38
  headers = {
@@ -36,9 +44,10 @@ class AppViz:
36
44
  if response.status_code != 200:
37
45
  raise ConnectionError(f"Authentication to AppViz failed: {response.text}")
38
46
 
39
- self.url = 'https://' + regions[region]
47
+ self.url = 'https://' + regions[self.region]
40
48
  self._token_type = response.json()['token_type']
41
49
  self._token = response.json()['access_token']
50
+ self._token_expires = datetime.now() + timedelta(seconds=response.json()['expires_in'])
42
51
 
43
52
  def create_application(self, name=None, **kwargs):
44
53
  if not name:
@@ -164,6 +173,10 @@ class AppViz:
164
173
  return [MyDict(x) for x in response]
165
174
 
166
175
  def _make_api_call(self, method, url_path, body=None, params=None):
176
+ # Check if the token is still valid, otherwise request a new one
177
+ if datetime.now() >= self._token_expires - timedelta(seconds=5):
178
+ self._init_token()
179
+
167
180
  valid_methods = ['get', 'post', 'delete']
168
181
  headers = {
169
182
  'Accept': 'application/json',
File without changes