-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bank.py
115 lines (87 loc) · 3.08 KB
/
Bank.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import requests
URL = "http://127.0.0.1:8000"
KEY_SUCCESS = 'success'
KEY_ACCOUNT = 'account'
KEY_BALANCE = 'balance'
KEY_MSG1 = 'message'
KEY_MSG2 = 'message2'
class Result:
"""
Object to save the result of an API call
"""
success = False
error = False
account = None
balance = None
message1 = None
message2 = None
def __init__(self, success=False, account=None, balance=None, message1=None, message2=None, error=False):
"""
:param success: Was the operation successful
:type success: bool or None
:param token: Name of the account
:type token: str or None
:param balance: Account balance in cents (1.23€ => 123)
:type balance: int or None
:param message1: Server-sent message row 1
:type message1: str
:param message2: Server-sent message row 2
:type message2: str
:param error: Did an error occur while parsing the API call result
:type error: bool
"""
self.success = success
self.account = account
self.balance = balance
self.message1 = message1
self.message2 = message2
self.error = error
@staticmethod
def from_object(o):
try:
success = o[KEY_SUCCESS]
if success:
account = o[KEY_ACCOUNT]
balance = o[KEY_BALANCE]
message1 = None
message2 = None
else:
account = None
balance = None
message1 = o[KEY_MSG1]
if KEY_MSG2 in o.keys():
message2 = o[KEY_MSG2]
else:
message2 = None
return Result(success=success, account=account, balance=balance,
message1=message1,message2=message2, error=False)
except ValueError:
return Result(error=True)
class Bank:
def __init__(self):
pass
def make_transaction(self, token, amount, message=None):
"""
Attempt to make a transaction
:param token: The token associated with the account to do the transaction against
:type token: str
:param amount: Amount of currency to move in cents
:type amount: int
:return: Result of the transaction
:rtype: Result
"""
if message:
req = requests.post(URL + "/api/token/" + token + "/transaction", {'amount': amount, 'message': message})
else:
req = requests.post(URL + "/api/token/" + token + "/transaction", {'amount': amount})
return Result.from_object(req.json())
def get_balance(self, token):
"""
Attempt to get account balance
:param token: The token associated with the account to get the balance for
:type token: str
:return: Account balance
:rtype: Result
"""
req = requests.get(URL + "/api/token/" + token + "/balance")
return Result.from_object(req.json())