-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
101 lines (81 loc) · 2.56 KB
/
app.py
File metadata and controls
101 lines (81 loc) · 2.56 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
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
92
93
94
95
96
97
98
99
100
101
import os
import config
from logging.config import dictConfig
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
# Logs configuration
dictConfig({
"version": 1,
"formatters": {
"console": {
"format": "[%(asctime)s] [%(levelname)s] %(module)s: %(message)s"
},
"file": {
"format": ("[%(asctime)s] [%(levelname)s] %(pathname)s - "
"line %(lineno)d: \n%(message)s\n")
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
"formatter": "console"
},
"file": {
"class": "logging.FileHandler",
"filename": os.getenv("LOG_FILE", default="webhooks.log"),
"formatter": "file"
}
},
"root": {
"level": "INFO",
"handlers": ["console", "file"]
}
})
app = Flask(__name__)
app.config.from_object(config.Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from events import Event
from middleware import verify_signature
@app.route("/webhook", methods=["POST"])
@verify_signature
def webhook():
"""Receives the Fintoc webhooks."""
try:
app.logger.info("POST request to webhook action")
data = request.get_json(force=True)
existing_event = Event.query.filter_by(id=data["id"]).first()
if existing_event:
app.logger.info(f"Event with id {data['id']} already exists")
return jsonify({
"success": False,
"message": f"Event with id {data['id']} already exists",
}), 200
event = Event(event=data)
# Save Event on the database
db.session.add(event)
db.session.commit()
app.logger.info(
f"Event type {event.type} generated with id {event.id}"
)
return jsonify({
"success": True,
"id": event.id,
"type": event.type,
}), 200
except Exception as err:
app.logger.error(err)
return jsonify({"success": False}), 500
@app.route("/events", methods=["GET"])
def get_events():
"""Get every saved event."""
try:
app.logger.info("GET request to get_events action")
events = Event.query.all()
app.logger.info(f"Returning {len(events)} events on the HTTP response.")
return jsonify([x.serialize() for x in events]), 200
except Exception as err:
app.logger.error(err)
return jsonify({"success": False}), 500