Common#
Misc#
Client and server common functions, types and classes that implements JSON-RPC protocol itself and agnostic to any transport protocol layer (http, socket, amqp) and server-side implementation.
- class pjrpc.common.BatchRequest(*requests)[source]#
Bases:
AbstractRequestJSON-RPC 2.0 batch request.
- Parameters:
requests (tuple[pjrpc.common.request.Request, ...]) – requests to be added to the batch
- class pjrpc.common.BatchResponse(responses=UnsetType.UNSET, error=UnsetType.UNSET)[source]#
Bases:
AbstractResponseJSON-RPC 2.0 batch response.
- Parameters:
responses (Union[UnsetType, tuple[pjrpc.common.response.Response, ...]]) – responses to be added to the batch
error (Union[UnsetType, JsonRpcError]) –
- classmethod from_json(data, error_cls=<class 'pjrpc.common.exceptions.JsonRpcError'>, check_ids=True)[source]#
Deserializes a batch response from json data.
- Parameters:
data (Any) – data the response to be deserialized from
error_cls (type[pjrpc.common.exceptions.JsonRpcError]) – error class
check_ids (bool) – check response ids for uniqueness
- Returns:
batch response object
- Return type:
- property is_error: bool#
Returns
Trueif the request has not succeeded. Note that it is not the same aspjrpc.common.BatchResponse.has_error. is_error indicates that the whole batch request failed, whereas has_error indicates that one of the requests in the batch failed.
- unwrap_results()[source]#
Returns the batch result as a tuple. If any response of the batch has an error raises an exception related to the first errored response.
- Return type:
- pjrpc.common.DEFAULT_CONTENT_TYPE = 'application/json'#
default JSON-RPC client/server content type
- class pjrpc.common.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#
Bases:
JSONEncoderLibrary default JSON encoder. Encodes request, response and error objects to be json serializable. All custom encoders should be inherited from it.
- default(o)[source]#
Implement this method in a subclass such that it returns a serializable object for
o, or calls the base implementation (to raise aTypeError).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return super().default(o)
- exception pjrpc.common.JsonRpcError(code, message, data=UnsetType.UNSET)[source]#
Bases:
BaseErrorJSON-RPC protocol error. For more information see Error object. All JSON-RPC protocol errors are inherited from it.
- Parameters:
- Return type:
None
- classmethod from_json(json_data)[source]#
Deserializes an error from json data. If data format is not correct
ValueErroris raised.- Parameters:
json_data (Any) – json data the error to be deserialized from
- Returns:
deserialized error
- Raises:
pjrpc.common.exceptions.DeserializationErrorif format is incorrect- Return type:
- class pjrpc.common.Request(method, params=None, id=None)[source]#
Bases:
AbstractRequestJSON-RPC 2.0 request.
- Parameters:
method (str) – method name
params (Optional[Union[list[Union[str, int, float, bool, NoneType, list[Any], tuple[Any, ...], dict[str, Any]]], tuple[Union[str, int, float, bool, NoneType, list[Any], tuple[Any, ...], dict[str, Any]], ...], dict[str, Union[str, int, float, bool, NoneType, list[Any], tuple[Any, ...], dict[str, Any]]]]]) – method parameters
- classmethod from_json(data)[source]#
Deserializes a request from json data.
- Parameters:
data (Any) – data the request to be deserialized from
- Returns:
request object
- Raises:
pjrpc.common.exceptions.DeserializationErrorif format is incorrect- Return type:
- pjrpc.common.REQUEST_CONTENT_TYPES = ('application/json', 'application/json-rpc', 'application/jsonrequest')#
allowed JSON-RPC server requests content types
- class pjrpc.common.Response(id=None, result=UnsetType.UNSET, error=UnsetType.UNSET)[source]#
Bases:
AbstractResponseJSON-RPC 2.0 response.
- Parameters:
- classmethod from_json(data, error_cls=<class 'pjrpc.common.exceptions.JsonRpcError'>)[source]#
Deserializes a response from json data.
- Parameters:
data (Any) – data the response to be deserialized from
error_cls (type[pjrpc.common.exceptions.JsonRpcError]) – error class
- Returns:
response object
- Raises:
pjrpc.common.exceptions.DeserializationErrorif format is incorrect- Return type:
- pjrpc.common.RESPONSE_CONTENT_TYPES = ('application/json', 'application/json-rpc')#
allowed JSON-RPC client responses content types
Types#
- pjrpc.common.typedefs.JsonRpcParamsT#
JSON-RPC parameters type
alias of
Union[list[Union[str,int,float,bool,None,list[JsonT],tuple[JsonT, …],dict[str, JsonT]]],tuple[Union[str,int,float,bool,None,list[JsonT],tuple[JsonT, …],dict[str, JsonT]], …],dict[str,Union[str,int,float,bool,None,list[JsonT],tuple[JsonT, …],dict[str, JsonT]]]]
Exceptions#
Definition of package exceptions and JSON-RPC protocol errors.
- exception pjrpc.common.exceptions.BaseError[source]#
Bases:
ExceptionBase package error. All package errors are inherited from it.
- exception pjrpc.common.exceptions.ProtocolError[source]#
Bases:
BaseErrorRaised when JSON-RPC protocol is violated.
- exception pjrpc.common.exceptions.IdentityError[source]#
Bases:
ProtocolErrorRaised when a batch requests/responses identifiers are not unique or missing.
- exception pjrpc.common.exceptions.DeserializationError[source]#
Bases:
ProtocolError,ValueErrorRequest/response deserializatoin error. Raised when request/response json has incorrect format.
- exception pjrpc.common.exceptions.JsonRpcError(code, message, data=UnsetType.UNSET)[source]#
Bases:
BaseErrorJSON-RPC protocol error. For more information see Error object. All JSON-RPC protocol errors are inherited from it.
- Parameters:
- Return type:
None
- classmethod from_json(json_data)[source]#
Deserializes an error from json data. If data format is not correct
ValueErroris raised.- Parameters:
json_data (Any) – json data the error to be deserialized from
- Returns:
deserialized error
- Raises:
pjrpc.common.exceptions.DeserializationErrorif format is incorrect- Return type:
- class pjrpc.common.exceptions.ParseError[source]#
Bases:
objectInvalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
- class pjrpc.common.exceptions.InvalidRequestError[source]#
Bases:
objectThe JSON sent is not a valid request object.
- class pjrpc.common.exceptions.MethodNotFoundError[source]#
Bases:
objectThe method does not exist / is not available.
Identifier generators#
Builtin request id generators. Implements several identifier types and generation strategies.
- pjrpc.common.generators.sequential(start=1, step=1)[source]#
Sequential id generator. Returns consecutive values starting from start with step step.
- pjrpc.common.generators.randint(a, b)[source]#
Random integer id generator. Returns random integers between a and b.