-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_connect.py
More file actions
115 lines (97 loc) · 3.78 KB
/
_connect.py
File metadata and controls
115 lines (97 loc) · 3.78 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import base64
import json
import logging
import os
from datetime import datetime, timedelta
from monthdelta import monthdelta
import dateutil.parser
import requests
import yaml
from flask import Flask
from websocket import create_connection
import time
import app
from threading import Lock
# Initialize the app
app = Flask(__name__, instance_relative_config=True)
lock = Lock()
def client_login():
client = read_cred()['env']['PREDIX_APP_CLIENT_ID']
secret = read_cred()['env']['PREDIX_APP_CLIENT_SECRET']
uaaURL = read_cred()['env']['PREDIX_SECURITY_UAA_URI'] + "/oauth/token"
credentials = base64.b64encode(str.join(':', [client, secret]))
headers = {
'authorization': "Basic " + credentials,
'cache-control': "no-cache",
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request('POST', uaaURL, data="grant_type=client_credentials", headers=headers)
if response.status_code == 200:
logging.debug("RESPONSE=" + str(response.json()))
return json.loads(response.text)['access_token']
else:
logging.warn("Failed to authenticate")
response.raise_for_status()
def get_credentials(path):
with open(path, 'r') as stream:
try:
return yaml.load(stream)
except yaml.YAMLError as exception:
return exception
def get_token(path):
try:
with lock:
if not os.path.isfile(path):
cache = {
'expires': (datetime.now() + timedelta(hours=10)).isoformat(),
'token': client_login()
}
with open(path, 'w') as outfile:
json.dump(cache, outfile)
return cache['token']
else:
read_token = open(path)
cache = json.load(read_token)
read_token.close()
expires = dateutil.parser.parse(cache['expires'])
if expires < datetime.now():
cache = {
'expires': (datetime.now() + timedelta(hours=10)).isoformat(),
'token': client_login()
}
with open(path, 'w') as outfile:
json.dump(cache, outfile)
time.sleep(2)
print(cache['token'])
return cache['token']
else:
return cache['token']
except:
print("Problem opening/reading token file ")
def read_cred():
return get_credentials(os.path.join(app.root_path, 'manifest_client.yml'))
def create_header():
HEADER = {
'Predix-Zone-Id': read_cred()['env']['PREDIX_DATA_TIMESERIES_QUERY_ZONE_ID'],
'Authorization': 'Bearer ' + get_token(os.path.join(app.root_path, 'token.json')),
'Content-Type': 'application/json'
}
return HEADER
def create_asset_header():
HEADER = {
'predix-zone-id': read_cred()['env']['PREDIX_DATA_ASSET_ZONE_ID'],
'authorization': 'Bearer ' + get_token(os.path.join(app.root_path, 'token.json')),
'Content-Type': 'application/json'
}
return HEADER
def create_analytics_connection(cat_id):
return read_cred()['env']['PREDIX_ANALYTICS_UAA'] + cat_id + "/execution"
def create_analytics_header():
HEADER = {
'predix-zone-id': read_cred()['env']['PREDIX_ANALYTICS_ZONE_ID'],
'authorization': "Bearer " + get_token(os.path.join(app.root_path, 'token.json')),
'content-type': "application/json"
}
return HEADER
def create_ingest_connection():
return create_connection(read_cred()['env']['PREDIX_DATA_TIMESERIES_INGEST_URI'], header=create_header())