Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Replace readthedocs with mkdocs
  • Loading branch information
bcb committed Aug 16, 2024
commit 4c3b742d528156c4b69da332831eaf30ebeb41b2
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img
alt="jsonrpcserver"
style="margin: 0 auto;"
src="https://github.com/explodinglabs/jsonrpcserver/blob/main/docs/logo.png?raw=true"
src="https://github.com/explodinglabs/jsonrpcserver/blob/main/logo.png?raw=true"
/>

![PyPI](https://img.shields.io/pypi/v/jsonrpcserver.svg)
Expand All @@ -16,7 +16,7 @@ pip install jsonrpcserver
```

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

@method
def ping() -> Result:
Expand Down
File renamed without changes.
13 changes: 9 additions & 4 deletions docs/Dispatch.md → docs/dispatch.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
The `dispatch` function takes a JSON-RPC request, attempts to call a method and gives a
JSON-RPC response.
# Dispatch

The `dispatch` function processes a JSON-RPC request, attempting to call the method(s)
and gives a JSON-RPC response.

```python
>>> dispatch('{"jsonrpc": "2.0", "method": "ping", "id": 1}')
'{"jsonrpc": "2.0", "result": "pong", "id": 1}'
```

It's a pure function; it will always give you a JSON-RPC response. No exceptions will be
raised.

[See how dispatch is used in different frameworks.](examples)

## Optional parameters

The `dispatch` function has some optional parameters that allow you to
customise how it works.
The `dispatch` function takes a request as its argument, and also has some optional
parameters that allow you to customise how it works.

### methods

Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Jsonrpcserver

Jsonrpcserver processes JSON-RPC requests.

## Quickstart

Install jsonrpcserver:
Expand All @@ -19,19 +23,15 @@ if __name__ == "__main__":
```

Start the server:

```sh
$ pip install jsonrpcserver
$ python server.py
* Listening on port 5000
```

Test the server:

Send a request:
```sh
$ 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.
`serve` starts a basic development server. Do not use it in a production deployment. Use
a production WSGI server instead, with jsonrpcserver's [dispatch](dispatch) function.
11 changes: 7 additions & 4 deletions docs/Methods.md → docs/methods.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Methods are functions that can be called by a JSON-RPC request. To write one,
decorate a function with `@method`:
# Methods

Methods are functions that can be called by a JSON-RPC request.

## Writing methods

To write a method, decorate a function with `@method`:

```python
from jsonrpcserver import method, Error, Ok, Result
Expand All @@ -24,9 +29,7 @@ def test() -> Result:
return Error(1, "There was a problem")
```

```{note}
Alternatively, raise a `JsonRpcError`, which takes the same arguments as `Error`.
```

## Parameters

Expand Down
18 changes: 5 additions & 13 deletions jsonrpcserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,27 @@
http.server module.
"""

import logging
from http.server import BaseHTTPRequestHandler, HTTPServer

from .main import dispatch


class RequestHandler(BaseHTTPRequestHandler):
"""Handle HTTP requests"""

def do_POST(self) -> None: # pylint: disable=invalid-name
"""Handle POST request"""
response = dispatch(
self.rfile.read(int(str(self.headers["Content-Length"]))).decode()
)
request = self.rfile.read(int(str(self.headers["Content-Length"]))).decode()
response = dispatch(request)
if response is not None:
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(str(response).encode())
self.wfile.write(response.encode())


def serve(name: str = "", port: int = 5000) -> None:
"""A simple function to serve HTTP requests"""
logging.info(" * Listening on port %s", port)
httpd = HTTPServer((name, port), RequestHandler)
try:
httpd = HTTPServer((name, port), RequestHandler)
httpd.serve_forever()
except KeyboardInterrupt:
pass
except Exception:
finally:
httpd.shutdown()
raise
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
markdown_extensions:
- pymdownx.highlight:
pygments_lang_class: true
- pymdownx.inlinehilite
- pymdownx.snippets
- pymdownx.details
- pymdownx.superfences
- pymdownx.mark
nav:
- Home: 'index.md'
- 'methods.md'
- 'dispatch.md'
- 'async.md'
- 'faq.md'
- 'examples.md'
repo_name: jsonrpcserver
repo_url: https://github.com/explodinglabs/jsonrpcserver
site_author: Exploding Labs
site_description: Welcome to the documentation for Jsonrcpcserver.
site_name: Jsonrpcserver
site_url: https://www.jsonrpcserver.com/
theme:
features:
- content.code.copy
- navigation.footer
- navigation.tabs
- toc.integrate
name: material
palette:
# Palette toggle for automatic mode
- media: "(prefers-color-scheme)"
toggle:
icon: material/brightness-auto
name: Switch to light mode
# Palette toggle for light mode
- media: "(prefers-color-scheme: light)"
scheme: default
toggle:
icon: material/brightness-7
name: Switch to dark mode
# Palette toggle for dark mode
- media: "(prefers-color-scheme: dark)"
scheme: slate
toggle:
icon: material/brightness-4
name: Switch to system preference
extra:
version:
provider: mike