Exemple #1
0
 def test_translate_app_base(self):
     """
     ## With empty dict params
     """
     js = '{"id": 85, "jsonrpc": "2.0", "method": "database_api.get_version", "params": {}}'
     js_data = json.loads(js)
     req = parse_request(js_data)
     expected = "{'id': 85, 'jsonrpc': '2.0', 'method': 'call', 'params': ['condenser_api', 'get_version', []]}"
     self.assertEqual(str(translate_to_app_base(req).data), expected)
     """
     ## No params provided
     """
     js = '{"id": 85, "jsonrpc": "2.0", "method": "database_api.get_version"}'
     js_data = json.loads(js)
     req = parse_request(js_data)
     expected = "{'id': 85, 'jsonrpc': '2.0', 'method': 'call', 'params': ['condenser_api', 'get_version', []]}"
     self.assertEqual(str(translate_to_app_base(req).data), expected)
     """
     ## Params provided with list
     """
     js = '{"id": 85, "jsonrpc": "2.0", "method": "condenser_api.get_following", "params":["hiveio",null,"blog",10]}'
     js_data = json.loads(js)
     req = parse_request(js_data)
     expected = "{'id': 85, 'jsonrpc': '2.0', 'method': 'call', 'params': ['condenser_api', 'get_following', ['hiveio', None, 'blog', 10]]}"
     self.assertEqual(str(translate_to_app_base(req).data), expected)
     """
     ## Params provided with dict
     """
     js = '{"id": 85, "jsonrpc": "2.0", "method": "condenser_api.get_following", "params": {"foo":"bar"}}'
     js_data = json.loads(js)
     req = parse_request(js_data)
     expected = "{'id': 85, 'jsonrpc': '2.0', 'method': 'call', 'params': ['condenser_api', 'get_following', ['bar']]}"
     self.assertEqual(str(translate_to_app_base(req).data), expected)
Exemple #2
0
async def tunnel(data: Optional[Dict]):
    request = parse_request(data)

    if request is None:
        return error_response({"error": "Not a valid json request"}, 406)

    path = "{}.{}".format(request.api, request.method)

    route = route_match(config_get("routes"), path)

    if route is None:
        return error_response({"error": "No route has matched: '{}'".format(path)}, 406)

    route_config = config_get("routes", route)

    if "translate_to_app_base" in route_config:
        request = translate_to_app_base(request)

    target_name = route_config["target"]

    payload = json.dumps(request.data, ensure_ascii=False).encode('utf8')

    try:
        target: str = config_get("targets", target_name)
    except NoSuchConfigException:
        return error_response({"error": "Not a valid target: {}".format(target_name)}, 406)

    cache_timeout: int = config_get("default-cache")
    if "cache" in route_config:
        cache_timeout = route_config["cache"]

    cache_key = "{}_{}".format(path, hash(str(request.params)))

    if cache_timeout > 0:
        resp = await cache_get(cache_key)
        if resp is not None:
            return success_response(resp, True, target_name, path, route)

    timeout = config_get_timeout(target_name)

    if re.match(HTTP_RE, target):
        fn = http_tunnel
    elif re.match(WS_RE, target):
        fn = ws_tunnel
    elif re.match(SOCK_RE, target):
        fn = sock_tunnel
    else:
        return error_response({"error": "Not a valid scheme: {}".format(target)}, 406)

    try:
        resp = await fn(*(target, payload, timeout))
    except BaseException as ex:
        return error_response({"error": str(ex)}, 500)

    if LOG_ERRORS and "error" in resp:
        logger.error("Query failed: {} - {}".format(request.data, json.dumps(resp)))

    if "error" not in resp and cache_timeout > 0:
        await cache_set(cache_key, resp, cache_timeout)

    return success_response(resp, False, target_name, path, route)
Exemple #3
0
 def test_parse_request_02(self):
     # should read api and method from "params"
     js = '{"jsonrpc":"2.0", "method":"call", "params": ["condenser_api", "get_follow_count", ["alice"]], "id":1}'
     js_data = json.loads(js)
     expected = "rpc_ver: 2.0, api: condenser_api, method: get_follow_count, params: ['alice'], id:1"
     self.assertEqual(str(parse_request(js_data)), expected)
Exemple #4
0
 def test_parse_request_03(self):
     # should create empty "params" field if not provided
     js = '{"jsonrpc":"2.0", "method":"database_api.get_dynamic_global_properties", "id":1}'
     js_data = json.loads(js)
     expected = "rpc_ver: 2.0, api: database_api, method: get_dynamic_global_properties, params: [], id:1"
     self.assertEqual(str(parse_request(js_data)), expected)
Exemple #5
0
 def test_parse_request_01(self):
     # should read api and method from "method"
     js = '{"jsonrpc":"2.0", "method":"bridge.get_ranked_posts", "params":{"sort":"trending","tag":"","observer":"alice"}, "id":1}'
     js_data = json.loads(js)
     expected = "rpc_ver: 2.0, api: bridge, method: get_ranked_posts, params: {'sort': 'trending', 'tag': '', 'observer': 'alice'}, id:1"
     self.assertEqual(str(parse_request(js_data)), expected)