Skip to content

Commit 9451357

Browse files
committed
Merge branch 'use-returns' into prepare-v6
2 parents 50040bd + 0ede79e commit 9451357

37 files changed

+388
-335
lines changed

.github/workflows/code-quality.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
with:
1111
python-version: 3.x
1212
- run: pip install --upgrade pip
13-
- run: pip install black==21.6b0 pylint==v3.0.0a3 mypy==v0.902 types-setuptools
13+
- run: pip install "black<23" pylint==v3.0.0a3 mypy==v0.950 types-setuptools "returns<1"
1414
- run: black --diff --check $(git ls-files -- '*.py' ':!:tests/*' ':!:docs/*' ':!:examples/*')
1515
- run: pylint --disable=all --enable=unused-import $(git ls-files -- '*.py' ':!:tests/*' ':!:docs/*' ':!:examples/*')
1616
- run: mypy --strict $(git ls-files -- '*.py' ':!:tests/*' ':!:docs/*' ':!:examples/*')

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fail_fast: true
22
repos:
33
- repo: https://github.com/ambv/black
4-
rev: 21.7b0
4+
rev: 22.3.0
55
hooks:
66
- id: black
77
args: [--diff, --check]
@@ -13,9 +13,9 @@ repos:
1313
args: [--disable=all, --enable=unused-import]
1414

1515
- repo: https://github.com/pre-commit/mirrors-mypy
16-
rev: v0.910
16+
rev: v0.950
1717
hooks:
1818
- id: mypy
1919
exclude: (^tests|^examples|^docs)
2020
args: [--strict]
21-
additional_dependencies: ['types-setuptools']
21+
additional_dependencies: ['types-setuptools', 'returns']

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
Process incoming JSON-RPC requests in Python.
1313

1414
```python
15-
from jsonrpcserver import method, serve, Success
15+
from jsonrpcserver import method, serve, Ok, Result
1616

1717
@method
18-
def ping():
19-
return Success("pong")
18+
def ping() -> Result:
19+
return Ok("pong")
2020

2121
if __name__ == "__main__":
2222
serve()

docs/async.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
Async dispatch is supported.
44

55
```python
6-
from jsonrpcserver import method, Success, async_dispatch
6+
from jsonrpcserver import async_dispatch, async_method, Ok, Result
77

8-
@method
8+
@async_method
99
async def ping() -> Result:
10-
return Success("pong")
10+
return Ok("pong")
1111

1212
await async_dispatch('{"jsonrpc": "2.0", "method": "ping", "id": 1}')
1313
```

docs/dispatch.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ and gives a JSON-RPC response.
1414

1515
### methods
1616

17-
This lets you specify a group of methods to dispatch to. It's an alternative to
18-
using the `@method` decorator. The value should be a dict mapping function
19-
names to functions.
17+
This lets you specify the methods to dispatch to. It's an alternative to using
18+
the `@method` decorator. The value should be a dict mapping function names to
19+
functions.
2020

2121
```python
2222
def ping():
23-
return Success("pong")
23+
return Ok("pong")
2424

2525
dispatch(request, methods={"ping": ping})
2626
```
@@ -35,7 +35,7 @@ If specified, this will be the first argument to all methods.
3535
```python
3636
@method
3737
def greet(context, name):
38-
return Success(context + " " + name)
38+
return Ok(context + " " + name)
3939

4040
>>> dispatch('{"jsonrpc": "2.0", "method": "greet", "params": ["Beau"], "id": 1}', context="Hello")
4141
'{"jsonrpc": "2.0", "result": "Hello Beau", "id": 1}'

docs/installation.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
Create a `server.py`:
44

55
```python
6-
from jsonrpcserver import Success, method, serve
6+
from jsonrpcserver import method, serve, Ok
77

88
@method
99
def ping():
10-
return Success("pong")
10+
return Ok("pong")
1111

1212
if __name__ == "__main__":
1313
serve()
@@ -27,3 +27,6 @@ Test the server:
2727
$ curl -X POST http://localhost:5000 -d '{"jsonrpc": "2.0", "method": "ping", "id": 1}'
2828
{"jsonrpc": "2.0", "result": "pong", "id": 1}
2929
```
30+
31+
`serve` is good for serving methods in development, but for production use
32+
`dispatch` instead.

