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.Request(method, params=None, id=None)[source]#
Bases:
AbstractRequest
JSON-RPC version 2.0 request.
- Parameters:
method (str) – method name
params (Optional[JsonRpcParams]) – method parameters
id (Optional[JsonRpcRequestId]) – request identifier
- property params: Optional[Union[List[Any], Tuple[Any, ...], Dict[str, Any]]]#
Request method parameters.
- class pjrpc.common.Response(id, result=UNSET, error=UNSET)[source]#
Bases:
AbstractResponse
JSON-RPC version 2.0 response.
- Parameters:
id (Optional[JsonRpcRequestId]) – response identifier
result (MaybeSet[Any]) – response result
error (MaybeSet[JsonRpcError]) – response error
- classmethod from_json(json_data, error_cls=<class 'pjrpc.common.exceptions.JsonRpcError'>)[source]#
Deserializes a response from json data.
- Parameters:
- Returns:
response object
- Raises:
pjrpc.common.exceptions.DeserializationError
if format is incorrect- Return type:
- property result: Any#
Response result. If the response has not succeeded raises an exception deserialized from the error field.
- property error: Union[UnsetType, JsonRpcError]#
Response error. If the response has succeeded returns
pjrpc.common.common.UNSET
.
Returns the request related response object if the response has been received from the server otherwise returns
None
.
- class pjrpc.common.BatchRequest(*requests, strict=True)[source]#
Bases:
AbstractRequest
JSON-RPC 2.0 batch request.
- Parameters:
- append(request)[source]#
Appends a request to the batch.
- Parameters:
request (Request) –
- Return type:
None
- class pjrpc.common.BatchResponse(*responses, error=UNSET, strict=True)[source]#
Bases:
AbstractResponse
JSON-RPC 2.0 batch response.
- Parameters:
responses (Response) – responses to be added to the batch
strict (bool) – if
True
checks response identifier uniquenesserror (MaybeSet[JsonRpcError]) –
- classmethod from_json(json_data, error_cls=<class 'pjrpc.common.exceptions.JsonRpcError'>)[source]#
Deserializes a batch response from json data.
- property error: Union[UnsetType, JsonRpcError]#
Response error. If the response has succeeded returns
pjrpc.common.common.UNSET
.
- property is_error: bool#
Returns
True
if the request has not succeeded. Note that it is not the same aspjrpc.common.BatchResponse.has_error
. is_error indicates that the batch request failed at all, while has_error indicates that one of the requests in the batch failed.
- property result: Tuple[Any, ...]#
Returns the batch result as a tuple. If any response of the batch has an error raises an exception of the first errored response.
Returns the request related response object if the response has been received from the server otherwise returns
None
.
- append(response)[source]#
Appends a response to the batch.
- Parameters:
response (Response) –
- Return type:
None
- class pjrpc.common.UnsetType[source]#
Bases:
object
Sentinel object. Used to distinct unset (missing) values from
None
ones.
- 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:
JSONEncoder
Library 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 JSONEncoder.default(self, o)
- pjrpc.common.DEFAULT_CONTENT_TYPE = 'application/json'#
default JSON-RPC client/server content type
- pjrpc.common.REQUEST_CONTENT_TYPES = ('application/json', 'application/json-rpc', 'application/jsonrequest')#
allowed JSON-RPC server requests content types
- pjrpc.common.RESPONSE_CONTENT_TYPES = ('application/json', 'application/json-rpc')#
allowed JSON-RPC client responses content types
Types#
- pjrpc.common.typedefs.JsonRpcParams#
JSON-RPC params type
Exceptions#
Definition of package exceptions and JSON-RPC protocol errors.
- exception pjrpc.common.exceptions.BaseError[source]#
Bases:
Exception
Base package error. All package errors are inherited from it.
- exception pjrpc.common.exceptions.IdentityError[source]#
Bases:
BaseError
Raised when a batch requests/responses identifiers are not unique or missing.
- exception pjrpc.common.exceptions.DeserializationError[source]#
Bases:
BaseError
,ValueError
Request/response deserializatoin error. Raised when request/response json has incorrect format.
- class pjrpc.common.exceptions.JsonRpcErrorMeta(name, bases, dct)[source]#
Bases:
type
pjrpc.common.exceptions.JsonRpcError
metaclass. Builds a mapping from an error code number to an error class inherited from apjrpc.common.exceptions.JsonRpcError
.
- exception pjrpc.common.exceptions.JsonRpcError(code=None, message=None, data=UNSET)[source]#
Bases:
BaseError
JSON-RPC protocol error. For more information see Error object. All JSON-RPC protocol errors are inherited from it.
- Parameters:
- classmethod from_json(json_data)[source]#
Deserializes an error from json data. If data format is not correct
ValueError
is raised.
- exception pjrpc.common.exceptions.ClientError(code=None, message=None, data=UNSET)[source]#
Bases:
JsonRpcError
Raised when a client sent an incorrect request.
- exception pjrpc.common.exceptions.ParseError(code=None, message=None, data=UNSET)[source]#
Bases:
ClientError
Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
- exception pjrpc.common.exceptions.InvalidRequestError(code=None, message=None, data=UNSET)[source]#
Bases:
ClientError
The JSON sent is not a valid request object.
- exception pjrpc.common.exceptions.MethodNotFoundError(code=None, message=None, data=UNSET)[source]#
Bases:
ClientError
The method does not exist / is not available.
- exception pjrpc.common.exceptions.InvalidParamsError(code=None, message=None, data=UNSET)[source]#
Bases:
ClientError
Invalid method parameter(s).
- exception pjrpc.common.exceptions.InternalError(code=None, message=None, data=UNSET)[source]#
Bases:
JsonRpcError
Internal JSON-RPC error.
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.