-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
91 lines (71 loc) · 2.41 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
83
84
85
86
87
88
89
90
91
import json
from http import HTTPStatus
from flask import Flask
from flask import request as flask_request
from marshmallow.exceptions import ValidationError
from sqlalchemy.exc import NoResultFound
from .storm.exceptions import MissingPostData, EmptyResult
from .storm.db.client import client
from .storm.services.functions import delete_schedule, get_schedule, post_schedule
app = Flask(__name__)
@app.route("/msg-scheduler", methods=["POST"])
def msg_scheduler_POST():
request_data = flask_request.get_json()
if not flask_request:
raise MissingPostData
processed_response = post_schedule(client, request_data)
return app.response_class(
response=json.dumps(processed_response),
status=HTTPStatus.CREATED,
)
@app.route("/msg-scheduler", methods=["GET"])
def msg_scheduler_GET():
request_id = flask_request.args
processed_response = get_schedule(client, request_id)
return app.response_class(
status=HTTPStatus.OK,
response=json.dumps(processed_response),
mimetype="application/json",
)
@app.route("/msg-scheduler", methods=["DELETE"])
def msg_scheduler_DELETE():
request_id = {"id": flask_request.args.get("id")}
processed_response = delete_schedule(client, request_id)
return app.response_class(
status=HTTPStatus.OK,
response=json.dumps(processed_response),
mimetype="application/json",
)
@app.errorhandler(EmptyResult)
def handle_no_result(e):
return app.response_class(
status=HTTPStatus.NOT_FOUND,
response="The requested object was not found",
mimetype="application/json",
)
@app.errorhandler(MissingPostData)
def handle_missing_post_data(e):
return app.response_class(
status=HTTPStatus.BAD_REQUEST,
response="Missing POST data",
mimetype="application/json",
)
@app.errorhandler(ValidationError)
def handle_validation_error(e):
message = ""
for key in e.messages:
message += f"{key}:"
for item in e.messages[key]:
message += f" {item}"
return app.response_class(
status=HTTPStatus.BAD_REQUEST,
response=json.dumps({"message": message}),
mimetype="application/json",
)
@app.errorhandler(500)
def internal_error():
return app.response_class(
status=HTTPStatus.INTERNAL_SERVER_ERROR,
response="Internal server error",
mimetype="application/json",
)