docs/methods.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ Methods are functions that can be called by a JSON-RPC request. To write one,
44
decorate a function with `@method`:
55

66
```python
7-
from jsonrpcserver import method, Result, Success, Error
7+
from jsonrpcserver import method, Error, Ok, Result
88

99
@method
1010
def ping() -> Result:
11-
return Success("pong")
11+
return Ok("pong")
1212
```
1313

14-
If you don't need to respond with any value simply `return Success()`.
14+
If you don't need to respond with any value simply `return Ok()`.
1515

1616
## Responses
1717

18-
Methods return either `Success` or `Error`. These are the [JSON-RPC response
18+
Methods return either `Ok` or `Error`. These are the [JSON-RPC response
1919
objects](https://www.jsonrpc.org/specification#response_object) (excluding the
20-
`jsonrpc` and `id` parts). `Error` takes a code, message, and optionally 'data'.
20+
`jsonrpc` and `id` parts). `Error` takes a code, message, and optionally
21+
'data'.
2122

2223
```python
2324
@method
@@ -36,7 +37,7 @@ Methods can accept arguments.
3637
```python
3738
@method
3839
def hello(name: str) -> Result:
39-
return Success("Hello " + name)
40+
return Ok("Hello " + name)
4041
```
4142

4243
Testing it:
@@ -53,13 +54,13 @@ The JSON-RPC error code for this is **-32602**. A shortcut, *InvalidParams*, is
5354
included so you don't need to remember that.
5455

5556
```python
56-
from jsonrpcserver import method, Result, InvalidParams, Success, dispatch
57+
from jsonrpcserver import dispatch, method, InvalidParams, Ok, Result
5758

