-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
162 lines (133 loc) · 5.13 KB
/
app.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from pprint import pprint # for pretty formatting
import requests # for making REST API requests
import json # for converting json payloads to strings
import uuid # to create UUIDs for Astra connections
import os # for accessing creds
from flask import render_template, request, redirect, url_for, abort
from flask import Flask
app = Flask(__name__)
# set up stuff
class Client:
"""
An API Client for connecting to Stargate
"""
def __init__(self, base_url, access_token, headers):
self.base_url = base_url
self.access_token = access_token
self.headers = headers
def post(self, payload={}, path=""):
"""
Via the requests library, performs a post with the payload to the path
"""
return requests.post(self.base_url + path,
data=json.dumps(payload),
headers=self.headers)
def put(self, payload={}, path=""):
"""
Via the requests library, performs a put with the payload to the path
"""
return requests.put(self.base_url + path,
data=json.dumps(payload),
headers=self.headers)
def patch(self, payload={}, path=""):
"""
Via the requests library, performs a patch with the payload to the path
"""
return requests.patch(self.base_url + path,
data=json.dumps(payload),
headers=self.headers)
def get(self, payload={}, path=""):
"""
Via the requests library, performs a get with the payload to the path
"""
return requests.get(self.base_url + path,
data=json.dumps(payload),
headers=self.headers)
def delete(self, payload={}, path=""):
"""
Via the requests library, performs a delete with the payload to the path
"""
return requests.delete(self.base_url + path,
data=json.dumps(payload),
headers=self.headers)
UUID = str(uuid.uuid1())
USER = os.environ["ASTRA_DB_USERNAME"] # NEVER store your creds directly in your code!
PASSWORD = os.environ["ASTRA_DB_PASSWORD"] # NEVER store your creds directly in your code!
DB_ID = os.environ["ASTRA_DB_ID"] # NEVER store your creds directly in your code!
REGION = os.environ["ASTRA_DB_REGION"] # NEVER store your creds directly in your code!
KEYSPACE = os.environ["ASTRA_DB_KEYSPACE"]
BASE_URL = f"https://{DB_ID}-{REGION}.apps.astra.datastax.com"
def authenticate(path="/api/rest/v1/auth"):
"""
This convenience function uses the v1 auth REST API to get an access token
returns: an auth token; 30 minute expiration
"""
url = BASE_URL + path # we still have to auth with the v1 API
payload = {"username": USER,
"password": PASSWORD}
headers = {'accept': '*/*',
'content-type': 'application/json',
'x-cassandra-request-id': UUID}
# make auth request to Astra
r = requests.post(url,
data=json.dumps(payload),
headers=headers)
# raise any authentication errror
if r.status_code != 200:
raise Exception(r.text)
# extract and return the auth token
data = json.loads(r.text)
return data["authToken"]
TOKEN = authenticate()
print(TOKEN)
HEADERS = {'content-type': 'application/json',
'x-cassandra-token': TOKEN}
stargate_client = Client(BASE_URL, TOKEN, HEADERS)
# keyspace, doc root etc
KEYSPACE = "stargate"
TABLE = "users_by_last_name"
TABLE_EMAIL = "certificates_by_email"
DOC_ROOT_PATH = f"/api/rest/v2/keyspaces/{KEYSPACE}/{TABLE}/"
DOC_ROOT_PATH_EMAIL = f"/api/rest/v2/keyspaces/{KEYSPACE}/{TABLE_EMAIL}/"
@app.route('/')
def default():
name = "welcome"
return redirect(url_for('hello', name=name))
@app.route('/hello/', methods=["GET"])
def welcome():
name = "welcome"
return redirect(url_for('hello', name=name))
@app.route('/hello/<name>')
def hello(name):
if not name:
name = ""
response = stargate_client.get(
{}, DOC_ROOT_PATH + name
)
data=json.loads(response.text)
name_data = data["data"]
return render_template('hello.html', name_data=name_data)
@app.route('/find_by_name/<name>')
def get_certificates(name):
response = stargate_client.get(
{}, DOC_ROOT_PATH + name
)
data = json.loads(response.text)
if len(data) == 0:
abort(404)
if data["count"] == 0:
return ("no data for this name")
return redirect(url_for('hello', name=name))
@app.route('/find_by_email/<email>')
def get_certificates_by_email(email):
response = stargate_client.get(
{}, DOC_ROOT_PATH_EMAIL + email
)
data = json.loads(response.text)
if len(data) == 0:
abort(404)
if data["count"] == 0:
return ("no data for this name")
return render_template('hello_email.html', email_data=data["data"])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)