2
2
3
3
import httplib
4
4
from flask import Flask , make_response , url_for
5
+ from flask .json import jsonify
5
6
from flask_restful import Api , abort
6
7
from webargs .flaskparser import parser
7
8
@@ -180,8 +181,8 @@ def open(self, partner_address, token_address, settle_timeout, balance=None):
180
181
balance
181
182
)
182
183
183
- result = self .channel_schema .dumps (channel_to_api_dict (raiden_service_result ))
184
- return result
184
+ result = self .channel_schema .dump (channel_to_api_dict (raiden_service_result ))
185
+ return jsonify ( result . data )
185
186
186
187
def deposit (self , token_address , partner_address , amount ):
187
188
@@ -191,8 +192,8 @@ def deposit(self, token_address, partner_address, amount):
191
192
amount
192
193
)
193
194
194
- result = self .channel_schema .dumps (channel_to_api_dict (raiden_service_result ))
195
- return result
195
+ result = self .channel_schema .dump (channel_to_api_dict (raiden_service_result ))
196
+ return jsonify ( result . data )
196
197
197
198
def close (self , token_address , partner_address ):
198
199
@@ -201,8 +202,8 @@ def close(self, token_address, partner_address):
201
202
partner_address
202
203
)
203
204
204
- result = self .channel_schema .dumps (channel_to_api_dict (raiden_service_result ))
205
- return result
205
+ result = self .channel_schema .dump (channel_to_api_dict (raiden_service_result ))
206
+ return jsonify ( result . data )
206
207
207
208
def connect (
208
209
self ,
@@ -231,8 +232,8 @@ def get_channel_list(self, token_address=None, partner_address=None):
231
232
assert isinstance (raiden_service_result , list )
232
233
233
234
channel_list = ChannelList (raiden_service_result )
234
- result = self .channel_list_schema .dumps (channel_list )
235
- return result
235
+ result = self .channel_list_schema .dump (channel_list )
236
+ return jsonify ( result . data )
236
237
237
238
def get_tokens_list (self ):
238
239
raiden_service_result = self .raiden_api .get_tokens_list ()
@@ -243,8 +244,8 @@ def get_tokens_list(self):
243
244
new_list .append ({'address' : result })
244
245
245
246
tokens_list = TokensList (new_list )
246
- result = self .tokens_list_schema .dumps (tokens_list )
247
- return result
247
+ result = self .tokens_list_schema .dump (tokens_list )
248
+ return jsonify ( result . data )
248
249
249
250
def get_network_events (self , from_block , to_block ):
250
251
raiden_service_result = self .raiden_api .get_network_events (
@@ -266,7 +267,8 @@ def get_channel_events(self, channel_address, from_block, to_block):
266
267
267
268
def get_channel (self , channel_address ):
268
269
channel = self .raiden_api .get_channel (channel_address )
269
- return self .channel_schema .dumps (channel_to_api_dict (channel ))
270
+ result = self .channel_schema .dump (channel_to_api_dict (channel ))
271
+ return jsonify (result .data )
270
272
271
273
def get_partners_by_token (self , token_address ):
272
274
return_list = []
@@ -282,8 +284,8 @@ def get_partners_by_token(self, token_address):
282
284
})
283
285
284
286
schema_list = PartnersPerTokenList (return_list )
285
- result = self .partner_per_token_list_schema .dumps (schema_list )
286
- return result
287
+ result = self .partner_per_token_list_schema .dump (schema_list )
288
+ return jsonify ( result . data )
287
289
288
290
def initiate_transfer (self , token_address , target_address , amount , identifier ):
289
291
@@ -300,18 +302,19 @@ def initiate_transfer(self, token_address, target_address, amount, identifier):
300
302
amount = amount ,
301
303
identifier = identifier
302
304
)
303
-
304
- transfer = {
305
- 'initiator_address' : self .raiden_api .raiden .address ,
306
- 'token_address' : token_address ,
307
- 'target_address' : target_address ,
308
- 'amount' : amount ,
309
- 'identifier' : identifier ,
310
- }
311
- return self .transfer_schema .dumps (transfer )
312
305
except (InvalidAmount , InvalidAddress , NoPathError ) as e :
313
306
return make_response (str (e ), httplib .CONFLICT )
314
307
308
+ transfer = {
309
+ 'initiator_address' : self .raiden_api .raiden .address ,
310
+ 'token_address' : token_address ,
311
+ 'target_address' : target_address ,
312
+ 'amount' : amount ,
313
+ 'identifier' : identifier ,
314
+ }
315
+ result = self .transfer_schema .dump (transfer )
316
+ return jsonify (result .data )
317
+
315
318
def patch_channel (self , channel_address , balance = None , state = None ):
316
319
if balance is not None and state is not None :
317
320
return make_response (
@@ -327,6 +330,7 @@ def patch_channel(self, channel_address, balance=None, state=None):
327
330
# find the channel
328
331
channel = self .raiden_api .get_channel (channel_address )
329
332
current_state = channel .state
333
+
330
334
# if we patch with `balance` it's a deposit
331
335
if balance is not None :
332
336
if current_state != CHANNEL_STATE_OPENED :
@@ -339,37 +343,40 @@ def patch_channel(self, channel_address, balance=None, state=None):
339
343
channel .partner_address ,
340
344
balance
341
345
)
342
- return self .channel_schema .dumps (channel_to_api_dict (raiden_service_result ))
346
+ result = self .channel_schema .dump (channel_to_api_dict (raiden_service_result ))
347
+ return jsonify (result .data )
343
348
344
- else :
345
- if state == CHANNEL_STATE_CLOSED :
346
- if current_state != CHANNEL_STATE_OPENED :
347
- return make_response (
348
- httplib .CONFLICT ,
349
- 'Attempted to close an already closed channel'
350
- )
351
- raiden_service_result = self .raiden_api .close (
352
- channel .token_address ,
353
- channel .partner_address
354
- )
355
- return self .channel_schema .dumps (channel_to_api_dict (raiden_service_result ))
356
- elif state == CHANNEL_STATE_SETTLED :
357
- if current_state == CHANNEL_STATE_SETTLED or current_state == CHANNEL_STATE_OPENED :
358
- return make_response (
359
- 'Attempted to settle a channel at its {} state' .format (current_state ),
360
- httplib .CONFLICT ,
361
- )
362
- raiden_service_result = self .raiden_api .settle (
363
- channel .token_address ,
364
- channel .partner_address
349
+ if state == CHANNEL_STATE_CLOSED :
350
+ if current_state != CHANNEL_STATE_OPENED :
351
+ return make_response (
352
+ httplib .CONFLICT ,
353
+ 'Attempted to close an already closed channel'
365
354
)
366
- return self .channel_schema .dumps (channel_to_api_dict (raiden_service_result ))
367
- else :
368
- # should never happen, channel_state is validated in the schema
355
+ raiden_service_result = self .raiden_api .close (
356
+ channel .token_address ,
357
+ channel .partner_address
358
+ )
359
+ result = self .channel_schema .dump (channel_to_api_dict (raiden_service_result ))
360
+ return jsonify (result .data )
361
+
362
+ if state == CHANNEL_STATE_SETTLED :
363
+ if current_state == CHANNEL_STATE_SETTLED or current_state == CHANNEL_STATE_OPENED :
369
364
return make_response (
370
- 'Provided invalid channel state {} ' .format (state ),
371
- httplib .BAD_REQUEST ,
365
+ 'Attempted to settle a channel at its {} state ' .format (current_state ),
366
+ httplib .CONFLICT ,
372
367
)
368
+ raiden_service_result = self .raiden_api .settle (
369
+ channel .token_address ,
370
+ channel .partner_address
371
+ )
372
+ result = self .channel_schema .dump (channel_to_api_dict (raiden_service_result ))
373
+ return jsonify (result .data )
374
+
375
+ # should never happen, channel_state is validated in the schema
376
+ return make_response (
377
+ 'Provided invalid channel state {}' .format (state ),
378
+ httplib .BAD_REQUEST ,
379
+ )
373
380
374
381
def token_swap (
375
382
self ,
0 commit comments