-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
52 lines (34 loc) · 1.04 KB
/
main.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
from http import HTTPStatus
from flask import Flask, jsonify, request
import config
import constants
import decorators
app = Flask(__name__)
logger = config.setup_logger()
@decorators.log(logger, constants.SQUARE)
def square(number):
value = int(number)
return value ** 2
def get_message(name):
return "Hello " + name
@app.route("/", methods=["POST"])
def message():
data = request.json
try:
logger.debug('Data received: {}'.format(data))
message = get_message(data['name'])
logger.debug('Message: {}'.format(message))
except TypeError as err:
logger.error('Error: {}'.format(err))
return jsonify(message="You didn't send name.")
return jsonify(message=message)
@app.route("/square", methods=["POST"])
def hello():
data = request.json
logger.debug({'a': 1, 'b': 2})
try:
number = data['number']
result = square(number)
except Exception:
return jsonify(message="Invalid Payload."), HTTPStatus.BAD_REQUEST
return jsonify(result=result)