Add JSON RPC response parsing#99
Open
kacper-ka wants to merge 6 commits into
Open
Conversation
+ 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>
Add JSON RPC Response parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirements
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:
JSONRPCInvalidResponseException(just likeJSONRPCInvalidRequestException);JSONRPCResponse- JSON RPC version agnostic parser, based onJSONRPCRequest. It exposes two class methods:from_jsonandfrom_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 inJSONRPCRequest;JSONRPC10ResponseandJSONRPC20Responseclasses:errorsetters - verify whetherresultfield is None prior to assigning the error object (raiseValueErrorif it is not), deleteresultkey in case ofJSONRPC20Response(resultanderrorfields are mutually exclusive), attempt to construct error object before assigning the value;resultanderrordeleters toJSONRPC20Response;from_jsonandfrom_dataclass methods - the mechanism reflects the one found inJSONRPC10RequestandJSONRPC20Request.To ensure proper implementation new tests have been added:
test_jsonrpc1.py:test_from_json_invalid_response_result- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with missingresultfield;test_from_json_invalid_response_error- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with missingerrorfield;test_from_json_invalid_response_id- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with missingidfield;test_from_json_invalid_response_both_result_and_error- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with bothresultanderrorfields other thanNone;test_from_json_invalid_response_extra_data- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with extra fields;test_from_json_response_result- tests whether parsing is correct for a specifiedresultfield;test_from_json_response_error- tests whether parsing is correct for a specifiederrorfieldtest_from_json_string_not_dict- tests whether the parser raises aValueErrorif supplied JSON string does not represent a dictionary.test_jsonrpc2.py:test_from_json_invalid_response_jsonrpc- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with missingjsonrpcfield;test_from_json_invalid_response_id- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with missingidfield;test_from_json_invalid_response_no_result_error- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with neitherresultnorerrorfields specified;test_from_json_invalid_response_result_and_error- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with bothresultanderrorfields specified;test_from_json_invalid_response_extra_data- tests whether the parser raises anJSONRPCInvalidResponseExceptionif supplied with JSON string with extra fields;test_from_json_response_result_null- tests whether the parsing is correct for aNoneas aresult;test_from_json_response_result- tests whether the parsing is correct for alistas aresult;test_from_json_response_error- tests whether the parsing is correct for an error response.Benefits
This change provides a symmetrical interface for both
JSONRPCRequestandJSONRPCResponse. 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. All tests are passing (except for the Python 3.3 environment, but that is an upstream issue), however it may be reasonable to closely inspect whether
error.setterupdate (inJSONRPC10ResponseandJSONRPC20Response) did not break anything.