forked from sentry-demos/flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
75 lines (59 loc) · 1.9 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
import os
from flask import Flask, request, json, abort
from flask_cors import CORS
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
dsn="https://2ba68720d38e42079b243c9c5774e05c@sentry.io/1316515",
integrations=[FlaskIntegration()],
release=os.environ.get("VERSION")
)
app = Flask(__name__)
CORS(app)
@app.route('/handled', methods=['GET'])
def handled_exception():
try:
'2' + 2
except Exception as err:
sentry_sdk.capture_exception(err)
abort(500)
return 'Success'
@app.route('/unhandled', methods=['GET'])
def unhandled_exception():
obj = {}
obj['keyDoesntExist']
Inventory = {
'wrench': 1,
'nails': 1,
'hammer': 1
}
def process_order(cart):
global Inventory
tempInventory = Inventory
for item in cart:
if Inventory[item['id']] <= 0:
raise Exception("Not enough inventory for " + item['id'])
else:
tempInventory[item['id']] -= 1
print 'Success: ' + item['id'] + ' was purchased, remaining stock is ' + str(tempInventory[item['id']])
Inventory = tempInventory
@app.before_request
def sentry_event_context():
if (request.data):
order = json.loads(request.data)
with sentry_sdk.configure_scope() as scope:
scope.user = { "email" : order["email"] }
transactionId = request.headers.get('X-Transaction-ID')
sessionId = request.headers.get('X-Session-ID')
global Inventory
with sentry_sdk.configure_scope() as scope:
scope.set_tag("transaction_id", transactionId)
scope.set_tag("session-id", sessionId)
scope.set_extra("inventory", Inventory)
@app.route('/checkout', methods=['POST'])
def checkout():
order = json.loads(request.data)
print "Processing order for: " + order["email"]
cart = order["cart"]
process_order(cart)
return 'Success'