-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
82 lines (76 loc) · 2.58 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
"""Flask App"""
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
import logging
import time
import json
import yaml
from lexer_parser import understand
from services import user, problem, mongodb, properties
app = Flask(__name__)
CORS(app)
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s-%(funcName)s-%(asctime)s: %(message)s")
# Endpoints
@app.route('/')
@cross_origin()
def index():
"""Hello World"""
return "Hello World!"
# User API Endpoint
@app.route('/api/user', methods = ['POST','GET'])
@cross_origin()
def user_endpoint():
""" Create and Exists operations for User """
if request.method == 'GET':
# Checking if user exists
email = request.args.get('email')
space = request.args.get('space')
if user.exists(space, email):
return jsonify(True)
return jsonify(False)
if request.method == 'POST':
# Storing user survey details
record = json.loads(request.data)
space = record["space"]
return jsonify(user.create(space, record))
# Problem Endpoint
@app.route('/api/problem', methods = ['GET', 'POST'])
@cross_origin()
def problem_endpoint():
""" Render specified problem grid """
if request.method == 'GET':
# Getting problem grid for the requested level
level = request.args.get('level')
return jsonify(problem.render(level))
if request.method == 'POST':
level = request.json['level']
space = request.json['space']
problem.render(level)
user_email = request.json['email']
commands = request.json['commands']
response = understand(commands)
to_log = {
"email": user_email,
"timestamp": str(time.time()),
"level": level,
"commands": commands,
"response": response
}
logging.info(f'Received commands and executed to get response: {to_log}')
mongodb.insert_one(space, properties.COMMANDS_COLLECTION, to_log)
return jsonify(response)
# Level Endpoint
@app.route('/api/level', methods = ['GET'])
@cross_origin()
def level_endpoint():
"""Get levels list for space """
if request.method == 'GET':
levels_list = []
space = request.args.get('space')
with open('config.yaml') as config_file:
config = yaml.load(config_file, Loader=yaml.FullLoader)
levels = config['levels']
for level in levels:
if space in levels[level]['space']:
levels_list.append(level)
return jsonify(levels_list)