예제 #1
0
 def _parse_post(req_class: T) -> T:
     json = request.get_json()
     if not json:
         raise exceptions.ApiException("JSON payload expected")
     try:
         return req_class.Schema().load(json)  # type: ignore
     except marshmallow.ValidationError as ex:
         raise exceptions.InvalidRequest(**ex.messages)
예제 #2
0
 def _parse_post(req_class: T) -> T:
     json = request.get_json()
     if not json:
         raise exceptions.ApiException("JSON payload expected")
     req, errors = req_class.Schema().load(json)  # type: ignore
     if errors:
         raise exceptions.InvalidRequest(**errors)
     return req
예제 #3
0
    def post(self, token_network_address: str):
        token_network_error = self._validate_token_network_argument(
            token_network_address)
        if token_network_error is not None:
            return token_network_error

        parser = reqparse.RequestParser()
        parser.add_argument('from',
                            type=str,
                            help='Payment initiator address.')
        parser.add_argument('to', type=str, help='Payment target address.')
        parser.add_argument('value', type=int, help='Maximum payment value.')
        parser.add_argument(
            'max_paths',
            type=int,
            help='Number of paths requested.',
            default=DEFAULT_MAX_PATHS,
        )

        args = parser.parse_args()
        error = self._validate_args(args)
        if error is not None:
            return error

        json = request.get_json()
        if not json:
            raise exceptions.ApiException('JSON payload expected')
        process_payment(json.get('iou'), self.pathfinding_service)

        token_network = self.pathfinding_service.token_networks.get(
            Address(token_network_address), )
        # Existence is checked in _validate_token_network_argument
        assert token_network, 'Requested token network cannot be found'

        try:
            paths = token_network.get_paths(
                source=args['from'],
                target=args['to'],
                value=args.value,
                max_paths=args.max_paths,
            )
        except NetworkXNoPath:
            return {
                'errors':
                'No suitable path found for transfer from {} to {}.'.format(
                    args['from'],
                    args['to'],
                )
            }, 400

        return {'result': paths}, 200
예제 #4
0
    def post(self, token_network_address: str) -> Tuple[dict, int]:
        token_network_error = self._validate_token_network_argument(
            token_network_address)
        if token_network_error is not None:
            return token_network_error

        json = request.get_json()
        if not json:
            raise exceptions.ApiException('JSON payload expected')
        path_req, errors = PathRequest.Schema().load(json)
        if errors:
            raise exceptions.InvalidRequest(**errors)
        process_payment(path_req.iou, self.pathfinding_service)

        token_network = self.pathfinding_service.token_networks.get(
            TokenNetworkAddress(token_network_address))
        # Existence is checked in _validate_token_network_argument
        assert token_network, 'Requested token network cannot be found'

        try:
            # only optional args if not None, so we can use defaults
            optional_args = {}
            for arg in ['diversity_penalty', 'fee_penalty']:
                value = getattr(path_req, arg)
                if value is not None:
                    optional_args[arg] = value

            paths = token_network.get_paths(
                source=path_req.from_,
                target=path_req.to,
                value=path_req.value,
                max_paths=path_req.max_paths,
                **optional_args,
            )
        except (NetworkXNoPath, NodeNotFound):
            return (
                {
                    'errors':
                    'No suitable path found for transfer from {} to {}.'.
                    format(path_req.from_, path_req.to)
                },
                400,
            )

        return {'result': paths}, 200