|
| 1 | +# Dispatch |
| 2 | + |
| 3 | +The `dispatch` function processes a JSON-RPC request, attempting to call the method(s) |
| 4 | +and gives a JSON-RPC response. |
| 5 | + |
| 6 | +```python |
| 7 | +>>> dispatch('{"jsonrpc": "2.0", "method": "ping", "id": 1}') |
| 8 | +'{"jsonrpc": "2.0", "result": "pong", "id": 1}' |
| 9 | +``` |
| 10 | + |
| 11 | +It's a pure function; it will always give you a JSON-RPC response. No exceptions will be |
| 12 | +raised. |
| 13 | + |
| 14 | +[See how dispatch is used in different frameworks.](examples) |
| 15 | + |
| 16 | +## Optional parameters |
| 17 | + |
| 18 | +The `dispatch` function takes a request as its argument, and also has some optional |
| 19 | +parameters that allow you to customise how it works. |
| 20 | + |
| 21 | +### methods |
| 22 | + |
| 23 | +This lets you specify the methods to dispatch to. It's an alternative to using |
| 24 | +the `@method` decorator. The value should be a dict mapping function names to |
| 25 | +functions. |
| 26 | + |
| 27 | +```python |
| 28 | +def ping(): |
| 29 | + return Ok("pong") |
| 30 | + |
| 31 | +dispatch(request, methods={"ping": ping}) |
| 32 | +``` |
| 33 | + |
| 34 | +Default is `global_methods`, which is an internal dict populated by the |
| 35 | +`@method` decorator. |
| 36 | + |
| 37 | +### context |
| 38 | + |
| 39 | +If specified, this will be the first argument to all methods. |
| 40 | + |
| 41 | +```python |
| 42 | +@method |
| 43 | +def greet(context, name): |
| 44 | + return Ok(f"Hello {context}") |
| 45 | + |
| 46 | +>>> dispatch('{"jsonrpc": "2.0", "method": "greet", "params": ["Beau"], "id": 1}', context="Beau") |
| 47 | +'{"jsonrpc": "2.0", "result": "Hello Beau", "id": 1}' |
| 48 | +``` |
| 49 | + |
| 50 | +### deserializer |
| 51 | + |
| 52 | +A function that parses the JSON request string. Default is `json.loads`. |
| 53 | + |
| 54 | +```python |
| 55 | +dispatch(request, deserializer=ujson.loads) |
| 56 | +``` |
| 57 | + |
| 58 | +### jsonrpc_validator |
| 59 | + |
| 60 | +A function that validates the request once the JSON string has been parsed. The |
| 61 | +function should raise an exception (any exception) if the request doesn't match |
| 62 | +the JSON-RPC spec (https://www.jsonrpc.org/specification). Default is |
| 63 | +`default_jsonrpc_validator` which uses Jsonschema to validate requests against |
| 64 | +a schema. |
| 65 | + |
| 66 | +To disable JSON-RPC validation, pass `jsonrpc_validator=lambda _: None`, which |
| 67 | +will improve performance because this validation takes around half the dispatch |
| 68 | +time. |
| 69 | + |
| 70 | +### args_validator |
| 71 | + |
| 72 | +A function that validates a request's parameters against the signature of the |
| 73 | +Python function that will be called for it. Note this should not validate the |
| 74 | +_values_ of the parameters, it should simply ensure the parameters match the |
| 75 | +Python function's signature. For reference, see the `validate_args` function in |
| 76 | +`dispatcher.py`, which is the default `args_validator`. |
| 77 | + |
| 78 | +### serializer |
| 79 | + |
| 80 | +A function that serializes the response string. Default is `json.dumps`. |
| 81 | + |
| 82 | +```python |
| 83 | +dispatch(request, serializer=ujson.dumps) |
| 84 | +``` |
0 commit comments