Skip to content

Commit

Permalink
fixed double encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
hackaugusto authored and LefterisJP committed May 31, 2017
1 parent b553747 commit 1210134
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
105 changes: 56 additions & 49 deletions raiden/api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import httplib
from flask import Flask, make_response, url_for
from flask.json import jsonify
from flask_restful import Api, abort
from webargs.flaskparser import parser

Expand Down Expand Up @@ -180,8 +181,8 @@ def open(self, partner_address, token_address, settle_timeout, balance=None):
balance
)

result = self.channel_schema.dumps(channel_to_api_dict(raiden_service_result))
return result
result = self.channel_schema.dump(channel_to_api_dict(raiden_service_result))
return jsonify(result.data)

def deposit(self, token_address, partner_address, amount):

Expand All @@ -191,8 +192,8 @@ def deposit(self, token_address, partner_address, amount):
amount
)

result = self.channel_schema.dumps(channel_to_api_dict(raiden_service_result))
return result
result = self.channel_schema.dump(channel_to_api_dict(raiden_service_result))
return jsonify(result.data)

def close(self, token_address, partner_address):

Expand All @@ -201,8 +202,8 @@ def close(self, token_address, partner_address):
partner_address
)

result = self.channel_schema.dumps(channel_to_api_dict(raiden_service_result))
return result
result = self.channel_schema.dump(channel_to_api_dict(raiden_service_result))
return jsonify(result.data)

def connect(
self,
Expand Down Expand Up @@ -231,8 +232,8 @@ def get_channel_list(self, token_address=None, partner_address=None):
assert isinstance(raiden_service_result, list)

channel_list = ChannelList(raiden_service_result)
result = self.channel_list_schema.dumps(channel_list)
return result
result = self.channel_list_schema.dump(channel_list)
return jsonify(result.data)

def get_tokens_list(self):
raiden_service_result = self.raiden_api.get_tokens_list()
Expand All @@ -243,8 +244,8 @@ def get_tokens_list(self):
new_list.append({'address': result})

tokens_list = TokensList(new_list)
result = self.tokens_list_schema.dumps(tokens_list)
return result
result = self.tokens_list_schema.dump(tokens_list)
return jsonify(result.data)

def get_network_events(self, from_block, to_block):
raiden_service_result = self.raiden_api.get_network_events(
Expand All @@ -266,7 +267,8 @@ def get_channel_events(self, channel_address, from_block, to_block):

def get_channel(self, channel_address):
channel = self.raiden_api.get_channel(channel_address)
return self.channel_schema.dumps(channel_to_api_dict(channel))
result = self.channel_schema.dump(channel_to_api_dict(channel))
return jsonify(result.data)

def get_partners_by_token(self, token_address):
return_list = []
Expand All @@ -282,8 +284,8 @@ def get_partners_by_token(self, token_address):
})

schema_list = PartnersPerTokenList(return_list)
result = self.partner_per_token_list_schema.dumps(schema_list)
return result
result = self.partner_per_token_list_schema.dump(schema_list)
return jsonify(result.data)

def initiate_transfer(self, token_address, target_address, amount, identifier):

Expand All @@ -300,18 +302,19 @@ def initiate_transfer(self, token_address, target_address, amount, identifier):
amount=amount,
identifier=identifier
)

transfer = {
'initiator_address': self.raiden_api.raiden.address,
'token_address': token_address,
'target_address': target_address,
'amount': amount,
'identifier': identifier,
}
return self.transfer_schema.dumps(transfer)
except (InvalidAmount, InvalidAddress, NoPathError) as e:
return make_response(str(e), httplib.CONFLICT)

transfer = {
'initiator_address': self.raiden_api.raiden.address,
'token_address': token_address,
'target_address': target_address,
'amount': amount,
'identifier': identifier,
}
result = self.transfer_schema.dump(transfer)
return jsonify(result.data)

def patch_channel(self, channel_address, balance=None, state=None):
if balance is not None and state is not None:
return make_response(
Expand All @@ -327,6 +330,7 @@ def patch_channel(self, channel_address, balance=None, state=None):
# find the channel
channel = self.raiden_api.get_channel(channel_address)
current_state = channel.state

# if we patch with `balance` it's a deposit
if balance is not None:
if current_state != CHANNEL_STATE_OPENED:
Expand All @@ -339,37 +343,40 @@ def patch_channel(self, channel_address, balance=None, state=None):
channel.partner_address,
balance
)
return self.channel_schema.dumps(channel_to_api_dict(raiden_service_result))
result = self.channel_schema.dump(channel_to_api_dict(raiden_service_result))
return jsonify(result.data)

else:
if state == CHANNEL_STATE_CLOSED:
if current_state != CHANNEL_STATE_OPENED:
return make_response(
httplib.CONFLICT,
'Attempted to close an already closed channel'
)
raiden_service_result = self.raiden_api.close(
channel.token_address,
channel.partner_address
)
return self.channel_schema.dumps(channel_to_api_dict(raiden_service_result))
elif state == CHANNEL_STATE_SETTLED:
if current_state == CHANNEL_STATE_SETTLED or current_state == CHANNEL_STATE_OPENED:
return make_response(
'Attempted to settle a channel at its {} state'.format(current_state),
httplib.CONFLICT,
)
raiden_service_result = self.raiden_api.settle(
channel.token_address,
channel.partner_address
if state == CHANNEL_STATE_CLOSED:
if current_state != CHANNEL_STATE_OPENED:
return make_response(
httplib.CONFLICT,
'Attempted to close an already closed channel'
)
return self.channel_schema.dumps(channel_to_api_dict(raiden_service_result))
else:
# should never happen, channel_state is validated in the schema
raiden_service_result = self.raiden_api.close(
channel.token_address,
channel.partner_address
)
result = self.channel_schema.dump(channel_to_api_dict(raiden_service_result))
return jsonify(result.data)

if state == CHANNEL_STATE_SETTLED:
if current_state == CHANNEL_STATE_SETTLED or current_state == CHANNEL_STATE_OPENED:
return make_response(
'Provided invalid channel state {}'.format(state),
httplib.BAD_REQUEST,
'Attempted to settle a channel at its {} state'.format(current_state),
httplib.CONFLICT,
)
raiden_service_result = self.raiden_api.settle(
channel.token_address,
channel.partner_address
)
result = self.channel_schema.dump(channel_to_api_dict(raiden_service_result))
return jsonify(result.data)

# should never happen, channel_state is validated in the schema
return make_response(
'Provided invalid channel state {}'.format(state),
httplib.BAD_REQUEST,
)

def token_swap(
self,
Expand Down
2 changes: 1 addition & 1 deletion raiden/tests/utils/apitestcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def settled(self):


def decode_response(response):
return json.loads(json.loads(response._content))
return json.loads(response._content)


class ApiTestContext():
Expand Down

0 comments on commit 1210134

Please sign in to comment.