forked from an-hai-tran/IBM-CFC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
75 lines (60 loc) · 2.54 KB
/
app.py
File metadata and controls
75 lines (60 loc) · 2.54 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
from flask import Flask, render_template,request
import requests
import json
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
# from ibm_platform_services.iam_identity_v1 import *
from ibm_platform_services import IamIdentityV1
import config
app = Flask(__name__)
@app.route("/")
def home():
return render_template('home.html')
@app.route("/form")
def form():
return render_template("form.html")
@app.route('/result',methods =["GET", "POST"])
def result():
form_input = request.form.to_dict()
if len(form_input) != 0:
if (form_input["state"] != "NJ"):
return render_template("noresult.html", message = "Sorry, our application currently only supports residents in New Jersey. Please check back again for further updates.")
else:
#access token
headers_token = {
'accept': 'application/json',
}
data_token = {
'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
'apikey': config.api_key
}
response_token = requests.post('https://iam.cloud.ibm.com/identity/token', headers=headers_token, data=data_token)
json_object = json.loads(response_token.text)
#fetch api
headers = {
'accept': 'application/json',
'authorization': 'Bearer ' + json_object["access_token"],
}
params = {
'blocking': 'true',
}
data = {
"race": form_input["Race"],
"county": form_input["county"]
}
response = requests.post('https://us-east.functions.cloud.ibm.com/api/v1/namespaces/26095e4b-8f64-4b8f-b1b6-59b7376c4898/actions/vaccine-algo-api-1', params=params, json = data, headers=headers)
response = dict(json.loads(response.text))
response=response["response"]["result"]
address = response["address"]
url="http://maps.google.com/maps?q="
for i in address.split():
url = url + "+" +i
if (response['provider'] == "Unavailable"):
return render_template("noresult.html", message="Sorry, no provider is available to you at the moment. Please check back again later.")
else:
return render_template("result.html", response=response, url=url)
return render_template("form.html")
@app.route("/info")
def info():
return render_template("info.html")
if __name__=="__main__":
app.run(debug=True)