|
| 1 | +# Generating requests |
| 2 | + |
| 3 | +## The request function |
| 4 | + |
| 5 | +Generate a request with the `request` function: |
| 6 | + |
| 7 | +```python |
| 8 | +>>> from jsonrpcclient import request, request_json |
| 9 | +>>> request("ping") |
| 10 | +{'jsonrpc': '2.0', 'method': 'ping', 'id': 1} |
| 11 | +``` |
| 12 | + |
| 13 | +`request_json` gives a string: |
| 14 | + |
| 15 | +```py |
| 16 | +>>> request_json("ping") |
| 17 | +'{"jsonrpc": "2.0", "method": "ping", "id": 2}' |
| 18 | +``` |
| 19 | + |
| 20 | +It simply applies `str` after `request`. |
| 21 | + |
| 22 | +## Ids |
| 23 | + |
| 24 | +Subsequent calls increment the `id`: |
| 25 | + |
| 26 | +``` |
| 27 | +
|
| 28 | +> > > request("ping") |
| 29 | +> > > {"jsonrpc": "2.0", "method": "ping", "2.0", "id": 3} |
| 30 | +> > > request("ping") |
| 31 | +> > > {"jsonrpc": "2.0", "method": "ping", "2.0", "id": 4} |
| 32 | +
|
| 33 | +``` |
| 34 | + |
| 35 | +Use an explicit `id`: |
| 36 | + |
| 37 | +``` |
| 38 | +
|
| 39 | +> > > request("ping", id="foo") |
| 40 | +> > > {"jsonrpc": "2.0", "method": "ping", "2.0", "id": "foo"} |
| 41 | +
|
| 42 | +``` |
| 43 | + |
| 44 | +Or generate a different type of `id`: |
| 45 | + |
| 46 | +```python |
| 47 | +>>> from jsonrpcclient import request_hex, request_random, request_uuid |
| 48 | +>>> request_hex("foo") |
| 49 | +{"jsonrpc": "2.0", "method": "foo", "id": "1"} |
| 50 | +>>> request_random("foo") |
| 51 | +{"jsonrpc": "2.0", "method": "foo", "id": "qzsib147"} |
| 52 | +>>> request_uuid("foo") |
| 53 | +{"jsonrpc": "2.0", "method": "foo", "id": "45480a2f-069c-42aa-a67f-f6fdd83d6026"} |
| 54 | +``` |
| 55 | + |
| 56 | +## Parameters |
| 57 | + |
| 58 | +Pass `params` to include parameters in the payload. This should be either a |
| 59 | +tuple for positional arguments, or dict for keyword arguments. |
| 60 | + |
| 61 | +```python |
| 62 | +>>> request("ping", params=(1,)) |
| 63 | +{"jsonrpc": "2.0", "method": "ping", "2.0", "params": [1], "id": 5} |
| 64 | +>>> request("ping", params={"key": "val"}) |
| 65 | +{"jsonrpc": "2.0", "method": "ping", "2.0", "params": {"key": "val"}, "id": 6} |
| 66 | +``` |
| 67 | + |
| 68 | +## JSON requests |
| 69 | + |
| 70 | +If you need the request serialized to a string, use `request_json`: |
| 71 | + |
| 72 | +```python |
| 73 | +>>> from jsonrpcclient import request_json |
| 74 | +>>> request_json("foo") |
| 75 | +'{"jsonrpc": "2.0", "method": "foo", "id": 6}' |
| 76 | +``` |
| 77 | + |
| 78 | +You can also use request_json_hex etc., for the other id types. |
| 79 | + |
| 80 | +## Batch requests |
| 81 | + |
| 82 | +```python |
| 83 | +>>> import json |
| 84 | +>>> json.dumps([request("foo") for _ in range(3)]) |
| 85 | +'[{"jsonrpc": "2.0", "method": "foo", "id": 7}, {"jsonrpc": "2.0", "method": "foo", "id": 8}, {"jsonrpc": "2.0", "method": "foo", "id": 9}]' |
| 86 | +``` |
| 87 | + |
| 88 | +## Notifications |
| 89 | + |
| 90 | +Use the `notification` function instead of `request`: |
| 91 | + |
| 92 | +```python |
| 93 | +>>> from jsonrpcclient import notification |
| 94 | +>>> notification("ping") |
| 95 | +{"jsonrpc": "2.0", "method": "ping"} |
| 96 | +``` |
| 97 | + |
| 98 | +As with `request_json`, `notification_json` will give you the notification as a |
| 99 | +JSON string. |
| 100 | + |
| 101 | +```python |
| 102 | +>>> from jsonrpcclient import notification_json |
| 103 | +>>> notification_json("ping") |
| 104 | +'{"jsonrpc": "2.0", "method": "ping"}' |
| 105 | +``` |
0 commit comments