Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON-RPC support #59

Open
ppKrauss opened this issue May 1, 2018 · 0 comments
Open

JSON-RPC support #59

ppKrauss opened this issue May 1, 2018 · 0 comments

Comments

@ppKrauss
Copy link

ppKrauss commented May 1, 2018

There are some JSON-RPC support or a simple way to configure it? If not, it is a suggestion.


Example: supposing rds_json_format=normal and rds_json_root=result, it is near to a JSON-RPC response,

 {"result":[{"id":2,"name":null},{"id":3,"name":"bob"}]}

need only to add jsonrpc and id attributes,

 { "jsonrpc":2.0, "id":123, "result":[{"id":2,"name":null},{"id":3,"name":"bob"}]}

where id is the ID for control the AJAX promises or any other kind of request-ID, and jsonrpc only a standard way to say that the client-server dialog is a JSON-RPC 2.0 (REST-adapted) dialect.

Error response also need some minimal adaptations, and use as primary-error-code the HTTP status codes... It is a convention over configuration proposal.


PS: no "protocol conflict" here, JSON-RPC can be used in a context of REST standards. It is only a standarized way to encapsulate JSON, and can be used also internally as generic SQL command tool.

Exemple of snippet code for internal error handling

/**
 * Add attributes "code" and "message" in a JSONb object, or create a new one.
 */
CREATE or replace FUNCTION lib.resp_error_add(
  p_code float, -- the error code in the form general_httpStatus.detail_internalCode
  p_msg text DEFAULT NULL,  -- the error message
  p_data JSONb DEFAULT NULL   -- optional data for warnings like 200.X
) RETURNS JSONb AS $f$
  SELECT CASE
      WHEN p_data IS NULL OR jsonb_typeof(p_data)='null' THEN x
      ELSE jsonb_build_object('result',p_data) || x
    END
  FROM (SELECT jsonb_build_object('code',p_code, 'message',p_msg)) t(x)
$f$ language SQL IMMUTABLE;

CREATE or replace FUNCTION lib.resp_error_add(
  int, text, JSONb DEFAULT NULL
) RETURNS JSONb AS $wrap$
  SELECT lib.resp_error_add((200.0+$1/1000.0)::float, $2, $3);
$wrap$ language SQL IMMUTABLE;

So, another interesting (optional) adaptation is to get the integer part of a float error code, or check that it is to by-pass as status 200.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant