Skip to content
Snippets Groups Projects
Commit 41c2b71f authored by Michał Kudela's avatar Michał Kudela
Browse files

Refactor `home` function: move logic josn input validation into new funciton...

Refactor `home` function: move logic josn input validation into new funciton -> `check_general_json_format`
parent 340e2535
No related branches found
No related tags found
2 merge requests!827Merge develop changes to master,!717Rewrite python function to sql ( condenser_api )
......@@ -740,6 +740,7 @@ def setup_runtime_code(db):
"postgrest/bridge_api/bridge_api_get_community_context.sql",
"postgrest/utilities/dispatch.sql",
"postgrest/utilities/get_api_method.sql",
"postgrest/utilities/check_general_json_format.sql",
]
sql_scripts_dir_path = Path(__file__).parent / 'sql_scripts'
......
......@@ -12,11 +12,7 @@ DECLARE
__params JSON;
__id JSON;
__result JSON;
__api_type TEXT;
__method_type TEXT;
__json_with_params_is_object BOOLEAN;
__method_is_call BOOLEAN;
__result JSONB;
__exception_message TEXT;
__exception JSONB;
BEGIN
......@@ -25,45 +21,19 @@ BEGIN
__params = (__request_data->'params');
__id = (__request_data->'id');
SELECT NULL::JSON INTO __result;
IF __jsonrpc != '2.0' OR __jsonrpc IS NULL OR __params IS NULL OR __id IS NULL OR __method IS NULL THEN
RAISE EXCEPTION '%', hivemind_postgrest_utilities.raise_invalid_json_format_exception('Invalid JSON-RPC');
END IF;
if lower(__method) = 'call' THEN
if json_array_length(__params) < 2 THEN
RAISE EXCEPTION '%', hivemind_postgrest_utilities.raise_invalid_json_format_exception('Invalid JSON-RPC');
END IF;
__api_type = __params->>0;
__method_type = __params->>1;
__params = __params->>2;
__json_with_params_is_object = False;
__method_is_call = True;
ELSE
SELECT substring(__method FROM '^[^.]+') INTO __api_type;
SELECT substring(__method FROM '[^.]+$') INTO __method_type;
__method_is_call = False;
IF json_typeof(__params) = 'object' THEN
__json_with_params_is_object = True;
ELSEIF json_typeof(__params) = 'array' THEN
IF json_array_length(__params) <> 0 THEN
__json_with_params_is_object = False;
ELSE
__json_with_params_is_object = True;
END IF;
ELSE
RAISE EXCEPTION '%', hivemind_postgrest_utilities.raise_invalid_json_format_exception('Invalid JSON format:' || json_typeof(__params)::text);
END IF;
END IF;
__result := hivemind_postgrest_utilities.dispatch(__api_type, __method_type, __json_with_params_is_object, __method_is_call, __params);
SELECT hivemind_postgrest_utilities.check_general_json_format(__jsonrpc, __method, __params, __id) INTO __result;
RETURN jsonb_build_object(
'jsonrpc', '2.0',
'result', __result,
'id', __id
);
'jsonrpc', '2.0',
'id', __id,
'result', hivemind_postgrest_utilities.dispatch(
__result->>'api_type'::TEXT,
__result->>'method_type'::TEXT,
(__result->>'json_with_params_is_object')::BOOLEAN,
(__result->>'method_is_call')::BOOLEAN,
(__result->>'params')::JSON
)
);
EXCEPTION
WHEN raise_exception THEN
......
DROP FUNCTION IF EXISTS hivemind_postgrest_utilities.check_general_json_format;
CREATE FUNCTION hivemind_postgrest_utilities.check_general_json_format(
IN __jsonrpc TEXT,
IN __method TEXT,
IN __params JSON,
IN __id JSON
) RETURNS JSONB
LANGUAGE 'plpgsql'
STABLE
AS
$$
DECLARE
__api_type TEXT;
__method_type TEXT;
__json_with_params_is_object BOOLEAN;
__method_is_call BOOLEAN;
BEGIN
IF __jsonrpc != '2.0' OR __jsonrpc IS NULL OR __params IS NULL OR __id IS NULL OR __method IS NULL THEN
RAISE EXCEPTION '%', hivemind_postgrest_utilities.raise_invalid_json_format_exception('Invalid JSON-RPC');
END IF;
if lower(__method) = 'call' THEN
if json_array_length(__params) < 2 THEN
RAISE EXCEPTION '%', hivemind_postgrest_utilities.raise_invalid_json_format_exception('Invalid JSON-RPC');
END IF;
__api_type = __params->>0;
__method_type = __params->>1;
__params = __params->>2;
__json_with_params_is_object = False;
__method_is_call = True;
ELSE
SELECT substring(__method FROM '^[^.]+') INTO __api_type;
SELECT substring(__method FROM '[^.]+$') INTO __method_type;
__method_is_call = False;
IF json_typeof(__params) = 'object' THEN
__json_with_params_is_object = True;
ELSEIF json_typeof(__params) = 'array' THEN
IF json_array_length(__params) <> 0 THEN
__json_with_params_is_object = False;
ELSE
__json_with_params_is_object = True;
END IF;
ELSE
RAISE EXCEPTION '%', hivemind_postgrest_utilities.raise_invalid_json_format_exception('Invalid JSON format:' || json_typeof(__params)::text);
END IF;
END IF;
RETURN jsonb_build_object(
'api_type', __api_type,
'method_type', __method_type,
'params', __params,
'json_with_params_is_object', __json_with_params_is_object,
'method_is_call', __method_is_call
);
END
$$
;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment