-
Notifications
You must be signed in to change notification settings - Fork 27
/
04-labo-solution.py
92 lines (69 loc) · 2.52 KB
/
04-labo-solution.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
import json
from urllib.error import HTTPError
from urllib.request import Request, urlopen
BASE_URL = "http://localhost:5000"
class ApiError(Exception):
pass
def perform_request(uri, method="GET", data=None):
request = Request('http://localhost:5000/{0}'.format(uri))
request.method = method
request.add_header("content-type", "application/json")
if data:
request.data = json.dumps(data).encode('utf-8')
try:
with urlopen(request) as response:
data = response.read()
headers = response.headers
if headers['content-type'] == "application/json":
return json.loads(data)
else:
return None
except HTTPError as e:
code = e.code
headers = e.headers
data = e.read()
error = ApiError()
error.code = code
if headers['content-type'] == "application/json":
error.content = json.loads(data)
raise error
def get_accounts():
return perform_request("accounts")
def get_account(account_id):
assert isinstance(account_id, int)
try:
return perform_request("accounts/{0}".format(account_id))
except ApiError as e:
if e.code == 404:
return None
else:
raise
def get_transactions(account_id):
assert isinstance(account_id, int)
return perform_request("accounts/{0}/transactions".format(account_id))
def get_transaction(account_id, transaction_id):
assert isinstance(account_id, int)
assert isinstance(transaction_id, int)
return perform_request("accounts/{0}/transactions/{1}".format(account_id, transaction_id))
def create_account(owner, balance):
assert balance >= 0
return perform_request("accounts", "POST", {
"account": { "owner": owner, "current_balance": balance }
})
def create_transaction(from_account_id, to_account_id, amount):
assert isinstance(from_account_id, int)
assert isinstance(to_account_id, int)
assert amount >= 0
try:
return perform_request("transactions", "POST", {
"transaction": { "from_account": from_account_id, "to_account": to_account_id, "amount": amount }
})
except ApiError as e:
if e.code == 422:
if e.content['error']['code'] == "insuffisant-funds":
print("Les fonds ne sont pas suffisant")
if e.content['error']['code'] == "account-not-found":
print("Le compte n'existe pas")
return None
else:
raise