-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
180 lines (133 loc) · 4.84 KB
/
api.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import requests
DISCOVERY_URI = "https://prod-s0-webapp-proxy.nubank.com.br/api/discovery"
REQUEST_HEADERS = {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Origin": "https://conta.nubank.com.br",
"Referer": "https://conta.nubank.com.br/",
}
class APIError(RuntimeError):
pass
def json_response(func):
def wrapper(self, *args, **kwargs):
response = func(self, *args, **kwargs)
try:
response_data = response.json()
except ValueError:
raise APIError("Unexpected response content")
if response_data.has_key("error"):
raise APIError("API error: %s" % (response_data["error"], ))
if not response.ok:
raise APIError("HTTP error: %s (code %d)" % (response.reason, response.status_code))
return response_data
return wrapper
def auth_required(func):
def wrapper(self, *args, **kwargs):
if self._signin_info is None:
raise APIError("You need to authenticate first")
return func(self, *args, **kwargs)
return wrapper
class NubankAPI(object):
_links = None
_signin_info = None
def login(self, login, password):
self._signin_info = self._login(login, password)
self._links.update(map(lambda l: (l[0], l[1]["href"]), self._signin_info["_links"].items()))
@auth_required
@json_response
def customer(self):
headers = {
"Authorization": "Bearer %s" % (self._signin_info["access_token"], ),
}
headers.update(REQUEST_HEADERS)
customer = requests.get(self._links["customer"], headers=headers)
return customer
@auth_required
@json_response
def revoke_all(self):
headers = {
"Authorization": "Bearer %s" % (self._signin_info["access_token"], ),
}
headers.update(REQUEST_HEADERS)
response = requests.post(self._links["account"], headers=headers)
return response
@auth_required
@json_response
def account(self):
headers = {
"Authorization": "Bearer %s" % (self._signin_info["access_token"], ),
}
headers.update(REQUEST_HEADERS)
account = requests.get(self._links["account"], headers=headers)
return account
@auth_required
@json_response
def events_page(self):
headers = {
"Authorization": "Bearer %s" % (self._signin_info["access_token"], ),
}
headers.update(REQUEST_HEADERS)
events_page = requests.get(self._links["events_page"], headers=headers)
return events_page
@auth_required
@json_response
def logout(self):
headers = {
"Authorization": "Bearer %s" % (self._signin_info["access_token"], ),
}
headers.update(REQUEST_HEADERS)
response = requests.post(self._links["revoke_token"], headers=headers)
return response
@auth_required
@json_response
def userinfo(self):
headers = {
"Authorization": "Bearer %s" % (self._signin_info["access_token"], ),
}
headers.update(REQUEST_HEADERS)
userinfo = requests.get(self._links["userinfo"], headers=headers)
return userinfo
@auth_required
@json_response
def change_password(self, password):
payload = {
"password": password,
}
headers = {
"Authorization": "Bearer %s" % (self._signin_info["access_token"], ),
}
headers.update(REQUEST_HEADERS)
response = requests.post(self._links["change_password"], json=payload, headers=headers).json()
return response
@auth_required
@json_response
def events(self):
headers = {
"Authorization": "Bearer %s" % (self._signin_info["access_token"], ),
}
headers.update(REQUEST_HEADERS)
events = requests.get(self._links["events"], headers=headers)
return events
@auth_required
@json_response
def bills_summary(self):
headers = {
"Authorization": "Bearer %s" % (self._signin_info["access_token"], ),
}
headers.update(REQUEST_HEADERS)
bills_summary = requests.get(self._links["bills_summary"], headers=headers)
return bills_summary
@json_response
def _discovery(self):
return requests.get(DISCOVERY_URI, headers=REQUEST_HEADERS)
@json_response
def _login(self, login, password):
payload = {
"login": login,
"password": password,
"grant_type": "password",
"client_id": "other.conta",
"client_secret": "yQPeLzoHuJzlMMSAjC-LgNUJdUecx8XO",
}
self._links = self._discovery()
return requests.post(self._links["login"], json=payload, headers=REQUEST_HEADERS)