-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
53 lines (42 loc) · 1.48 KB
/
Copy pathapp.py
File metadata and controls
53 lines (42 loc) · 1.48 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
#!/usr/bin/python3
"""
This code is used as an example for the Chapter10 of the book DevOps With Linux
"""
from functools import wraps
from flask import Flask, request, jsonify
APP = Flask(__name__)
def check_card(func):
"""
This function validates the credit card transactions
"""
wraps(func)
def validation(*args, **kwargs):
"""
This function is a decorator,
which will return the function corresponding to the respective action
"""
data = request.get_json()
if not data.get("status"):
response = {"approved": False,
"newLimit": data.get("limit"),
"reason": "Blocked Card"}
return jsonify(response)
if data.get("limit") < data.get("transaction").get("amount"):
response = {"approved": False,
"newLimit": data.get("limit"),
"reason": "Transaction above the limit"}
return jsonify(response)
return func(*args, **kwargs)
return validation
@APP.route("/api/transaction", methods=["POST"])
@check_card
def transaction():
"""
This function is resposible to expose the endpoint for receiving the incoming transactions
"""
card = request.get_json()
new_limit = card.get("limit") - card.get("transaction").get("amount")
response = {"approved": True, "newLimit": new_limit}
return jsonify(response)
if __name__ == '__main__':
APP.run(debug=True)