Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0f1fd29
Replace Oslash with Returns
bcb May 14, 2022
c1426f6
Fix github action
bcb May 14, 2022
d806553
Fix github action
bcb May 14, 2022
0ede79e
Add comment to docs
bcb May 17, 2022
9451357
Merge branch 'use-returns' into prepare-v6
bcb May 18, 2022
a854670
Prepare version 6.0.0
bcb May 18, 2022
e9c2da2
Merge branch 'main' into prepare-v6
bcb May 19, 2022
8808a43
Adjust docstring
bcb May 20, 2022
03f80d0
Remove errant comma
bcb May 24, 2022
173bbca
Merge branch 'main' into release/6.0.0
bcb Sep 21, 2022
609b990
Adjust github workflow
bcb Sep 21, 2022
bae441c
Upgrade mypy in github workflow
bcb Sep 21, 2022
866a4e2
Enable all Pylint errors
bcb Oct 10, 2022
e74da97
Merge branch 'main' into release/6.0.0
bcb Oct 10, 2022
f6294ee
Fix not re-exported error
bcb Oct 10, 2022
ab30150
Upgrade pylint
bcb Nov 9, 2022
4f510b1
Pylint fixes
bcb Nov 9, 2022
73ba6a3
Update changelog
bcb Nov 9, 2022
0e67522
Fix some tests
bcb Nov 16, 2022
f4e8761
Fix parameters
bcb Feb 26, 2023
558215b
Fixes to satisfy ruff
bcb Feb 26, 2023
0a88d8f
Replace pylint with ruff
bcb Mar 3, 2023
65f798c
Adjustments to satisfy ruff and mypy
bcb Mar 3, 2023
843fdcd
Fix a repr
bcb May 10, 2023
ae90cdf
Use ruff in github actions
bcb May 10, 2023
6db0f90
Fix type error
bcb May 10, 2023
e7287b5
Upgrade ruff
bcb May 10, 2023
75cee0e
Replace setup.py with pyproject.toml
bcb May 24, 2023
9a1fe9f
Always stop the server when exiting serve()
bcb May 31, 2023
ffe8374
Replace black and isort with ruff (#278)
bcb Jul 29, 2024
be34675
Move documentation to Github wiki (#280)
bcb Jul 31, 2024
389959d
Update readme (#281)
bcb Jul 31, 2024
8c23c46
Replace readthedocs with mkdocs (#282)
bcb Aug 16, 2024
1d9153e
Remove pylint pragmas (#283)
bcb Aug 17, 2024
6efc172
Move request-schema.json to a .py file (#284)
bcb Aug 28, 2024
5c745ce
Add jsonschema dependency
bcb Nov 14, 2024
c2b807e
Add license badge
bcb Mar 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
with:
python-version: 3.x
- run: pip install --upgrade pip
- run: pip install black==21.6b0 pylint==v3.0.0a3 mypy==v0.902 types-setuptools
- run: pip install "black<23" pylint==v3.0.0a3 mypy==v0.950 types-setuptools "returns<1"
- run: black --diff --check $(git ls-files -- '*.py' ':!:tests/*' ':!:docs/*' ':!:examples/*')
- run: pylint --disable=all --enable=unused-import $(git ls-files -- '*.py' ':!:tests/*' ':!:docs/*' ':!:examples/*')
- run: mypy --strict $(git ls-files -- '*.py' ':!:tests/*' ':!:docs/*' ':!:examples/*')
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fail_fast: true
repos:
- repo: https://github.com/ambv/black
rev: 21.7b0
rev: 22.3.0
hooks:
- id: black
args: [--diff, --check]
Expand All @@ -13,9 +13,9 @@ repos:
args: [--disable=all, --enable=unused-import]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910
rev: v0.950
hooks:
- id: mypy
exclude: (^tests|^examples|^docs)
args: [--strict]
additional_dependencies: ['types-setuptools']
additional_dependencies: ['types-setuptools', 'returns']
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
Process incoming JSON-RPC requests in Python.

```python
from jsonrpcserver import method, serve, Success
from jsonrpcserver import method, serve, Ok, Result

@method
def ping():
return Success("pong")
def ping() -> Result:
return Ok("pong")

if __name__ == "__main__":
serve()
Expand Down
6 changes: 3 additions & 3 deletions docs/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
Async dispatch is supported.

```python
from jsonrpcserver import method, Success, async_dispatch
from jsonrpcserver import async_dispatch, async_method, Ok, Result

@method
@async_method
async def ping() -> Result:
return Success("pong")
return Ok("pong")

await async_dispatch('{"jsonrpc": "2.0", "method": "ping", "id": 1}')
```
Expand Down
10 changes: 5 additions & 5 deletions docs/dispatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ and gives a JSON-RPC response.

### methods

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

```python
def ping():
return Success("pong")
return Ok("pong")

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

>>> dispatch('{"jsonrpc": "2.0", "method": "greet", "params": ["Beau"], "id": 1}', context="Hello")
'{"jsonrpc": "2.0", "result": "Hello Beau", "id": 1}'
Expand Down
7 changes: 5 additions & 2 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
Create a `server.py`:

```python
from jsonrpcserver import Success, method, serve
from jsonrpcserver import method, serve, Ok

@method
def ping():
return Success("pong")
return Ok("pong")

if __name__ == "__main__":
serve()
Expand All @@ -27,3 +27,6 @@ Test the server:
$ curl -X POST http://localhost:5000 -d '{"jsonrpc": "2.0", "method": "ping", "id": 1}'
{"jsonrpc": "2.0", "result": "pong", "id": 1}
```

`serve` is good for serving methods in development, but for production use
`dispatch` instead.
17 changes: 9 additions & 8 deletions docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ Methods are functions that can be called by a JSON-RPC request. To write one,
decorate a function with `@method`:

```python
from jsonrpcserver import method, Result, Success, Error
from jsonrpcserver import method, Error, Ok, Result

@method
def ping() -> Result:
return Success("pong")
return Ok("pong")
```

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

## Responses

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

```python
@method
Expand All @@ -36,7 +37,7 @@ Methods can accept arguments.
```python
@method
def hello(name: str) -> Result:
return Success("Hello " + name)
return Ok("Hello " + name)
```

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

```python
from jsonrpcserver import method, Result, InvalidParams, Success, dispatch
from jsonrpcserver import dispatch, method, InvalidParams, Ok, Result

@method
def within_range(num: int) -> Result:
if num not in range(1, 5):
return InvalidParams("Value must be 1-5")
return Success()
return Ok()
```

This is the same as saying
Expand Down
6 changes: 3 additions & 3 deletions examples/aiohttp_server.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from aiohttp import web
from jsonrpcserver import method, Result, Success, async_dispatch
from jsonrpcserver import async_dispatch, async_method, Ok, Result


@method
@async_method
async def ping() -> Result:
return Success("pong")
return Ok("pong")


async def handle(request):
Expand Down
6 changes: 3 additions & 3 deletions examples/aiozmq_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from jsonrpcserver import method, Result, Success, async_dispatch
from jsonrpcserver import async_dispatch, async_method, Ok, Result
import aiozmq
import asyncio
import zmq


@method
@async_method
async def ping() -> Result:
return Success("pong")
return Ok("pong")


async def main():
Expand Down
6 changes: 3 additions & 3 deletions examples/asyncio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import asyncio
import json

from jsonrpcserver import method, Result, Success, async_dispatch
from jsonrpcserver import async_dispatch, async_method, Ok, Result


@method
@async_method
async def sleep_() -> Result:
await asyncio.sleep(1)
return Success()
return Ok()


async def handle(request: str) -> None:
Expand Down
4 changes: 2 additions & 2 deletions examples/django_server.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from jsonrpcserver import method, Result, Success, dispatch
from jsonrpcserver import dispatch, method, Ok, Result


@method
def ping() -> Result:
return Success("pong")
return Ok("pong")


@csrf_exempt
Expand Down
4 changes: 2 additions & 2 deletions examples/fastapi_server.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from fastapi import FastAPI, Request, Response
from jsonrpcserver import Result, Success, dispatch, method
from jsonrpcserver import dispatch, method, Ok, Result
import uvicorn

app = FastAPI()


@method
def ping() -> Result:
return Success("pong")
return Ok("pong")


@app.post("/")
Expand Down
4 changes: 2 additions & 2 deletions examples/flask_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from flask import Flask, Response, request
from jsonrpcserver import method, Result, Success, dispatch
from jsonrpcserver import dispatch, method, Ok, Result

app = Flask(__name__)


@method
def ping() -> Result:
return Success("pong")
return Ok("pong")


@app.route("/", methods=["POST"])
Expand Down
4 changes: 2 additions & 2 deletions examples/http_server.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from http.server import BaseHTTPRequestHandler, HTTPServer

from jsonrpcserver import method, Result, Success, dispatch
from jsonrpcserver import dispatch, method, Ok, Result


@method
def ping() -> Result:
return Success("pong")
return Ok("pong")


class TestHttpServer(BaseHTTPRequestHandler):
Expand Down
4 changes: 2 additions & 2 deletions examples/jsonrpcserver_server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from jsonrpcserver import method, Result, Success, serve
from jsonrpcserver import method, serve, Ok, Result


@method
def ping() -> Result:
return Success("pong")
return Ok("pong")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions examples/sanic_server.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from sanic import Sanic
from sanic.request import Request
from sanic.response import json
from jsonrpcserver import Result, Success, dispatch_to_serializable, method
from jsonrpcserver import dispatch_to_serializable, method, Ok, Result

app = Sanic("JSON-RPC app")


@method
def ping() -> Result:
return Success("pong")
return Ok("pong")


@app.route("/", methods=["POST"])
Expand Down
4 changes: 2 additions & 2 deletions examples/socketio_server.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from flask import Flask
from flask_socketio import SocketIO, send
from jsonrpcserver import method, Result, Success, dispatch
from jsonrpcserver import dispatch, method, Ok, Result

app = Flask(__name__)
socketio = SocketIO(app)


@method
def ping() -> Result:
return Success("pong")
return Ok("pong")


@socketio.on("message")
Expand Down
6 changes: 3 additions & 3 deletions examples/tornado_server.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from jsonrpcserver import method, Result, Success, async_dispatch
from jsonrpcserver import async_dispatch, async_method, Ok, Result
from tornado import ioloop, web


@method
@async_method
async def ping() -> Result:
return Success("pong")
return Ok("pong")


class MainHandler(web.RequestHandler):
Expand Down
6 changes: 3 additions & 3 deletions examples/websockets_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import asyncio

from jsonrpcserver import method, Success, Result, async_dispatch
from jsonrpcserver import async_dispatch, async_method, Ok, Result
import websockets


@method
@async_method
async def ping() -> Result:
return Success("pong")
return Ok("pong")


async def main(websocket, path):
Expand Down
8 changes: 5 additions & 3 deletions examples/werkzeug_server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from jsonrpcserver import method, Result, Success, dispatch
from jsonrpcserver import method, Result, Ok, dispatch
from werkzeug.serving import run_simple
from werkzeug.wrappers import Request, Response


@method
def ping() -> Result:
return Success("pong")
return Ok("pong")


@Request.application
def application(request):
return Response(dispatch(request.data.decode()), 200, mimetype="application/json")
return Response(
dispatch(request.get_data().decode()), 200, mimetype="application/json"
)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions examples/zeromq_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from jsonrpcserver import method, Result, Success, dispatch
from jsonrpcserver import dispatch, method, Ok, Result
import zmq

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


@method
def ping() -> Result:
return Success("pong")
return Ok("pong")


if __name__ == "__main__":
Expand Down
Loading