-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
266 lines (199 loc) · 7.58 KB
/
client.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""Tucker Sync client module.
Usage:
from client import Client
client = Client(base_url)
client.check_connection()
License:
The MIT License (MIT), see LICENSE.txt for more details.
Copyright:
Copyright (c) 2014 Steven Tucker and Gavin Kromhout
"""
import requests
import uuid
from common import APIRequestType, JSONKey, APIErrorCode, HTTP, JSON, Logger, \
APIRequest, AccountOpenRequestBody, AccountModifyRequestBody
LOG = Logger(__file__)
class Client(object):
"""A Tucker Sync Client Implementation."""
def __init__(self, base_url, key, email, password):
self.request = APIRequest()
self.base_url = base_url
self.key = key
self.email = email
self.password = password
self.UUID = uuid.uuid4()
# TODO init storage.
@property
def base_url(self):
return self.request.base_url
@base_url.setter
def base_url(self, base_url):
self.request.base_url = base_url
@property
def key(self):
return self.request.key
@key.setter
def key(self, key):
self.request.key = key
@property
def email(self):
return self.request.email
@email.setter
def email(self, email):
self.request.email = email
@property
def password(self):
return self.request.password
@password.setter
def password(self, password):
self.request.password = password
def check_connection(self):
"""Check the connection to the server.
Return true if the server responds with an API error code."""
try:
self.post_request(APIRequestType.TEST)
except ClientException:
LOG.debug(self, 'Check connection failed with an exception.')
return False
# Success
return True
def check_authentication(self):
"""Check authentication against an account on the server."""
try:
jo = self.post_request(APIRequestType.TEST)
except ClientException:
LOG.debug(self, 'Check authentication failed with an exception.')
return False
error_code = jo[JSONKey.ERROR]
if error_code != APIErrorCode.SUCCESS:
LOG.debug(self,
'Check authentication failed with API error code = %s',
error_code)
LOG.debug(self,
'Check authentication failed with API error name = %s',
APIErrorCode.name(error_code))
return False
# Success
return True
def account_open(self):
"""Open a new account on the server."""
rb = AccountOpenRequestBody()
rb.clientUUID = self.UUID
js = JSON.dumps(rb.to_primitive())
try:
jo = self.post_request(APIRequestType.ACCOUNT_OPEN, js)
except ClientException:
LOG.debug(self, 'Account open failed with an exception.')
return False
error_code = jo[JSONKey.ERROR]
if error_code != APIErrorCode.SUCCESS:
LOG.debug(self, 'Account open failed with API error code = %s',
error_code)
LOG.debug(self, 'Account open failed with API error name = %s',
APIErrorCode.name(error_code))
return False
# Success
return True
def account_close(self):
"""Close an existing account on the server."""
try:
jo = self.post_request(APIRequestType.ACCOUNT_CLOSE)
except ClientException:
LOG.debug(self, 'Account close failed with an exception.')
return False
error_code = jo[JSONKey.ERROR]
if error_code != APIErrorCode.SUCCESS:
LOG.debug(self, 'Account close failed with API error code = %s',
error_code)
LOG.debug(self, 'Account close failed with API error name = %s',
APIErrorCode.name(error_code))
return False
# Success
return True
def account_modify(self, new_email, new_password):
"""Modify an existing account on the server."""
request_body = AccountModifyRequestBody()
request_body.email = new_email
request_body.password = new_password
try:
js = self.get_json_request_string(request_body)
except ClientException:
LOG.debug(self, 'Account modify failed with an exception.')
return False
try:
jo = self.post_request(APIRequestType.ACCOUNT_MODIFY, js)
except ClientException:
LOG.debug(self, 'Account modify failed with an exception.')
return False
error_code = jo[JSONKey.ERROR]
if error_code != APIErrorCode.SUCCESS:
LOG.debug(self, 'Account modify failed with API error code = %s',
error_code)
LOG.debug(self, 'Account modify failed with API error name = %s',
APIErrorCode.name(error_code))
return False
# Success
return True
def get_json_request_string(self, model):
"""Get json request string from model or raise a ClientException."""
# Validate before conversion.
try:
model.validate()
except Exception as e:
LOG.debug(self, 'Validate exception = %s' % e)
raise ClientException
try:
js = JSON.dumps(model.to_primitive())
except Exception as e:
LOG.debug(self, 'JSON dumps exception = %s' % e)
raise ClientException
return js
def post_request(self, api_request_type, data=None):
"""Post the request.
Return the response json object (Python dictionary) or
raise an exception."""
self.request.type = api_request_type
self.request.body = data
LOG.debug(self, 'base_url = %s', self.request.base_url)
LOG.debug(self, 'params = %s', self.request.params)
LOG.debug(self, 'headers= %s', self.request.headers)
LOG.debug(self, 'body = %s', self.request.body)
try:
response = requests.post(self.request.base_url,
self.request.body,
params=self.request.params,
headers=self.request.headers)
except Exception as e:
LOG.debug(self, 'Request post failed with exception = %s', e)
raise ClientException
try:
jo = self.get_json_object(response)
except ClientException:
raise ClientException
# Success.
return jo
@staticmethod
def get_json_object(response):
"""Get the json object (Python dictionary) from the response.
Return jo or raise an exception."""
LOG.debug(Client, 'status_code = %s', response.status_code)
LOG.debug(Client, 'content = %s', response.content)
if response.status_code != HTTP.OK:
LOG.debug(Client, 'HTTP status code != HTTP.OK')
raise ClientException
try:
jo = JSON.loads(response.content)
except Exception as e:
LOG.debug(Client, 'JSON.loads exception = %s', e)
raise ClientException
if not type(jo) is dict:
LOG.debug(Client, 'Type of jo is not an object/dict.')
raise ClientException
if JSONKey.ERROR not in jo:
LOG.debug(Client, 'The decoded jo has no `error` key.')
raise ClientException
# Success.
return jo
class ClientException(Exception):
"""Custom exception class."""
pass