|
1 | 1 | # Examples |
2 | 2 |
|
3 | | -## aiohttp |
4 | | - |
5 | | -```python |
6 | | -import asyncio |
7 | | -import logging |
8 | | - |
9 | | -from aiohttp import ClientSession |
10 | | - |
11 | | -from jsonrpcclient import Error, Ok, parse, request |
12 | | - |
13 | | - |
14 | | -async def main() -> None: |
15 | | - """Handle async request""" |
16 | | - async with ClientSession() as session: |
17 | | - async with session.post( |
18 | | - "http://localhost:5000", json=request("ping") |
19 | | - ) as response: |
20 | | - parsed = parse(await response.json()) |
21 | | - if isinstance(parsed, Ok): |
22 | | - print(parsed.result) |
23 | | - elif isinstance(parse, Error): |
24 | | - logging.error(parsed.message) |
25 | | - |
26 | | - |
27 | | -asyncio.get_event_loop().run_until_complete(main()) |
28 | | -``` |
29 | | - |
30 | | -See [blog post](https://explodinglabs.github.io/jsonrpc/aiohttp). |
31 | | - |
32 | | -## Requests |
33 | | - |
34 | | -```python |
35 | | -import logging |
36 | | - |
37 | | -import requests |
38 | | - |
39 | | -from jsonrpcclient import Error, Ok, parse, request |
40 | | - |
41 | | -response = requests.post("http://localhost:5000/", json=request("ping"), timeout=10) |
42 | | -parsed = parse(response.json()) |
43 | | -if isinstance(parsed, Ok): |
44 | | - print(parsed.result) |
45 | | -elif isinstance(parsed, Error): |
46 | | - logging.error(parsed.message) |
47 | | -``` |
48 | | - |
49 | | -## Websockets |
50 | | - |
51 | | -```python |
52 | | -import asyncio |
53 | | -import logging |
54 | | - |
55 | | -from websockets.client import connect |
56 | | - |
57 | | -from jsonrpcclient import Error, Ok, parse_json, request_json |
58 | | - |
59 | | - |
60 | | -async def main() -> None: |
61 | | - """Handle request""" |
62 | | - async with connect("ws://localhost:5000") as socket: |
63 | | - await socket.send(request_json("ping")) |
64 | | - response = parse_json(await socket.recv()) |
65 | | - |
66 | | - if isinstance(response, Ok): |
67 | | - print(response.result) |
68 | | - elif isinstance(response, Error): |
69 | | - logging.error(response.message) |
70 | | - |
71 | | - |
72 | | -asyncio.get_event_loop().run_until_complete(main()) |
73 | | -``` |
74 | | - |
75 | | -See [blog post](https://explodinglabs.github.io/jsonrpc/websockets). |
76 | | - |
77 | | -## ZeroMQ |
78 | | - |
79 | | -```python |
80 | | -import logging |
81 | | - |
82 | | -import zmq |
83 | | - |
84 | | -from jsonrpcclient import Ok, parse_json, request_json |
85 | | - |
86 | | -socket = zmq.Context().socket(zmq.REQ) |
87 | | -socket.connect("tcp://localhost:5000") |
88 | | -socket.send_string(request_json("ping")) |
89 | | -response = parse_json(socket.recv().decode()) |
90 | | -if isinstance(response, Ok): |
91 | | - print(response.result) |
92 | | -else: |
93 | | - logging.error(response.message) |
94 | | -``` |
95 | | - |
96 | | -See [blog post](https://explodinglabs.github.io/jsonrpc/zeromq). |
| 3 | +Examples have moved to the [Community Wiki](https://github.com/explodinglabs/jsonrpcclient/wiki). |
0 commit comments