-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (44 loc) · 1.7 KB
/
main.py
File metadata and controls
51 lines (44 loc) · 1.7 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
import os
from flask import Flask, request
import json
# create a Flask instance
app = Flask(__name__)
# a simple description of the API written in html.
# Flask can print and return raw text to the browser.
# This enables html, json, etc.
description = """
<!DOCTYPE html>
<head>
<title>API Landing</title>
</head>
<body>
<h3>A simple API using Flask</h3>
<p> Usage: http://FQDN/api?value=2</p>
</body>
"""
# Routes refer to url'
# our root url '/' will show our html description
@app.route('/', methods=['GET'])
def hello_world():
# return a html format string that is rendered in the browser
return description
# our '/api' url
# requires user integer argument: value
# returns error message if wrong arguments are passed.
@app.route('/api', methods=['GET'])
def square():
if not all(k in request.args for k in (["value"])):
supplied_parameters = f"{[k for k in request.args]}"
return json.dumps({"Required parameters" : 'value', "Supplied parameters" : supplied_parameters}), 400
else:
# assign and cast variable to int
value = int(request.args['value'])
# or use the built in get method and assign a type
# http://werkzeug.palletsprojects.com/en/0.15.x/datastructures/#werkzeug.datastructures.MultiDict.get
value = request.args.get('value', type=int)
return json.dumps({"Value Squared" : value**2})
if __name__ == "__main__":
# for debugging locally
# app.run(debug=True, host='0.0.0.0',port=5000)
# for production
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))