Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Enable all Pylint errors
  • Loading branch information
bcb committed Oct 10, 2022
commit 866a4e2aa94e1cd160095891e6b4d335e8280781
10 changes: 5 additions & 5 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.x
python-version: 3.8
- run: pip install --upgrade pip
- run: pip install "black<23" pylint==v3.0.0a3 mypy==v0.971 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/*')
- run: pip install types-setuptools "black<23" "pylint<3" "mypy<1" "jsonschema<5" pytest "returns<1" "aiohttp<4" "aiozmq<1" "django<5" "fastapi<1" "flask<3" "flask-socketio<5.3.1" "pyzmq" "sanic" "tornado<7" "uvicorn<1" "websockets<11"
- run: black --diff --check $(git ls-files -- '*.py' ':!:docs/*')
- run: pylint $(git ls-files -- '*.py' ':!:docs/*')
- run: mypy --strict $(git ls-files -- '*.py' ':!:docs/*')
41 changes: 36 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
default_language_version:
python: python3.8
exclude: (^docs)
fail_fast: true
repos:
- repo: https://github.com/ambv/black
Expand All @@ -6,16 +9,44 @@ repos:
- id: black
args: [--diff, --check]

- repo: https://github.com/pre-commit/mirrors-pylint
rev: v3.0.0a5
- repo: https://github.com/PyCQA/pylint
rev: v2.15.3
hooks:
- id: pylint
args: [--disable=all, --enable=unused-import]
args: [--ignored-modules=returns]
additional_dependencies:
- returns<1
- aiohttp<4
- aiozmq<1
- django<5
- fastapi<1
- flask<3
- flask-socketio<5.3.1
- jsonschema<5
- pytest
- pyzmq
- sanic
- tornado<7
- uvicorn<1
- websockets<11

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.971
hooks:
- id: mypy
exclude: (^tests|^examples|^docs)
args: [--strict]
additional_dependencies: ['types-setuptools', 'returns']
additional_dependencies:
- returns<1
- aiohttp<4
- aiozmq<1
- django<5
- fastapi<1
- flask<3
- flask-socketio<5.3.1
- pytest
- pyzmq
- sanic
- tornado<7
- types-setuptools
- uvicorn<1
- websockets<11
5 changes: 4 additions & 1 deletion examples/aiohttp_server.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""AioHTTP server"""
from aiohttp import web
from jsonrpcserver import async_dispatch, async_method, Ok, Result


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


async def handle(request):
async def handle(request: web.Request) -> web.Response:
"""Handle aiohttp request"""
return web.Response(
text=await async_dispatch(await request.text()), content_type="application/json"
)
Expand Down
10 changes: 7 additions & 3 deletions examples/aiozmq_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from jsonrpcserver import async_dispatch, async_method, Ok, Result
import aiozmq
"""AioZMQ server"""
import asyncio

import aiozmq # type: ignore
import zmq
from jsonrpcserver import async_dispatch, async_method, Ok, Result


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


async def main():
async def main() -> None:
"""Handle AioZMQ request"""
rep = await aiozmq.create_zmq_stream(zmq.REP, bind="tcp://*:5000")
while True:
request = (await rep.read())[0].decode()
Expand Down
8 changes: 5 additions & 3 deletions examples/asyncio_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Demonstrates processing a batch of 100 requests asynchronously"""
"""Demonstrates processing a batch of 100 requests asynchronously with asyncio."""
import asyncio
import json

Expand All @@ -7,12 +7,14 @@

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


async def handle(request: str) -> None:
print(await async_dispatch(request))
async def handle(req: str) -> None:
"""Handle asyncio event"""
print(await async_dispatch(req))


if __name__ == "__main__":
Expand Down
11 changes: 7 additions & 4 deletions examples/django_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
"""Django server"""
from django.http import HttpRequest, HttpResponse # type: ignore
from django.views.decorators.csrf import csrf_exempt # type: ignore
from jsonrpcserver import dispatch, method, Ok, Result


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


@csrf_exempt
def jsonrpc(request):
@csrf_exempt # type: ignore
def jsonrpc(request: HttpRequest) -> HttpResponse:
"""Handle Django request"""
return HttpResponse(
dispatch(request.body.decode()), content_type="application/json"
)
7 changes: 5 additions & 2 deletions examples/fastapi_server.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
"""FastAPI server"""
from fastapi import FastAPI, Request, Response
from jsonrpcserver import dispatch, method, Ok, Result
import uvicorn
import uvicorn # type: ignore

app = FastAPI()


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


@app.post("/")
async def index(request: Request):
async def index(request: Request) -> Response:
"""Handle FastAPI request"""
return Response(dispatch(await request.body()))


Expand Down
5 changes: 4 additions & 1 deletion examples/flask_server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Flask server"""
from flask import Flask, Response, request
from jsonrpcserver import dispatch, method, Ok, Result

Expand All @@ -6,11 +7,13 @@

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


@app.route("/", methods=["POST"])
def index():
def index() -> Response:
"""Handle Flask request"""
return Response(
dispatch(request.get_data().decode()), content_type="application/json"
)
Expand Down
10 changes: 9 additions & 1 deletion examples/http_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
"""HTTPServer server

Demonstrates using Python's builtin http.server module to serve JSON-RPC.
"""
from http.server import BaseHTTPRequestHandler, HTTPServer

from jsonrpcserver import dispatch, method, Ok, Result


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


class TestHttpServer(BaseHTTPRequestHandler):
def do_POST(self):
"""HTTPServer request handler"""

def do_POST(self) -> None: # pylint: disable=invalid-name
"""POST handler"""
# Process request
request = self.rfile.read(int(self.headers["Content-Length"])).decode()
response = dispatch(request)
Expand Down
5 changes: 5 additions & 0 deletions examples/jsonrpcserver_server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""Jsonrpcserver server.

Uses jsonrpcserver's built-in "serve" function.
"""
from jsonrpcserver import method, serve, Ok, Result


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


Expand Down
7 changes: 5 additions & 2 deletions examples/sanic_server.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"""Sanic server"""
from sanic import Sanic
from sanic.request import Request
from sanic.response import json
from sanic.response import HTTPResponse, json
from jsonrpcserver import dispatch_to_serializable, method, Ok, Result

app = Sanic("JSON-RPC app")


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


@app.route("/", methods=["POST"])
async def test(request: Request):
async def test(request: Request) -> HTTPResponse:
"""Handle Sanic request"""
return json(dispatch_to_serializable(request.body))


Expand Down
11 changes: 7 additions & 4 deletions examples/socketio_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Flask
from flask_socketio import SocketIO, send
"""SocketIO server"""
from flask import Flask, Request
from flask_socketio import SocketIO, send # type: ignore
from jsonrpcserver import dispatch, method, Ok, Result

app = Flask(__name__)
Expand All @@ -8,11 +9,13 @@

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


@socketio.on("message")
def handle_message(request):
@socketio.on("message") # type: ignore
def handle_message(request: Request) -> None:
"""Handle SocketIO request"""
if response := dispatch(request):
send(response, json=True)

Expand Down
15 changes: 13 additions & 2 deletions examples/tornado_server.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
from jsonrpcserver import async_dispatch, async_method, Ok, Result
"""Tornado server"""
from typing import Awaitable, Optional

from tornado import ioloop, web
from jsonrpcserver import async_dispatch, async_method, Ok, Result


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


class MainHandler(web.RequestHandler):
"""Handle Tornado request"""

async def post(self) -> None:
"""Post"""
request = self.request.body.decode()
if response := await async_dispatch(request):
response = await async_dispatch(request)
if response:
self.write(response)

def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
pass


app = web.Application([(r"/", MainHandler)])

Expand Down
9 changes: 6 additions & 3 deletions examples/websockets_server.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
"""Websockets server"""
import asyncio

from websockets.server import WebSocketServerProtocol, serve
from jsonrpcserver import async_dispatch, async_method, Ok, Result
import websockets


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


async def main(websocket, path):
async def main(websocket: WebSocketServerProtocol, _: str) -> None:
"""Handle Websocket message"""
if response := await async_dispatch(await websocket.recv()):
await websocket.send(response)


start_server = websockets.serve(main, "localhost", 5000)
start_server = serve(main, "localhost", 5000)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
11 changes: 6 additions & 5 deletions examples/werkzeug_server.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from jsonrpcserver import method, Result, Ok, dispatch
"""Werkzeug server"""
from werkzeug.serving import run_simple
from werkzeug.wrappers import Request, Response
from jsonrpcserver import method, Result, Ok, dispatch


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


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


if __name__ == "__main__":
Expand Down
4 changes: 3 additions & 1 deletion examples/zeromq_server.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from jsonrpcserver import dispatch, method, Ok, Result
"""ZeroMQ server"""
import zmq
from jsonrpcserver import dispatch, method, Ok, Result

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


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


Expand Down
Loading