5859
@method
5960
def within_range(num: int) -> Result:
6061
if num not in range(1, 5):
6162
return InvalidParams("Value must be 1-5")
62-
return Success()
63+
return Ok()
6364
```
6465

6566
This is the same as saying

examples/aiohttp_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from aiohttp import web
2-
from jsonrpcserver import method, Result, Success, async_dispatch
2+
from jsonrpcserver import async_dispatch, async_method, Ok, Result
33

44

5-
@method
5+
@async_method
66
async def ping() -> Result:
7-
return Success("pong")
7+
return Ok("pong")
88

99

1010
async def handle(request):

examples/aiozmq_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from jsonrpcserver import method, Result, Success, async_dispatch
1+
from jsonrpcserver import async_dispatch, async_method, Ok, Result
22
import aiozmq
33
import asyncio
44
import zmq
55

66

7-
@method
7+
@async_method
88
async def ping() -> Result:
9-
return Success("pong")
9+
return Ok("pong")
1010

1111

1212
async def main():

examples/asyncio_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import asyncio
33
import json
44

5-
from jsonrpcserver import method, Result, Success, async_dispatch
5+
from jsonrpcserver import async_dispatch, async_method, Ok, Result
66

77

8-
@method
8+
@async_method
99
async def sleep_() -> Result:
1010
await asyncio.sleep(1)
11-
return Success()
11+
return Ok()
1212

1313

1414
async def handle(request: str) -> None:

examples/django_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from django.http import HttpResponse
22
from django.views.decorators.csrf import csrf_exempt
3-
from jsonrpcserver import method, Result, Success, dispatch
3+
from jsonrpcserver import dispatch, method, Ok, Result
44

55

66
@method
77
def ping() -> Result:
8-
return Success("pong")
8+
return Ok("pong")
99

1010

1111
@csrf_exempt

examples/fastapi_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from fastapi import FastAPI, Request, Response
2-
from jsonrpcserver import Result, Success, dispatch, method
2+
from jsonrpcserver import dispatch, method, Ok, Result
33
import uvicorn
44

55
app = FastAPI()
66

77

88
@method
99
def ping() -> Result:
10-
return Success("pong")
10+
return Ok("pong")
1111

1212

1313
@app.post("/")

examples/flask_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from flask import Flask, Response, request
2-
from jsonrpcserver import method, Result, Success, dispatch
2+
from jsonrpcserver import dispatch, method, Ok, Result
33

44
app = Flask(__name__)
55

66

77
@method
88
def ping() -> Result:
9-
return Success("pong")
9+
return Ok("pong")
1010

1111

1212
@app.route("/", methods=["POST"])

examples/http_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from http.server import BaseHTTPRequestHandler, HTTPServer
22

3-
from jsonrpcserver import method, Result, Success, dispatch
3+
from jsonrpcserver import dispatch, method, Ok, Result
44

55

66
@method
77
def ping() -> Result:
8-
return Success("pong")
8+
return Ok("pong")
99

1010

1111
class TestHttpServer(BaseHTTPRequestHandler):

examples/jsonrpcserver_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from jsonrpcserver import method, Result, Success, serve
1+
from jsonrpcserver import method, serve, Ok, Result
22

33

44
@method
55
def ping() -> Result:
6-
return Success("pong")
6+
return Ok("pong")
77

88

99
if __name__ == "__main__":

examples/sanic_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from sanic import Sanic
22
from sanic.request import Request
33
from sanic.response import json
4-
from jsonrpcserver import Result, Success, dispatch_to_serializable, method
4+
from jsonrpcserver import dispatch_to_serializable, method, Ok, Result
55

66
app = Sanic("JSON-RPC app")
77

88

99
@method
1010
def ping() -> Result:
11-
return Success("pong")
11+
return Ok("pong")
1212

1313

1414
@app.route("/", methods=["POST"])

examples/socketio_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from flask import Flask
22
from flask_socketio import SocketIO, send
3-
from jsonrpcserver import method, Result, Success, dispatch
3+
from jsonrpcserver import dispatch, method, Ok, Result
44

55
app = Flask(__name__)
66
socketio = SocketIO(app)
77

88

99
@method
1010
def ping() -> Result:
11-
return Success("pong")
11+
return Ok("pong")
1212

1313

1414
@socketio.on("message")

examples/tornado_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from jsonrpcserver import method, Result, Success, async_dispatch
1+
from jsonrpcserver import async_dispatch, async_method, Ok, Result
22
from tornado import ioloop, web
33

44

5-
@method
5+
@async_method
66
async def ping() -> Result:
7-
return Success("pong")
7+
return Ok("pong")
88

99

1010
class MainHandler(web.RequestHandler):

examples/websockets_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import asyncio
22

3-
from jsonrpcserver import method, Success, Result, async_dispatch
3+
from jsonrpcserver import async_dispatch, async_method, Ok, Result
44
import websockets
55

66

7-
@method
7+
@async_method
88
async def ping() -> Result:
9-
return Success("pong")
9+
return Ok("pong")
1010

1111

1212
async def main(websocket, path):

examples/werkzeug_server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
from jsonrpcserver import method, Result, Success, dispatch
1+
from jsonrpcserver import method, Result, Ok, dispatch
22
from werkzeug.serving import run_simple
33
from werkzeug.wrappers import Request, Response
44

55

66
@method
77
def ping() -> Result:
8-
return Success("pong")
8+
return Ok("pong")
99

1010

1111
@Request.application
1212
def application(request):
13-
return Response(dispatch(request.data.decode()), 200, mimetype="application/json")
13+
return Response(
14+
dispatch(request.get_data().decode()), 200, mimetype="application/json"
15+
)
1416

1517

1618
if __name__ == "__main__":

examples/zeromq_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from jsonrpcserver import method, Result, Success, dispatch
1+
from jsonrpcserver import dispatch, method, Ok, Result
22
import zmq
33

44
socket = zmq.Context().socket(zmq.REP)
55

66

77
@method
88
def ping() -> Result:
9-
return Success("pong")
9+
return Ok("pong")
1010

1111

1212
if __name__ == "__main__":

0 commit comments

Comments
 (0)