-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
47 lines (33 loc) · 997 Bytes
/
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
import random
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/spongemock', methods=["GET"])
def mockText():
if validate_query(request):
string = request.args.get('text')
chars = [x for x in string.lower()]
mocked_string = []
for char in chars:
if random.randint(0, 1) > 0:
mocked_string.append(char.upper())
else:
mocked_string.append(char)
return jsonify({
"error": None,
"mockedText": "".join(mocked_string)
}), 200
else:
return jsonify(invalid_query()), 400
def validate_query(request_data):
if request_data.args.get('text') is None:
return False
if type(request_data.args.get('text')) is not str:
return False
return True
def invalid_query():
return {
"error": "query is not valid",
"mockedText": None
}
if __name__ == '__main__':
app.run(port=5000)