Skip to content

Commit ce571a6

Browse files
committed
Initial commit from jsonrpcclient repo
0 parents  commit ce571a6

File tree

8 files changed

+275
-0
lines changed

8 files changed

+275
-0
lines changed

.github/workflows/deploy-docs.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ⚠️ This workflow deploys docs to explodinglabs.com
2+
# It is active only in the official repo. Forks won't trigger deployment.
3+
name: Build and Deploy MkDocs
4+
5+
on:
6+
push:
7+
branches:
8+
- main # or your main development branch
9+
10+
jobs:
11+
deploy:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout source repo
16+
uses: actions/checkout@v3
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: 3.x
22+
23+
- name: Install dependencies
24+
run: pip install mkdocs mkdocs-material
25+
26+
- name: Build MkDocs site
27+
run: mkdocs build
28+
29+
- name: Clone target repo
30+
run: |
31+
git clone https://x-access-token:${{ secrets.TARGET_REPO_PAT }}@github.com/explodinglabs/explodinglabs.com.git target-repo
32+
rm -rf target-repo/$(basename "$GITHUB_REPOSITORY")
33+
mkdir -p target-repo/$(basename "$GITHUB_REPOSITORY")
34+
cp -a site/. target-repo/$(basename "$GITHUB_REPOSITORY")/
35+
env:
36+
GIT_AUTHOR_NAME: GitHub Actions
37+
GIT_COMMITTER_NAME: GitHub Actions
38+
GIT_AUTHOR_EMAIL: [email protected]
39+
GIT_COMMITTER_EMAIL: [email protected]
40+
41+
- name: Commit and push
42+
run: |
43+
cd target-repo
44+
git config user.name "Exploding Labs Bot"
45+
git config user.email "[email protected]"
46+
git add .
47+
git commit -m "Update site from docs source repo" || echo "No changes to commit"
48+
git push

docs/examples.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Examples
2+
3+
Examples have moved to the [Community Wiki](https://github.com/explodinglabs/jsonrpcclient/wiki).

docs/faq.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# FAQ
2+
3+
## How to use a different json library?
4+
5+
The `request_json` function is simply `json.dumps` after `request`, and the
6+
`parse_json` function is simply `parse` after `json.loads`.
7+
8+
So here's how one could write their own, using a different json library (ujson
9+
here):
10+
11+
```python
12+
from jsonrpcclient import request, parse
13+
from jsonrpcclient.utils import compose
14+
import ujson
15+
16+
parse_json = compose(parse, ujson.loads)
17+
request_json = compose(ujson.dumps, request)
18+
```

docs/images/logo.png

19.2 KB
Loading

docs/index.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<style>
2+
.md-content__inner h1:first-of-type {
3+
display: none;
4+
}
5+
</style>
6+
7+
![jsonrpcclient](images/logo.png)
8+
9+
_Generate JSON-RPC requests and parse responses in Python._
10+
11+
Jump to:
12+
[GitHub](https://github.com/explodinglabs/jsonrpcclient) |
13+
[Community Wiki](https://github.com/explodinglabs/jsonrpcclient/wiki)
14+
15+
```sh
16+
pip install jsonrpcclient
17+
```
18+
19+
## Documentation
20+
21+
- [Requests](requests.md)
22+
- [Responses](responses.md)
23+
- [Faq](faq.md)
24+
- [Examples](examples.md)

docs/requests.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
```

docs/responses.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Parsing responses
2+
3+
The library includes a `parse` function which turns a deserialized response
4+
into a nice namedtuple.
5+
6+
```python
7+
>>> parse({"jsonrpc": "2.0", "result": "pong", "id": 1})
8+
Ok(result='pong', id=1)
9+
>>> parse({"jsonrpc": "2.0", "error": {"code": 1, "message": "There was an error", "data": None}, "id": 1})
10+
Error(code=1, message='There was an error', data=None, id=1)
11+
```
12+
13+
If you have a string, use `parse_json`.
14+
15+
```python
16+
>>> parse_json('{"jsonrpc": "2.0", "result": "pong", "id": 1}')
17+
Ok(result='pong', id=1)
18+
```
19+
20+
To use the result, in Python versions prior to 3.10:
21+
22+
```python
23+
from jsonrpcclient import Error, Ok
24+
parsed = parse(response)
25+
if isinstance(parsed, Ok):
26+
print(parsed.result)
27+
elif isinstance(parse, Error):
28+
logging.error(parsed.message)
29+
```
30+
31+
In Python 3.10+, use pattern matching:
32+
33+
```python
34+
match parse(response):
35+
case Ok(result, id):
36+
print(result)
37+
case Error(code, message, data, id):
38+
logging.error(message)
39+
```

mkdocs.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
site_name: jsonrpcclient
2+
site_url: https://explodinglabs.com/jsonrpcclient/
3+
repo_url: https://github.com/explodinglabs/jsonrpcclient
4+
theme:
5+
name: material
6+
features:
7+
- navigation.footer
8+
- palette.toggle
9+
- content.code.copy
10+
palette:
11+
# Palette toggle for automatic mode
12+
- media: "(prefers-color-scheme)"
13+
toggle:
14+
icon: material/brightness-auto
15+
name: Switch to light mode
16+
17+
# Palette toggle for light mode
18+
- media: "(prefers-color-scheme: light)"
19+
scheme: default
20+
toggle:
21+
icon: material/brightness-7
22+
name: Switch to dark mode
23+
24+
# Palette toggle for dark mode
25+
- media: "(prefers-color-scheme: dark)"
26+
scheme: slate
27+
toggle:
28+
icon: material/brightness-4
29+
name: Switch to system preference
30+
markdown_extensions:
31+
- pymdownx.highlight
32+
- pymdownx.superfences
33+
nav:
34+
- Home: index.md
35+
- Requests: requests.md
36+
- Responses: responses.md
37+
- FAQ: faq.md
38+
- Examples: examples.md

0 commit comments

Comments
 (0)