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

Add JSON RPC Response parsing #1

Merged
merged 4 commits into from
Aug 29, 2018
Merged

Add JSON RPC Response parsing #1

merged 4 commits into from
Aug 29, 2018

Conversation

kacper-ka
Copy link
Owner

Requirements

  • Filling out the template is required. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.
  • All new code requires tests to ensure against regressions.
  • If pull request breaks tests it would not be merged.

Description of the Change

This PR adds methods needed for parsing JSON RPC responses from a received JSON string. In order to accomplish that the following changes have been made:

  • new exception class: JSONRPCInvalidResponseException (just like JSONRPCInvalidRequestException);
  • new class: JSONRPCResponse - JSON RPC version agnostic parser, based on JSONRPCRequest. It exposes two class methods: from_json and from_data - the former just deserializes JSON string and the latter relays parsing to version-specific class. The mechanism is the exact copy of the one used in JSONRPCRequest;
  • updated JSONRPC10Response and JSONRPC20Response classes:
    • updated error setters - verify whether result field is None prior to assigning the error object (raise ValueError if it is not), delete result key in case of JSONRPC20Response (result and error fields are mutually exclusive), attempt to construct error object before assigning the value;
    • added result and error deleters to JSONRPC20Response;
    • added from_json and from_data class methods - the mechanism reflects the one found in JSONRPC10Request and JSONRPC20Request.

To ensure proper implementation new tests have been added:

  • test_jsonrpc1.py:
    • test_from_json_invalid_response_result - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with missing result field;
    • test_from_json_invalid_response_error - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with missing error field;
    • test_from_json_invalid_response_id - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with missing id field;
    • test_from_json_invalid_response_both_result_and_error - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with both result and error fields other than None;
    • test_from_json_invalid_response_extra_data - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with extra fields;
    • test_from_json_response_result - tests whether parsing is correct for a specified result field;
    • test_from_json_response_error - tests whether parsing is correct for a specified error field
    • test_from_json_string_not_dict - tests whether the parser raises a ValueError if supplied JSON string does not represent a dictionary.
  • test_jsonrpc2.py:
    • test_from_json_invalid_response_jsonrpc - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with missing jsonrpc field;
    • test_from_json_invalid_response_id - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with missing id field;
    • test_from_json_invalid_response_no_result_error - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with neither result nor error fields specified;
    • test_from_json_invalid_response_result_and_error - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with both result and error fields specified;
    • test_from_json_invalid_response_extra_data - tests whether the parser raises an JSONRPCInvalidResponseException if supplied with JSON string with extra fields;
    • test_from_json_response_result_null - tests whether the parsing is correct for a None as a result;
    • test_from_json_response_result - tests whether the parsing is correct for a list as a result;
    • test_from_json_response_error - tests whether the parsing is correct for an error response.

Benefits

This change provides a symmetrical interface for both JSONRPCRequest and JSONRPCResponse. This provides an easy and consistent way to implement symmetrical interfaces, in which either side of an connection acts as a server-client hybrid, e.g. server that sends notifications to connected clients.

Possible Drawbacks

Should be none as all tests are passing.

Kacper Kubkowski and others added 4 commits August 29, 2018 09:37
+ jsonrpc/exceptions.py: add JSONRPCInvalidResponseException class;
+ jsonrpc/jsonrpc.py: add JSONRPCResponse class;
* jsonrpc/jsonrpc1.py: update JSONRPC10Response.error setter, add
  from_json and from_data class methods;
* jsonrpc/jsonrpc2.py: add JSONRPC20Response.result and error deleters,
  update error setter, add from_json and from_data class methods;
+ jsonrpc/tests/test_jsonrpc1.py, jsonrpc/tests/test_jsonrpc2.py: add
  functions testing new features;
+ added self to AUTHORS.

Signed-off-by: Kacper Kubkowski <kkubkowski@gmail.com>
@kacper-ka
Copy link
Owner Author

Travis CI build fails, but the only failing configuration is Python 3.3, which seems to have issues at the base fork as well - not considered as a regression.

@kacper-ka kacper-ka merged commit ae0a7b9 into master Aug 29, 2018
@kacper-ka kacper-ka deleted the add-response-parser branch August 29, 2018 08:57
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

Successfully merging this pull request may close these issues.

1 participant