示例#1
0
def post_pfs_paths(
        url: str, token_network_address: TokenNetworkAddress,
        payload: Dict[str, Any]) -> Tuple[List[Dict[str, Any]], UUID]:
    try:
        response = requests.post(
            f"{url}/api/v1/{to_checksum_address(token_network_address)}/paths",
            json=payload,
            timeout=DEFAULT_HTTP_REQUEST_TIMEOUT,
        )
    except requests.RequestException as e:
        raise ServiceRequestFailed(
            f"Could not connect to Pathfinding Service ({str(e)})",
            dict(parameters=payload, exc_info=True),
        )

    if response.status_code != 200:
        info = {"http_error": response.status_code}
        try:
            response_json = get_response_json(response)
        except ValueError:
            log.error(
                "Pathfinding Service returned error code (malformed json in response)",
                response=response,
            )
            raise ServiceRequestFailed(
                "Pathfinding Service returned error code (malformed json in response)",
                info)

        error = info["error"] = response_json.get("errors")
        error_code = info["error_code"] = response_json.get("error_code", 0)

        if PFSError.is_iou_rejected(error_code):
            raise ServiceRequestIOURejected(error, error_code)

        raise ServiceRequestFailed(error, info)

    try:
        response_json = get_response_json(response)
        return response_json["result"], UUID(response_json["feedback_token"])
    except KeyError:
        raise ServiceRequestFailed(
            "Answer from Pathfinding Service not understood ('result' field missing)",
            dict(response=get_response_json(response)),
        )
    except ValueError:
        raise ServiceRequestFailed(
            "Pathfinding Service returned invalid json",
            dict(response_text=response.text, exc_info=True),
        )
示例#2
0
def get_pfs_info(url: str) -> PFSInfo:
    try:
        response = requests.get(f"{url}/api/v1/info",
                                timeout=DEFAULT_HTTP_REQUEST_TIMEOUT)
        infos = get_response_json(response)

        return PFSInfo(
            url=url,
            price=infos["price_info"],
            chain_id=infos["network_info"]["chain_id"],
            token_network_registry_address=TokenNetworkRegistryAddress(
                to_canonical_address(
                    infos["network_info"]["token_network_registry_address"])),
            user_deposit_address=Address(
                to_canonical_address(
                    infos["network_info"]["user_deposit_address"])),
            payment_address=to_canonical_address(infos["payment_address"]),
            message=infos["message"],
            operator=infos["operator"],
            version=infos["version"],
        )
    except requests.exceptions.RequestException as e:
        msg = "Selected Pathfinding Service did not respond"
        raise ServiceRequestFailed(msg) from e
    except (json.JSONDecodeError, requests.exceptions.RequestException,
            KeyError) as e:
        msg = "Selected Pathfinding Service returned unexpected reply"
        raise ServiceRequestFailed(msg) from e
示例#3
0
def get_pfs_info(url: str) -> PFSInfo:
    try:
        response = session.get(f"{url}/api/v1/info")
        infos = get_response_json(response)
        matrix_server_info = urlparse(infos["matrix_server"])

        return PFSInfo(
            url=url,
            price=infos["price_info"],
            chain_id=infos["network_info"]["chain_id"],
            token_network_registry_address=TokenNetworkRegistryAddress(
                to_canonical_address(infos["network_info"]["token_network_registry_address"])
            ),
            user_deposit_address=Address(
                to_canonical_address(infos["network_info"]["user_deposit_address"])
            ),
            payment_address=to_canonical_address(infos["payment_address"]),
            message=infos["message"],
            operator=infos["operator"],
            version=infos["version"],
            confirmed_block_number=infos["network_info"]["confirmed_block"]["number"],
            matrix_server=matrix_server_info.netloc,
            matrix_room_id=infos.get("matrix_room_id"),
        )
    except RequestException as e:
        msg = "Selected Pathfinding Service did not respond"
        raise ServiceRequestFailed(msg) from e
    except (json.JSONDecodeError, KeyError, ValueError) as e:
        msg = "Selected Pathfinding Service returned unexpected reply"
        raise ServiceRequestFailed(msg) from e
示例#4
0
def post_pfs_paths(
    url: str, token_network_address: TokenNetworkAddress, payload: Dict[str, Any]
) -> Tuple[List[Dict[str, Any]], UUID]:
    try:
        response = session.post(
            f"{url}/api/v1/{to_checksum_address(token_network_address)}/paths", json=payload
        )
    except RequestException as e:
        raise ServiceRequestFailed(
            f"Could not connect to Pathfinding Service ({str(e)})",
            dict(parameters=payload, exc_info=True),
        )

    if response.status_code != 200:
        try:
            response_json = get_response_json(response)
        except ValueError:
            log.error(
                "Pathfinding Service returned error code (malformed json in response)",
                response=response,
            )
            raise ServiceRequestFailed(
                "Pathfinding Service returned error code (malformed json in response)",
                {"http_error": response.status_code},
            )

        raise PFSReturnedError.from_response(response_json)

    try:
        response_json = get_response_json(response)
        return response_json["result"], UUID(response_json["feedback_token"])
    except KeyError:
        raise ServiceRequestFailed(
            "Answer from Pathfinding Service not understood ('result' field missing)",
            dict(response=get_response_json(response)),
        )
    except ValueError:
        raise ServiceRequestFailed(
            "Pathfinding Service returned invalid json",
            dict(response_text=response.text, exc_info=True),
        )
示例#5
0
def get_pfs_info(url: str) -> PFSInfo:
    try:
        response = requests.get(f"{url}/api/v1/info",
                                timeout=DEFAULT_HTTP_REQUEST_TIMEOUT)
        infos = get_response_json(response)

        return PFSInfo(
            url=url,
            price=infos["price_info"],
            chain_id=infos["network_info"]["chain_id"],
            token_network_registry_address=TokenNetworkRegistryAddress(
                to_canonical_address(
                    infos["network_info"]["registry_address"])),
            payment_address=to_canonical_address(infos["payment_address"]),
            message=infos["message"],
            operator=infos["operator"],
            version=infos["version"],
        )
    except (json.JSONDecodeError, requests.exceptions.RequestException,
            KeyError) as e:
        raise ServiceRequestFailed(str(e)) from e