Modules¶
Errors¶
Error classes for typedjsonrpc.
-
exception
typedjsonrpc.errors.
InternalError
(data=None)[source]¶ Internal JSON-RPC error.
-
static
from_error
(exc, debug_url=None)[source]¶ Wraps another Exception in an InternalError.
Return type: InternalError
-
static
-
exception
typedjsonrpc.errors.
InvalidRequestError
(data=None)[source]¶ The JSON sent is not a valid request object.
-
exception
typedjsonrpc.errors.
InvalidReturnTypeError
(data=None)[source]¶ Return type does not match expected type.
Method Info¶
Data structures for wrapping methods and information about them.
-
class
typedjsonrpc.method_info.
MethodInfo
[source]¶ An object wrapping a method and information about it.
Attribute name: Name of the function Attribute method: The function being described Attribute signature: A description of the types this method takes as parameters and returns -
description
¶ Returns the docstring for this method.
Return type: str
-
params
¶ The parameters for this method in a JSON-compatible format
Return type: list[dict[str, str]]
-
returns
¶ The return type for this method in a JSON-compatible format.
This handles the special case of
None
which allowstype(None)
also.Return type: str or None
-
Parameter Checker¶
Logic for checking parameter declarations and parameter types.
-
typedjsonrpc.parameter_checker.
check_return_type
(value, expected_type)[source]¶ Checks that the given return value has the correct type.
Parameters: - value (any) – Value returned by the method
- expected_type (type) – Expected return type
-
typedjsonrpc.parameter_checker.
check_type_declaration
(parameter_names, parameter_types)[source]¶ Checks that exactly the given parameter names have declared types.
Parameters: - parameter_names (list[str]) – The names of the parameters in the method declaration
- parameter_types (dict[str, type]) – Parameter type by name
-
typedjsonrpc.parameter_checker.
check_types
(parameters, parameter_types)[source]¶ Checks that the given parameters have the correct types.
Parameters: - parameters (dict[str, object]) – List of (name, value) pairs of the given parameters
- parameter_types (dict[str,type]) – Parameter type by name.
-
typedjsonrpc.parameter_checker.
validate_params_match
(method, parameters)[source]¶ Validates that the given parameters are exactly the method’s declared parameters.
Parameters: - method (function) – The method to be called
- parameters (dict[str, object] | list[object]) – The parameters to use in the call
Registry¶
Logic for storing and calling jsonrpc methods.
-
class
typedjsonrpc.registry.
Registry
(debug=False)[source]¶ The registry for storing and calling jsonrpc methods.
Attribute debug: Debug option which enables recording of tracebacks Attribute tracebacks: Tracebacks for debugging -
describe
()[source]¶ Returns a description of all the methods in the registry.
Returns: Description Return type: dict[str, object]
-
dispatch
(request)[source]¶ Takes a request and dispatches its data to a jsonrpc method.
Parameters: request (werkzeug.wrappers.Request) – a werkzeug request with json data Returns: json output of the corresponding method Return type: str
-
json_decoder
¶ The JSON decoder class to use. Defaults to
json.JSONDecoder
alias of
JSONDecoder
-
json_encoder
¶ The JSON encoder class to use. Defaults to
json.JSONEncoder
alias of
JSONEncoder
-
method
(returns, **parameter_types)[source]¶ Syntactic sugar for registering a method
Example:
>>> registry = Registry() >>> @registry.method(returns=int, x=int, y=int) ... def add(x, y): ... return x + y
Parameters: - returns (type) – The method’s return type
- parameter_types (dict[str,type]) – The types of the method’s parameters
-
register
(name, method, method_signature=None)[source]¶ Registers a method with a given name and signature.
Parameters: - name (str) – The name used to register the method
- method (function) – The method to register
- method_signature (MethodSignature or None) – The method signature for the given function
-
Server¶
Contains the Werkzeug server for debugging and WSGI compatibility.
-
class
typedjsonrpc.server.
Server
(registry, endpoint='/api')[source]¶ A basic WSGI-compatible server for typedjsonrpc endpoints.
Attribute registry: The registry for this server
-
class
typedjsonrpc.server.
DebuggedJsonRpcApplication
(app, **kwargs)[source]¶ A JSON-RPC-specific debugged application.
This differs from DebuggedApplication since the normal debugger assumes you are hitting the endpoint from a web browser.
A returned response will be JSON of the form: {“traceback_id”: <id>} which you can use to hit the endpoint http://<host>:<port>/debug/<traceback_id>.
NOTE: This should never be used in production because the user gets shell access in debug mode.
-
debug_application
(environ, start_response)[source]¶ Run the application and preserve the traceback frames.
Parameters: - environ (dict[str, object]) – The environment which is passed into the wsgi application
- start_response ((str, list[(str, str)]) -> None) – The start_response function of the wsgi application
Return type: generator[str]
-
handle_debug
(environ, start_response, traceback_id)[source]¶ Handles the debug endpoint for inspecting previous errors.
Parameters: - environ (dict[str, object]) – The environment which is passed into the wsgi application
- start_response ((str, list[(str, str)]) -> NoneType) – The start_response function of the wsgi application
- traceback_id (int) – The id of the traceback to inspect
-