volue-insight-timeseries 2.0.2__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.
@@ -0,0 +1 @@
1
+ 2.0.2
@@ -0,0 +1,11 @@
1
+ #
2
+ # Volue Insight API access library
3
+ #
4
+
5
+ import os
6
+ from .session import Session
7
+ from . import auth, curves, events, session, util
8
+
9
+ here = os.path.abspath(os.path.dirname(__file__))
10
+ with open(os.path.join(here, 'VERSION')) as fv:
11
+ VERSION = __version__ = fv.read().strip()
@@ -0,0 +1,71 @@
1
+ #
2
+ # Authentication support
3
+ #
4
+
5
+ import json
6
+ import time
7
+ import threading
8
+ import os
9
+
10
+ try:
11
+ from urllib.parse import urljoin
12
+ except ImportError:
13
+ from urlparse import urljoin
14
+
15
+
16
+ class AuthFailedException(Exception):
17
+ pass
18
+
19
+
20
+ class OAuth:
21
+ """
22
+ Authentication based on OAuth client ID.
23
+ This is the main authentication mechanism for customer access to the data center.
24
+ """
25
+
26
+ def __init__(self, session, client_id, client_secret, auth_urlbase):
27
+ self.client_id = client_id
28
+ self.client_secret = client_secret
29
+ self.auth_urlbase = auth_urlbase
30
+ self.token = None
31
+ self.token_type = None
32
+ self.valid_until = None
33
+ self.session = session
34
+ self._authenticate()
35
+
36
+ def validate_auth(self):
37
+ """Check valid_until and fetch new token if needed"""
38
+ # To avoid sending duplicated authentication requests in other threads
39
+ with threading.Lock():
40
+ if (not self.valid_until) or time.time() > self.valid_until:
41
+ self._authenticate()
42
+
43
+ def _authenticate(self):
44
+ # Wipe out any old values before (re-)login
45
+ self.token = None
46
+ self.token_type = None
47
+ self.valid_until = None
48
+ now = time.time()
49
+ url = urljoin(self.auth_urlbase, '/oauth2/token')
50
+ auth = (self.client_id, self.client_secret)
51
+ data = {'grant_type': 'client_credentials'}
52
+ response = self.session.send_data_request('POST', self.auth_urlbase, url, rawdata=data, authval=auth)
53
+ if response.status_code != 200:
54
+ raise AuthFailedException('Authentication failed: {}'.format(response.content))
55
+ # Parse token
56
+ rsp = json.loads(response.content.decode())
57
+ self.token = rsp['access_token']
58
+ self.token_type = rsp['token_type']
59
+ self.valid_until = now + int(rsp['expires_in'] * 0.95)
60
+
61
+ def get_headers(self, data):
62
+ """The web-token auth header is simple"""
63
+ headers = {}
64
+ if self.token is not None and self.token_type is not None:
65
+ headers['Authorization'] = '{} {}'.format(self.token_type, self.token)
66
+
67
+ wapi_request_source = os.getenv('WAPI_REQUEST_SOURCE')
68
+ if wapi_request_source:
69
+ headers['X-Request-Source'] = wapi_request_source
70
+
71
+ return headers