-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·36 lines (31 loc) · 1.12 KB
/
app.py
File metadata and controls
executable file
·36 lines (31 loc) · 1.12 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
from flask import Flask, jsonify, request, make_response, render_template
from flask_cors import CORS, cross_origin
import json
import CodingChallenge
import urllib
app = Flask(__name__)
cors = CORS(app, resources={"/api": {"origins": "*"}})
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route('/')
def index():
return render_template('index.html')
@app.errorhandler(404)
def not_found(error):
"""
Returns 404 error upon inavlid endpoint
"""
return make_response(jsonify({"Usage": "Please make POST request to /api/containsOrigi with 'vertices' as key and comma separated coordinates as Values"}), 404)
@app.route("/api/containsOrigin", methods=["POST"])
def return_contains_origin():
"""
Returns True if the given triangle encloses the origin,
returns 400 (BAD REQUEST) on invalid triangle
"""
try:
vertices = urllib.unquote(urllib.unquote(request.form['vertices']))
main_triangle = CodingChallenge.Triangle(*vertices.split(","))
return jsonify(main_triangle.contains_origin())
except TypeError:
return make_response("Please provide a valid tringle with 6 vertices", 400)
if __name__ == "__main__":
app.run(debug=True)