Skip to content

Commit

Permalink
Add type hint support to use pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
abersheeran committed Aug 9, 2020
1 parent 1177913 commit 97ac15f
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 54 deletions.
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -22,6 +22,9 @@ pip install git+https://github.com/abersheeran/rpc.py@setup.py

### Server side:

<details markdown="1">
<summary>Use <code>ASGI</code> mode to register <code>async def</code>...</summary>

```python
import uvicorn
from rpcpy import RPC
Expand All @@ -48,9 +51,13 @@ async def yield_data(max_num: int):
if __name__ == "__main__":
uvicorn.run(app, interface="asgi3", port=65432)
```
</details>

OR

<details markdown="1">
<summary>Use <code>WSGI</code> mode to register <code>def</code>...</summary>

```python
import uvicorn
from rpcpy import RPC
Expand All @@ -77,9 +84,15 @@ def yield_data(max_num: int):
if __name__ == "__main__":
uvicorn.run(app, interface="wsgi", port=65432)
```
</details>

### Client side:

Notice: Regardless of whether the server uses the WSGI mode or the ASGI mode, the client can freely use the asynchronous or synchronous mode.

<details markdown="1">
<summary>Use <code>httpx.Client()</code> mode to register <code>def</code>...</summary>

```python
import httpx
from rpcpy.client import Client
Expand All @@ -101,9 +114,13 @@ def sayhi(name: str) -> str:
def yield_data(max_num: int):
yield
```
</details>

OR

<details markdown="1">
<summary>Use <code>httpx.AsyncClient()</code> mode to register <code>async def</code>...</summary>

```python
import httpx
from rpcpy.client import Client
Expand All @@ -125,6 +142,7 @@ async def sayhi(name: str) -> str:
async def yield_data(max_num: int):
yield
```
</details>

### Sub-route

Expand All @@ -142,6 +160,16 @@ RPC(serializer=JSONSerializer())
RPC(serializer=PickleSerializer())
```

## Type hint and OpenAPI Doc

Thanks to the great work of [pydantic](https://pydantic-docs.helpmanual.io/), which makes rpc.py allow you to use type annotation to annotate the types of function parameters and response values, and perform type verification and JSON serialization . At the same time, it is allowed to generate openapi documents for human reading.

### OpenAPI Documents

If you want to open the OpenAPI document, you need to initialize `RPC` like this `RPC(openapi={"title": "TITLE", "description": "DESCRIPTION", "version": "v1"})`.

Then, visit the `"{prefix}openapi-docs"` of RPC and you will be able to see the automatically generated OpenAPI documentation. (If you do not set the `prefix`, the `prefix` is `"/"`)

## Limitations

Currently, function parameters must be serializable by `json`.
57 changes: 54 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "rpc.py"
version = "0.2.2"
version = "0.3.0"
description = "An easy-to-use and powerful RPC framework. Base WSGI & ASGI."
authors = ["abersheeran <me@abersheeran.com>"]
readme = "README.md"
Expand All @@ -20,11 +20,14 @@ packages = [
[tool.poetry.dependencies]
python = "^3.6"

typing-extensions = {version = "^3.7.4", python = "<3.8"}
httpx = {version = "^0.13.3", optional = true} # for client and test
pydantic = {version = "^1.6.1", optional = true} # for openapi docs

[tool.poetry.extras]
client = ["httpx"]
full = ["httpx"]
type = ["pydantic"]
full = ["httpx", "pydantic"]

[tool.poetry.dev-dependencies]
flake8 = "*"
Expand Down

0 comments on commit 97ac15f

Please sign in to comment.