Full Code of abersheeran/rpc.py for AI

master cb7e907a0281 cached
26 files
77.8 KB
17.7k tokens
102 symbols
1 requests
Download .txt
Repository: abersheeran/rpc.py
Branch: master
Commit: cb7e907a0281
Files: 26
Total size: 77.8 KB

Directory structure:
gitextract_2emp0qll/

├── .editorconfig
├── .gitattributes
├── .github/
│   ├── FUNDING.yml
│   └── workflows/
│       └── ci.yml
├── .gitignore
├── LICENSE
├── README.md
├── examples/
│   ├── async_client.py
│   ├── async_server.py
│   ├── sync_client.py
│   └── sync_server.py
├── pyproject.toml
├── rpcpy/
│   ├── __init__.py
│   ├── __version__.py
│   ├── application.py
│   ├── client.py
│   ├── exceptions.py
│   ├── openapi.py
│   └── serializers.py
├── script/
│   ├── check.py
│   └── upload.py
└── tests/
    ├── __init__.py
    ├── test_application.py
    ├── test_client.py
    ├── test_serializers.py
    └── test_version.py

================================================
FILE CONTENTS
================================================

================================================
FILE: .editorconfig
================================================
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
end_of_line = crlf
insert_final_newline = true
charset = utf-8

[*.py]
indent_style = space
indent_size = 4

[*.{yml,yaml}]
indent_style = space
indent_size = 2


================================================
FILE: .gitattributes
================================================
* text=auto


================================================
FILE: .github/FUNDING.yml
================================================
# These are supported funding model platforms

custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
  - "https://donate.aber.sh/"


================================================
FILE: .github/workflows/ci.yml
================================================
name: CI

on:
  push:
    branches:
      - master
    tags:
      - "*"
    paths:
      - "**.py"
      - "!rpcpy/__version__.py"
  pull_request:
    branches:
      - master
    paths:
      - "**.py"
      - "!rpcpy/__version__.py"

jobs:
  Tests:
    name: "Python ${{ matrix.python-version }} ${{ matrix.os }}"
    runs-on: "${{ matrix.os }}"
    strategy:
      matrix:
        python-version: [3.7, 3.8, 3.9, "3.10"]
        os: [windows-latest, ubuntu-latest, macos-latest]
    env:
      OS: ${{ matrix.os }}
      PYTHON: ${{ matrix.python-version }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip poetry
          poetry config virtualenvs.create false --local
          poetry install -E full
      - name: Static type and format checking
        run: |
          python script/check.py
      - name: Generate coverage report
        run: |
          pytest --cov=./ --cov-report=xml
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v1
        with:
          files: ./coverage.xml
          directory: ./coverage/reports/
          flags: unittests
          env_vars: OS,PYTHON
          fail_ci_if_error: true
          path_to_write_report: ./coverage/codecov_report.txt
          verbose: true
      - name: Uninstall pydantic
        run: |
          pip uninstall -y pydantic
      - name: Generate coverage report
        run: |
          pytest --cov=./ --cov-report=xml
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v1
        with:
          files: ./coverage.xml
          directory: ./coverage/reports/
          flags: unittests
          env_vars: OS,PYTHON
          fail_ci_if_error: true
          path_to_write_report: ./coverage/codecov_report.txt
          verbose: true

  Publish:
    needs: Tests
    if: startsWith(github.ref, 'refs/tags/')

    runs-on: "${{ matrix.os }}"
    environment: release
    permissions:
      id-token: write
    strategy:
      matrix:
        python-version: [3.7]
        os: [ubuntu-latest]

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip poetry

      - name: Build
        run: |
          poetry build

      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1


================================================
FILE: .gitignore
================================================
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IPython Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/
.venv/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject
*.npy
*.pkl

# mypy
.mypy_cache/

# VSCode
.vscode/

# PyCharm
.idea/

# mkdocs
site/


================================================
FILE: LICENSE
================================================
                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   APPENDIX: How to apply the Apache License to your work.

      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "[]"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.

   Copyright 2022 abersheeran

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.


================================================
FILE: README.md
================================================
# rpc.py

[![Codecov](https://img.shields.io/codecov/c/github/abersheeran/rpc.py?style=flat-square)](https://codecov.io/gh/abersheeran/rpc.py)

An fast and powerful RPC framework based on ASGI/WSGI. Based on WSGI/ASGI, you can deploy the rpc.py server to any server and use http2 to get better performance. And based on httpx's support for multiple http protocols, the client can also use http/1.0, http/1.1 or http2.

You can freely use ordinary functions and asynchronous functions for one-time response. You can also use generator functions or asynchronous generator functions to stream responses.

## Install

Install from PyPi:

```bash
pip install rpc.py

# need use client
pip install rpc.py[client]

# need use pydantic type hint or OpenAPI docs
pip install rpc.py[type]

# need use msgpack to serializer
pip install rpc.py[msgpack]

# need use CBOR to serializer
pip install rpc.py[cbor]

# or install all dependencies
pip install rpc.py[full]
```

Install from github:

```bash
pip install git+https://github.com/abersheeran/rpc.py@setup.py
```

## Usage

### Server side:

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

```python
from typing import AsyncGenerator
from typing_extensions import TypedDict

import uvicorn
from rpcpy import RPC

app = RPC(mode="ASGI")


@app.register
async def none() -> None:
    return


@app.register
async def sayhi(name: str) -> str:
    return f"hi {name}"


@app.register
async def yield_data(max_num: int) -> AsyncGenerator[int, None]:
    for i in range(max_num):
        yield i


D = TypedDict("D", {"key": str, "other-key": str})


@app.register
async def query_dict(value: str) -> D:
    return {"key": value, "other-key": value}


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
from typing import Generator
from typing_extensions import TypedDict

import uvicorn
from rpcpy import RPC

app = RPC()


@app.register
def none() -> None:
    return


@app.register
def sayhi(name: str) -> str:
    return f"hi {name}"


@app.register
def yield_data(max_num: int) -> Generator[int, None, None]:
    for i in range(max_num):
        yield i


D = TypedDict("D", {"key": str, "other-key": str})


@app.register
def query_dict(value: str) -> D:
    return {"key": value, "other-key": value}


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
from typing import Generator
from typing_extensions import TypedDict

import httpx
from rpcpy.client import Client

app = Client(httpx.Client(), base_url="http://127.0.0.1:65432/")


@app.remote_call
def none() -> None:
    ...


@app.remote_call
def sayhi(name: str) -> str:
    ...


@app.remote_call
def yield_data(max_num: int) -> Generator[int, None, None]:
    yield


D = TypedDict("D", {"key": str, "other-key": str})


@app.remote_call
def query_dict(value: str) -> D:
    ...
```
</details>

OR

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

```python
from typing import AsyncGenerator
from typing_extensions import TypedDict

import httpx
from rpcpy.client import Client

app = Client(httpx.AsyncClient(), base_url="http://127.0.0.1:65432/")


@app.remote_call
async def none() -> None:
    ...


@app.remote_call
async def sayhi(name: str) -> str:
    ...


@app.remote_call
async def yield_data(max_num: int) -> AsyncGenerator[int, None]:
    yield


D = TypedDict("D", {"key": str, "other-key": str})


@app.remote_call
async def query_dict(value: str) -> D:
    ...
```
</details>

### Server as client

You can also write two copies of code in one place. Just make sure that `server.register` is executed before `client.remote_call`.

```python
import httpx
from rpcpy import RPC
from rpcpy.client import Client

server = RPC()
client = Client(httpx.Client(), base_url="http://127.0.0.1:65432/")


@client.remote_call
@server.register
def sayhi(name: str) -> str:
    return f"hi {name}"


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, interface="wsgi", port=65432)
```

### Sub-route

If you need to deploy the rpc.py server under `example.com/sub-route/*`, you need to set `RPC(prefix="/sub-route/")` and modify the `Client(base_path=https://example.com/sub-route/)`.

### Serialization

Currently supports three serializers, JSON, Msgpack and CBOR. JSON is used by default. You can override the default `JSONSerializer` with parameters.

```python
from rpcpy.serializers import MsgpackSerializer, CBORSerializer

RPC(
    ...,
    response_serializer=MsgpackSerializer(),
)
# Or
Client(
    ...,
    request_serializer=CBORSerializer(),
)
```

## 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, file upload is not supported, but you can do this by passing a `bytes` object.


================================================
FILE: examples/async_client.py
================================================
from typing import AsyncGenerator

import httpx
from typing_extensions import TypedDict

from rpcpy.client import Client

app = Client(httpx.AsyncClient(), base_url="http://127.0.0.1:65432/")


@app.remote_call
async def none() -> None:
    ...


@app.remote_call
async def sayhi(name: str) -> str:
    ...


@app.remote_call
async def yield_data(max_num: int) -> AsyncGenerator[int, None]:
    yield


D = TypedDict("D", {"key": str, "other-key": str})


@app.remote_call
async def query_dict(value: str) -> D:
    ...


================================================
FILE: examples/async_server.py
================================================
from typing import AsyncGenerator

import uvicorn
from typing_extensions import TypedDict

from rpcpy import RPC

app = RPC(mode="ASGI")


@app.register
async def none() -> None:
    return


@app.register
async def sayhi(name: str) -> str:
    return f"hi {name}"


@app.register
async def yield_data(max_num: int) -> AsyncGenerator[int, None]:
    for i in range(max_num):
        yield i


D = TypedDict("D", {"key": str, "other-key": str})


@app.register
async def query_dict(value: str) -> D:
    return {"key": value, "other-key": value}


if __name__ == "__main__":
    uvicorn.run(app, interface="asgi3", port=65432)


================================================
FILE: examples/sync_client.py
================================================
from typing import Generator

import httpx
from typing_extensions import TypedDict

from rpcpy.client import Client

app = Client(httpx.Client(), base_url="http://127.0.0.1:65432/")


@app.remote_call
def none() -> None:
    ...


@app.remote_call
def sayhi(name: str) -> str:
    ...


@app.remote_call
def yield_data(max_num: int) -> Generator[int, None, None]:
    yield


D = TypedDict("D", {"key": str, "other-key": str})


@app.remote_call
def query_dict(value: str) -> D:
    ...


================================================
FILE: examples/sync_server.py
================================================
from typing import Generator

import uvicorn
from typing_extensions import TypedDict

from rpcpy import RPC

app = RPC()


@app.register
def none() -> None:
    return


@app.register
def sayhi(name: str) -> str:
    return f"hi {name}"


@app.register
def yield_data(max_num: int) -> Generator[int, None, None]:
    for i in range(max_num):
        yield i


D = TypedDict("D", {"key": str, "other-key": str})


@app.register
def query_dict(value: str) -> D:
    return {"key": value, "other-key": value}


if __name__ == "__main__":
    uvicorn.run(app, interface="wsgi", port=65432)


================================================
FILE: pyproject.toml
================================================
[tool.poetry]
authors = ["abersheeran <me@abersheeran.com>"]
description = "An fast and powerful RPC framework based on ASGI/WSGI."
license = "Apache-2.0"
name = "rpc.py"
readme = "README.md"
version = "0.6.0"

homepage = "https://github.com/abersheeran/rpc.py"
repository = "https://github.com/abersheeran/rpc.py"

classifiers = [
  "Programming Language :: Python :: 3",
  "Typing :: Typed",
  "Programming Language :: Python :: Implementation :: CPython",
  "Programming Language :: Python :: Implementation :: PyPy",
  "Topic :: Internet :: WWW/HTTP",
  "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
]

packages = [
  {include = "rpcpy"},
]

[tool.poetry.dependencies]
python = "^3.7"

baize = "*"
cbor2 = {version = "^5.2.0", optional = true}
httpx = {version = ">=0.22,<0.24", optional = true}# for client and test
msgpack = {version = "^1.0.0", optional = true}
pydantic = {version = "^1.9", optional = true}# for openapi docs
typing-extensions = {version = "*", python = "<3.8"}

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

[tool.poetry.dev-dependencies]
black = {version = "*", allow-prereleases = true}
flake8 = "*"
isort = "*"
mypy = "*"
pytest = "*"
pytest-asyncio = "*"
pytest-cov = "*"

[tool.black]
line-length = 91

[tool.isort]
profile = "black"

[tool.pytest.ini_options]
asyncio_mode = "auto"

[tool.coverage.run]
omit = ["*/.venv/*", "*/tests/*"]

[tool.coverage.report]
exclude_lines = [
  "pragma: no cover",
  "raise NotImplementedError",
  "raise RuntimeError",
  "if False:",
  "assert False",
  "if typing.TYPE_CHECKING:",
  "if TYPE_CHECKING:",
  "@typing.overload",
]
show_missing = true
skip_covered = true

[build-system]
build-backend = "poetry.masonry.api"
requires = ["poetry>=0.12"]


================================================
FILE: rpcpy/__init__.py
================================================
from .application import RPC

__all__ = ["RPC"]


================================================
FILE: rpcpy/__version__.py
================================================
VERSION = (0, 6, 0)

__version__ = ".".join(map(str, VERSION))


================================================
FILE: rpcpy/application.py
================================================
from __future__ import annotations

import copy
import inspect
import json
import sys
import typing
from base64 import b64encode
from collections.abc import AsyncGenerator, Generator

if sys.version_info[:2] < (3, 8):
    from typing_extensions import Literal, TypedDict
else:
    from typing import Literal, TypedDict

from baize.asgi import PlainTextResponse as AsgiResponse
from baize.asgi import Request as AsgiRequest
from baize.asgi import SendEventResponse as AsgiEventResponse
from baize.typing import (
    ASGIApp,
    Environ,
    Receive,
    Scope,
    Send,
    ServerSentEvent,
    StartResponse,
    WSGIApp,
)
from baize.wsgi import PlainTextResponse as WsgiResponse
from baize.wsgi import Request as WsgiRequest
from baize.wsgi import SendEventResponse as WsgiEventResponse

from rpcpy.exceptions import CallbackError, SerializerNotFound
from rpcpy.openapi import TEMPLATE as OPENAPI_TEMPLATE
from rpcpy.openapi import (
    ValidationError,
    create_model,
    is_typed_dict_type,
    parse_typed_dict,
    set_type_model,
)
from rpcpy.serializers import (
    SERIALIZER_NAMES,
    SERIALIZER_TYPES,
    BaseSerializer,
    JSONSerializer,
    get_serializer,
)

__all__ = ["RPC", "WsgiRPC", "AsgiRPC"]

Callable = typing.TypeVar("Callable", bound=typing.Callable)


class RPCMeta(type):
    def __call__(cls, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
        mode = kwargs.get("mode", "WSGI")
        assert mode in ("WSGI", "ASGI"), "mode must be in ('WSGI', 'ASGI')"

        if cls.__name__ == "RPC":
            if mode == "WSGI":
                return WsgiRPC(*args, **kwargs)

            if mode == "ASGI":
                return AsgiRPC(*args, **kwargs)

        return super().__call__(*args, **kwargs)


OpenAPI = TypedDict("OpenAPI", {"title": str, "description": str, "version": str})


class RPC(metaclass=RPCMeta):
    def __init__(
        self,
        *,
        mode: Literal["WSGI", "ASGI"] = "WSGI",
        prefix: str = "/",
        response_serializer: BaseSerializer = JSONSerializer(),
        openapi: typing.Optional[OpenAPI] = None,
    ) -> None:
        assert prefix.startswith("/") and prefix.endswith("/")
        self.callbacks: typing.Dict[str, typing.Callable] = {}
        self.prefix = prefix
        self.response_serializer = response_serializer
        self.openapi = openapi

    def register(self, func: Callable) -> Callable:
        self.callbacks[func.__name__] = func
        set_type_model(func)
        return func

    def get_openapi_docs(self) -> dict:
        openapi: typing.Dict[str, typing.Any] = {
            "openapi": "3.0.0",
            "info": copy.deepcopy(self.openapi) or {},
            "paths": {},
        }
        openapi["definitions"] = definitions = {}

        for name, callback in self.callbacks.items():
            _ = {}
            # summary and description
            doc = callback.__doc__
            if isinstance(doc, str):
                _.update(
                    zip(
                        ("summary", "description"),
                        map(lambda i: i.strip(), doc.strip().split("\n\n", 1)),
                    )
                )
            _["parameters"] = [
                {
                    "name": "content-type",
                    "in": "header",
                    "description": "At least one of serializer and content-type must be used"
                    " so that the server can know which serializer is used to parse the data.",
                    "required": True,
                    "schema": {
                        "type": "string",
                        "enum": [serializer_type for serializer_type in SERIALIZER_TYPES],
                    },
                },
                {
                    "name": "serializer",
                    "in": "header",
                    "description": "At least one of serializer and content-type must be used"
                    " so that the server can know which serializer is used to parse the data.",
                    "required": True,
                    "schema": {
                        "type": "string",
                        "enum": [serializer_name for serializer_name in SERIALIZER_NAMES],
                    },
                },
            ]
            # request body
            body_model = getattr(callback, "__body_model__", None)
            if body_model:
                _schema = copy.deepcopy(body_model.schema())
                definitions.update(_schema.pop("definitions", {}))
                del _schema["title"]
                _["requestBody"] = {
                    "required": True,
                    "content": {
                        serializer_type: {"schema": _schema}
                        for serializer_type in SERIALIZER_TYPES
                    },
                }
            # response & only 200
            sig = inspect.signature(callback)
            if sig.return_annotation != sig.empty:
                content_type = self.response_serializer.content_type
                return_annotation = sig.return_annotation
                if getattr(sig.return_annotation, "__origin__", None) in (
                    Generator,
                    AsyncGenerator,
                ):
                    content_type = "text/event-stream"
                    return_annotation = return_annotation.__args__[0]
                if is_typed_dict_type(return_annotation):
                    resp_model = parse_typed_dict(return_annotation)
                elif return_annotation is None:
                    resp_model = create_model(callback.__name__ + "-return")
                else:
                    resp_model = create_model(
                        callback.__name__ + "-return",
                        __root__=(return_annotation, ...),
                    )
                _schema = copy.deepcopy(resp_model.schema())
                definitions.update(_schema.pop("definitions", {}))
                del _schema["title"]
                _["responses"] = {
                    200: {
                        "content": {content_type: {"schema": _schema}},
                        "headers": {
                            "serializer": {
                                "schema": {
                                    "type": "string",
                                    "enum": [self.response_serializer.name],
                                },
                                "description": "Serializer Name",
                            }
                        },
                    }
                }
            if _:
                openapi["paths"][f"{self.prefix}{name}"] = {"post": _}
        return openapi

    @typing.overload
    def return_response_class(self, request: WsgiRequest) -> typing.Type[WsgiResponse]:
        pass

    @typing.overload
    def return_response_class(self, request: AsgiRequest) -> typing.Type[AsgiResponse]:
        pass

    def return_response_class(self, request):
        return AsgiResponse if isinstance(request, AsgiRequest) else WsgiResponse

    @typing.overload
    def respond_openapi(self, request: WsgiRequest) -> WsgiResponse | None:
        pass

    @typing.overload
    def respond_openapi(self, request: AsgiRequest) -> AsgiResponse | None:
        pass

    def respond_openapi(self, request):
        response_class = self.return_response_class(request)

        if self.openapi is not None and request.method == "GET":
            if request.url.path[len(self.prefix) :] == "openapi-docs":
                return response_class(OPENAPI_TEMPLATE, media_type="text/html")
            elif request.url.path[len(self.prefix) :] == "get-openapi-docs":
                return response_class(
                    json.dumps(self.get_openapi_docs(), ensure_ascii=False),
                    media_type="application/json",
                )

        return None

    def preprocess(
        self, request: WsgiRequest | AsgiRequest
    ) -> typing.Tuple[BaseSerializer, typing.Callable]:
        """
        Preprocess request
        """
        # check request method
        if request.method != "POST":
            raise CallbackError(content="", status_code=405)

        # check serializer
        try:
            serializer = get_serializer(request.headers)
        except SerializerNotFound as exception:
            raise CallbackError(content=str(exception), status_code=415)

        # check callback
        callback = self.callbacks.get(request.url.path[len(self.prefix) :], None)
        if callback is None:
            raise CallbackError(content="", status_code=404)

        return serializer, callback

    def preprocess_body(
        self, serializer: BaseSerializer, callback: typing.Callable, body: bytes
    ) -> typing.Dict[str, typing.Any]:
        """
        Preprocess request body
        """
        if not body:
            data = {}
        else:
            data = serializer.decode(body)

        if hasattr(callback, "__body_model__"):
            try:
                model = getattr(callback, "__body_model__")(**data)
            except ValidationError as exception:
                raise CallbackError(
                    status_code=422,
                    headers={"content-type": "application/json"},
                    content=exception.json(),
                )
            data = model.dict()

        return data

    def format_exception(self, exception: Exception) -> bytes:
        return self.response_serializer.encode(
            f"{exception.__class__.__qualname__}: {exception}"
        )


class WsgiRPC(RPC):
    def register(self, func: Callable) -> Callable:
        if inspect.iscoroutinefunction(func) or inspect.isasyncgenfunction(func):
            raise TypeError("WSGI mode can only register synchronization functions.")
        return super().register(func)

    def create_generator(
        self, generator: typing.Generator
    ) -> typing.Generator[ServerSentEvent, None, None]:
        try:
            for data in generator:
                yield {
                    "event": "yield",
                    "data": b64encode(self.response_serializer.encode(data)).decode(
                        "ascii"
                    ),
                }
        except Exception as exception:
            yield {
                "event": "exception",
                "data": b64encode(self.format_exception(exception)).decode("ascii"),
            }

    def on_call(
        self,
        callback: typing.Callable[..., typing.Any],
        data: typing.Dict[str, typing.Any],
    ) -> WsgiResponse | WsgiEventResponse:
        response: WsgiResponse | WsgiEventResponse
        try:
            result = callback(**data)
        except Exception as exception:
            message = self.format_exception(exception)
            response = WsgiResponse(
                message,
                headers={
                    "content-type": self.response_serializer.content_type,
                    "callback-status": "exception",
                },
            )
        else:
            if inspect.isgenerator(result):
                response = WsgiEventResponse(
                    self.create_generator(result), headers={"serializer-base": "base64"}
                )
            else:
                response = WsgiResponse(
                    self.response_serializer.encode(result),
                    headers={"content-type": self.response_serializer.content_type},
                )

        return response

    def __call__(
        self, environ: Environ, start_response: StartResponse
    ) -> typing.Iterable[bytes]:
        request = WsgiRequest(environ)
        response: WSGIApp | None = self.respond_openapi(request)
        if response is None:
            try:
                serializer, callback = self.preprocess(request)
                data = self.preprocess_body(serializer, callback, request.body)
            except CallbackError as exception:
                response = WsgiResponse(
                    content=exception.content or b"",
                    status_code=exception.status_code,
                    headers=exception.headers,
                )
            else:
                response = self.on_call(callback, data)
                response.headers["serializer"] = self.response_serializer.name
        return response(environ, start_response)


class AsgiRPC(RPC):
    def register(self, func: Callable) -> Callable:
        if not (inspect.iscoroutinefunction(func) or inspect.isasyncgenfunction(func)):
            raise TypeError("ASGI mode can only register asynchronous functions.")
        return super().register(func)

    async def create_generator(
        self, generator: typing.AsyncGenerator
    ) -> typing.AsyncGenerator[ServerSentEvent, None]:
        try:
            async for data in generator:
                yield {
                    "event": "yield",
                    "data": b64encode(self.response_serializer.encode(data)).decode(
                        "ascii"
                    ),
                }
        except Exception as exception:
            yield {
                "event": "exception",
                "data": b64encode(self.format_exception(exception)).decode("ascii"),
            }

    async def on_call(
        self,
        callback: typing.Callable[..., typing.Awaitable[typing.Any]],
        data: typing.Dict[str, typing.Any],
    ) -> AsgiResponse | AsgiEventResponse:
        response: AsgiResponse | AsgiEventResponse
        try:
            if inspect.isasyncgenfunction(callback):
                result = callback(**data)
            else:
                result = await callback(**data)
        except Exception as exception:
            message = self.format_exception(exception)
            response = AsgiResponse(
                message,
                headers={
                    "content-type": self.response_serializer.content_type,
                    "callback-status": "exception",
                },
            )
        else:
            if inspect.isasyncgen(result):
                response = AsgiEventResponse(
                    self.create_generator(result), headers={"serializer-base": "base64"}
                )
            else:
                response = AsgiResponse(
                    self.response_serializer.encode(result),
                    headers={"content-type": self.response_serializer.content_type},
                )

        return response

    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
        request = AsgiRequest(scope, receive, send)
        response: ASGIApp | None = self.respond_openapi(request)
        if response is None:
            try:
                serializer, callback = self.preprocess(request)
                data = self.preprocess_body(serializer, callback, await request.body)
            except CallbackError as exception:
                response = AsgiResponse(
                    content=exception.content or b"",
                    status_code=exception.status_code,
                    headers=exception.headers,
                )
            else:
                response = await self.on_call(callback, data)
                response.headers["serializer"] = self.response_serializer.name
        return await response(scope, receive, send)


================================================
FILE: rpcpy/client.py
================================================
from __future__ import annotations

import functools
import inspect
import typing
from base64 import b64decode

import httpx

from rpcpy.exceptions import RemoteCallError
from rpcpy.openapi import validate_arguments
from rpcpy.serializers import BaseSerializer, JSONSerializer, get_serializer

if typing.TYPE_CHECKING:
    from baize.typing import ServerSentEvent

__all__ = ["Client"]

Callable = typing.TypeVar("Callable", bound=typing.Callable)


class ClientMeta(type):
    def __call__(cls, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
        if cls.__name__ == "Client":
            if isinstance(args[0], httpx.Client):
                return SyncClient(*args, **kwargs)

            if isinstance(args[0], httpx.AsyncClient):
                return AsyncClient(*args, **kwargs)

            raise TypeError(
                "The parameter `client` must be an httpx.Client or httpx.AsyncClient object."
            )

        return super().__call__(*args, **kwargs)


class Client(metaclass=ClientMeta):
    def __init__(
        self,
        client: typing.Union[httpx.Client, httpx.AsyncClient],
        *,
        base_url: str,
        request_serializer: BaseSerializer = JSONSerializer(),
    ) -> None:
        assert base_url.endswith("/"), "base_url must be end with '/'"
        self.base_url = base_url
        self.client = client
        self.request_serializer = request_serializer

    def remote_call(self, func: Callable) -> Callable:
        return func

    def _get_url(self, func: typing.Callable) -> str:
        return self.base_url + func.__name__

    def _get_content(
        self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any
    ) -> bytes:
        sig = inspect.signature(func)
        bound_values = sig.bind(*args, **kwargs)
        parameters = dict(**bound_values.arguments)
        if parameters:
            return self.request_serializer.encode(parameters)
        else:
            return b""


class AsyncClient(Client):
    if typing.TYPE_CHECKING:
        client: httpx.AsyncClient

    def remote_call(self, func: Callable) -> Callable:
        if not (inspect.iscoroutinefunction(func) or inspect.isasyncgenfunction(func)):
            raise TypeError(
                "Asynchronous Client can only register asynchronous functions."
            )

        func = super().remote_call(func)
        url = self._get_url(func)

        if not inspect.isasyncgenfunction(func):

            @validate_arguments
            @functools.wraps(func)
            async def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
                post_content = self._get_content(func, *args, **kwargs)
                resp = await self.client.post(
                    url,
                    content=post_content,
                    headers={
                        "content-type": self.request_serializer.content_type,
                        "serializer": self.request_serializer.name,
                    },
                )
                resp.raise_for_status()
                content = get_serializer(resp.headers).decode(resp.content)
                if resp.headers.get("callback-status") == "exception":
                    raise RemoteCallError(content)
                else:
                    return content

        else:

            @validate_arguments
            @functools.wraps(func)
            async def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
                post_content = self._get_content(func, *args, **kwargs)
                async with self.client.stream(
                    "POST",
                    url,
                    content=post_content,
                    headers={
                        "content-type": self.request_serializer.content_type,
                        "serializer": self.request_serializer.name,
                    },
                ) as resp:
                    resp.raise_for_status()
                    sse_parser = ServerSentEventsParser()
                    serializer = get_serializer(resp.headers)
                    async for line in resp.aiter_lines():
                        event = sse_parser.feed(line)
                        if not event:
                            continue

                        if event["event"] == "yield":
                            yield serializer.decode(
                                b64decode(event["data"].encode("ascii"))
                            )
                        elif event["event"] == "exception":
                            raise RemoteCallError(
                                serializer.decode(b64decode(event["data"].encode("ascii")))
                            )
                        else:
                            raise RuntimeError(f"Unknown event type: {event['event']}")

        return typing.cast(Callable, wrapper)


class SyncClient(Client):
    if typing.TYPE_CHECKING:
        client: httpx.Client

    def remote_call(self, func: Callable) -> Callable:
        if inspect.iscoroutinefunction(func) or inspect.isasyncgenfunction(func):
            raise TypeError(
                "Synchronization Client can only register synchronization functions."
            )

        func = super().remote_call(func)
        url = self._get_url(func)

        if not inspect.isgeneratorfunction(func):

            @validate_arguments
            @functools.wraps(func)
            def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
                post_content = self._get_content(func, *args, **kwargs)
                resp = self.client.post(
                    url,
                    content=post_content,
                    headers={
                        "content-type": self.request_serializer.content_type,
                        "serializer": self.request_serializer.name,
                    },
                )
                resp.raise_for_status()
                content = get_serializer(resp.headers).decode(resp.content)
                if resp.headers.get("callback-status") == "exception":
                    raise RemoteCallError(content)
                else:
                    return content

        else:

            @validate_arguments
            @functools.wraps(func)
            def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
                post_content = self._get_content(func, *args, **kwargs)
                with self.client.stream(
                    "POST",
                    url,
                    content=post_content,
                    headers={
                        "content-type": self.request_serializer.content_type,
                        "serializer": self.request_serializer.name,
                    },
                ) as resp:
                    resp.raise_for_status()
                    sse_parser = ServerSentEventsParser()
                    serializer = get_serializer(resp.headers)
                    for line in resp.iter_lines():
                        event = sse_parser.feed(line)
                        if not event:
                            continue

                        if event["event"] == "yield":
                            yield serializer.decode(
                                b64decode(event["data"].encode("ascii"))
                            )
                        elif event["event"] == "exception":
                            raise RemoteCallError(
                                serializer.decode(b64decode(event["data"].encode("ascii")))
                            )
                        else:
                            raise RuntimeError(f"Unknown event type: {event['event']}")

        return typing.cast(Callable, wrapper)


class ServerSentEventsParser:
    def __init__(self) -> None:
        self.message: ServerSentEvent = {}

    def feed(self, line: str) -> ServerSentEvent | None:
        if line == "\n":  # event split line
            event = self.message
            self.message = {}
            return event

        if line[0] == ":":  # ignore comment
            return None

        try:
            key, value = map(str.strip, line.split(":", maxsplit=1))
        except ValueError:
            key = line.strip()
            value = ""

        if key not in ("data", "event", "id", "retry"):  # ignore undefined key
            return None

        if key == "data" and key in self.message:
            self.message["data"] = f'{self.message["data"]}\n{value}'
        elif key == "retry":
            try:
                self.message["retry"] = int(value)
            except ValueError:
                pass  # ignore non-integer retry value
        else:
            self.message[key] = value  # type: ignore[literal-required]

        return None


================================================
FILE: rpcpy/exceptions.py
================================================
from __future__ import annotations

from baize.exceptions import HTTPException


class SerializerNotFound(Exception):
    """
    Serializer not found
    """


class CallbackError(HTTPException[str]):
    """
    Callback error
    """


class RemoteCallError(Exception):
    """
    Remote call error
    """


================================================
FILE: rpcpy/openapi.py
================================================
from __future__ import annotations

import functools
import inspect
import typing
import warnings

__all__ = [
    "create_model",
    "validate_arguments",
    "set_type_model",
    "is_typed_dict_type",
    "parse_typed_dict",
    "TEMPLATE",
]

Callable = typing.TypeVar("Callable", bound=typing.Callable)

try:
    from pydantic import BaseModel, ValidationError, create_model
    from pydantic import validate_arguments as pydantic_validate_arguments

    # visit this issue
    # https://github.com/samuelcolvin/pydantic/issues/1205
    def validate_arguments(function: Callable) -> Callable:
        function = pydantic_validate_arguments(function)

        @functools.wraps(function)
        def change_exception(*args, **kwargs):
            try:
                return function(*args, **kwargs)
            except ValidationError as exception:
                type_error = TypeError(
                    "Failed to pass pydantic's type verification, please output"
                    " `.more_info` of this exception to view detailed information."
                )
                type_error.more_info = exception
                raise type_error

        return change_exception  # type: ignore

except ImportError:

    def create_model(*args, **kwargs):  # type: ignore
        raise NotImplementedError("Need install `pydantic` from pypi.")

    def validate_arguments(function: Callable) -> Callable:
        return function

    class ValidationError(Exception):  # type: ignore
        """
        Just for import
        """

    if typing.TYPE_CHECKING:
        from pydantic import BaseModel


def set_type_model(func: Callable) -> Callable:
    """
    try generate request body model from type hint and default value
    """
    sig = inspect.signature(func)
    field_definitions: typing.Dict[str, typing.Any] = {}
    for name, parameter in sig.parameters.items():
        if parameter.annotation == parameter.empty:
            # raise ValueError(
            #     f"You must specify the type for the parameter {func.__name__}:{name}."
            # )
            return func  # Maybe the type hint should be mandatory? I'm not sure.
        if parameter.default == parameter.empty:
            field_definitions[name] = (parameter.annotation, ...)
        else:
            field_definitions[name] = (parameter.annotation, parameter.default)
    if field_definitions:
        try:
            body_model: typing.Type[BaseModel] = create_model(
                func.__name__, **field_definitions
            )
            setattr(func, "__body_model__", body_model)
        except NotImplementedError:
            message = (
                "If you wanna using type hint "
                "to create OpenAPI docs or convert type, "
                "please install `pydantic` from pypi."
            )
            warnings.warn(message, ImportWarning)
    return func


def is_typed_dict_type(type_) -> bool:
    return (
        isinstance(type_, type)
        and issubclass(type_, dict)
        and getattr(type_, "__annotations__", False)
    )


def parse_typed_dict(typed_dict) -> typing.Type[BaseModel]:
    """
    parse `TypedDict` to generate `pydantic.BaseModel`
    """
    annotations = {}
    for name, field in typed_dict.__annotations__.items():
        if is_typed_dict_type(field):
            annotations[name] = (parse_typed_dict(field), ...)
        else:
            default_value = getattr(typed_dict, name, ...)
            annotations[name] = (field, default_value)

    return create_model(typed_dict.__name__, **annotations)  # type: ignore


TEMPLATE = """<!DOCTYPE html>
<html>

<head>
    <link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.30.0/swagger-ui.css">
    <title>OpenAPI Docs</title>
</head>

<body>
    <div id="swagger-ui"></div>
    <script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.30.0/swagger-ui-bundle.js"></script>
    <script>
        const ui = SwaggerUIBundle({
            url: './get-openapi-docs',
            dom_id: '#swagger-ui',
            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIBundle.SwaggerUIStandalonePreset
            ],
            layout: "BaseLayout",
            deepLinking: true,
            showExtensions: true,
            showCommonExtensions: true
        })

    </script>
</body>

</html>
"""


================================================
FILE: rpcpy/serializers.py
================================================
import json
import pickle
import typing
from abc import ABCMeta, abstractmethod

try:
    import msgpack
except ImportError:  # pragma: no cover
    msgpack = None  # type: ignore

try:
    import cbor2 as cbor
except ImportError:  # pragma: no cover
    cbor = None  # type: ignore

from rpcpy.exceptions import SerializerNotFound


class BaseSerializer(metaclass=ABCMeta):
    """
    Base Serializer
    """

    name: str
    content_type: str

    @abstractmethod
    def encode(self, data: typing.Any) -> bytes:
        raise NotImplementedError()

    @abstractmethod
    def decode(self, raw_data: bytes) -> typing.Any:
        raise NotImplementedError()


class JSONSerializer(BaseSerializer):
    name = "json"
    content_type = "application/json"

    def __init__(
        self,
        default_encode: typing.Callable = None,
        default_decode: typing.Callable = None,
    ) -> None:
        self.default_encode = default_encode
        self.default_decode = default_decode

    def encode(self, data: typing.Any) -> bytes:
        return json.dumps(
            data,
            ensure_ascii=False,
            default=self.default_encode,
        ).encode("utf8")

    def decode(self, data: bytes) -> typing.Any:
        return json.loads(
            data.decode("utf8"),
            object_hook=self.default_decode,
        )


class PickleSerializer(BaseSerializer):
    name = "pickle"
    content_type = "application/x-pickle"

    def encode(self, data: typing.Any) -> bytes:
        return pickle.dumps(data)

    def decode(self, data: bytes) -> typing.Any:
        return pickle.loads(data)


class MsgpackSerializer(BaseSerializer):
    """
    Msgpack: https://github.com/msgpack/msgpack-python
    """

    name = "msgpack"
    content_type = "application/x-msgpack"

    def __init__(
        self,
        default_encode: typing.Callable = None,
        default_decode: typing.Callable = None,
    ) -> None:
        self.default_encode = default_encode
        self.default_decode = default_decode

    def encode(self, data: typing.Any) -> bytes:
        return msgpack.packb(data, default=self.default_encode)

    def decode(self, data: bytes) -> typing.Any:
        return msgpack.unpackb(data, object_hook=self.default_decode)


class CBORSerializer(BaseSerializer):
    """
    CBOR: https://tools.ietf.org/html/rfc7049
    """

    name = "cbor"
    content_type = "application/x-cbor"

    def encode(self, data: typing.Any) -> bytes:
        return cbor.dumps(data)

    def decode(self, data: bytes) -> typing.Any:
        return cbor.loads(data)


# Since the release of pickle to the external network may lead to
# arbitrary code execution vulnerabilities, this serialization
# method is not enabled by default. It is recommended to turn it on
# when there is physical isolation from the outside.

SERIALIZER_NAMES = {
    JSONSerializer.name: JSONSerializer(),
    # PickleSerializer.name: PickleSerializer(),
    MsgpackSerializer.name: MsgpackSerializer(),
    CBORSerializer.name: CBORSerializer(),
}

SERIALIZER_TYPES = {
    JSONSerializer.content_type: JSONSerializer(),
    # PickleSerializer.content_type: PickleSerializer(),
    MsgpackSerializer.content_type: MsgpackSerializer(),
    CBORSerializer.content_type: CBORSerializer(),
}


def get_serializer(headers: typing.Mapping) -> BaseSerializer:
    """
    parse header and try find serializer
    """
    serializer_name = headers.get("serializer", None)
    if serializer_name:
        if serializer_name not in SERIALIZER_NAMES:
            raise SerializerNotFound(f"Serializer `{serializer_name}` not found")
        return SERIALIZER_NAMES[serializer_name]

    serializer_type = headers.get("content-type", None)
    if serializer_type:
        if serializer_type not in SERIALIZER_TYPES:
            raise SerializerNotFound(f"Serializer for `{serializer_type}` not found")
        return SERIALIZER_TYPES[serializer_type]

    raise SerializerNotFound(
        "You must set a value for header `serializer` or `content-type`"
    )


================================================
FILE: script/check.py
================================================
import subprocess

source_dirs = "rpcpy tests"
subprocess.check_call(f"isort --check --diff {source_dirs}", shell=True)
subprocess.check_call(f"black --check --diff {source_dirs}", shell=True)
subprocess.check_call(f"flake8 --ignore W503,E203,E501,E731 {source_dirs}", shell=True)
subprocess.check_call(f"mypy --ignore-missing-imports {source_dirs}", shell=True)


================================================
FILE: script/upload.py
================================================
import os
import subprocess

here = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

package_name = "rpcpy"


def get_version(package: str = package_name) -> str:
    """
    Return version.
    """
    _globals: dict = {}
    with open(os.path.join(here, package, "__version__.py")) as f:
        exec(f.read(), _globals)

    return _globals["__version__"]


os.chdir(here)
subprocess.check_call(f"poetry version {get_version()}", shell=True)
subprocess.check_call(f"git add {package_name}/__version__.py pyproject.toml", shell=True)
subprocess.check_call(f'git commit -m "v{get_version()}"', shell=True)
subprocess.check_call("git push", shell=True)
subprocess.check_call("git tag v{0}".format(get_version()), shell=True)
subprocess.check_call("git push --tags", shell=True)


================================================
FILE: tests/__init__.py
================================================


================================================
FILE: tests/test_application.py
================================================
import asyncio
import json
import sys
import time
from typing import AsyncGenerator, Generator

if sys.version_info[:2] < (3, 8):
    from typing_extensions import TypedDict
else:
    from typing import TypedDict

import httpx
import pytest

from rpcpy.application import RPC, AsgiRPC, WsgiRPC
from rpcpy.serializers import SERIALIZER_NAMES, SERIALIZER_TYPES


def test_wsgirpc():
    rpc = RPC()
    assert isinstance(rpc, WsgiRPC)

    @rpc.register
    def sayhi(name: str) -> str:
        return f"hi {name}"

    with pytest.raises(
        TypeError, match="WSGI mode can only register synchronization functions."
    ):

        @rpc.register
        async def async_sayhi(name: str) -> str:
            return f"hi {name}"

    @rpc.register
    def sayhi_without_type_hint(name):
        return f"hi {name}"

    with httpx.Client(app=rpc, base_url="http://testServer/") as client:
        assert client.get("/openapi-docs").status_code == 405
        assert client.post("/sayhi", data={"name": "Aber"}).status_code == 415
        assert client.post("/sayhi", json={"name": "Aber"}).status_code == 200
        assert (
            client.post("/sayhi_without_type_hint", json={"name": "Aber"})
        ).status_code == 200
        assert (
            client.post("/sayhi", content=json.dumps({"name": "Aber"})).status_code == 415
        )
        assert (
            client.post(
                "/sayhi",
                content=json.dumps({"name": "Aber"}).encode("utf8"),
                headers={"serializer": "application/json"},
            ).status_code
            == 415
        )
        assert (
            client.post(
                "/sayhi",
                content=json.dumps({"name": "Aber"}).encode("utf8"),
                headers={"content-type": "", "serializer": "json"},
            ).status_code
            == 200
        )
        assert client.post("/non-exists", json={"name": "Aber"}).status_code == 404


@pytest.mark.asyncio
async def test_asgirpc():
    rpc = RPC(mode="ASGI")
    assert isinstance(rpc, AsgiRPC)

    @rpc.register
    async def sayhi(name: str) -> str:
        return f"hi {name}"

    @rpc.register
    async def sayhi_without_type_hint(name):
        return f"hi {name}"

    with pytest.raises(
        TypeError, match="ASGI mode can only register asynchronous functions."
    ):

        @rpc.register
        def sync_sayhi(name: str) -> str:
            return f"hi {name}"

    async with httpx.AsyncClient(app=rpc, base_url="http://testServer/") as client:
        assert (await client.get("/openapi-docs")).status_code == 405
        assert (await client.post("/sayhi", data={"name": "Aber"})).status_code == 415
        assert (await client.post("/sayhi", json={"name": "Aber"})).status_code == 200
        assert (
            await client.post("/sayhi_without_type_hint", json={"name": "Aber"})
        ).status_code == 200
        assert (
            await client.post(
                "/sayhi",
                content=json.dumps({"name": "Aber"}).encode("utf8"),
                headers={"serializer": "application/json"},
            )
        ).status_code == 415
        assert (
            await client.post(
                "/sayhi",
                content=json.dumps({"name": "Aber"}).encode("utf8"),
                headers={"content-type": "", "serializer": "json"},
            )
        ).status_code == 200
        assert (await client.post("/non-exists", json={"name": "Aber"})).status_code == 404


@pytest.mark.skipif("pydantic" in sys.modules, reason="Installed pydantic")
def test_wsgi_openapi_without_pydantic():
    rpc = RPC(openapi={"title": "Title", "description": "Description", "version": "v1"})

    @rpc.register
    def sayhi(name: str) -> str:
        """
        say hi with name
        """
        return f"hi {name}"

    with pytest.raises(NotImplementedError):
        rpc.get_openapi_docs()


@pytest.mark.skipif("pydantic" in sys.modules, reason="Installed pydantic")
@pytest.mark.asyncio
async def test_asgi_openapi_without_pydantic():
    rpc = RPC(
        mode="ASGI",
        openapi={"title": "Title", "description": "Description", "version": "v1"},
    )

    @rpc.register
    async def sayhi(name: str) -> str:
        """
        say hi with name
        """
        return f"hi {name}"

    with pytest.raises(NotImplementedError):
        rpc.get_openapi_docs()


@pytest.mark.skipif("pydantic" not in sys.modules, reason="Missing pydantic")
def test_wsgi_openapi():
    rpc = RPC(openapi={"title": "Title", "description": "Description", "version": "v1"})

    @rpc.register
    def none() -> None:
        return None

    @rpc.register
    def sayhi(name: str) -> str:
        """
        say hi with name
        """
        return f"hi {name}"

    class DNSRecord(TypedDict):
        record: str
        ttl: int

    class DNS(TypedDict):
        dns_type: str
        host: str
        result: DNSRecord

    @rpc.register
    def query_dns(dns_type: str, host: str) -> DNS:
        return {"dns_type": dns_type, "host": host, "result": {"record": "", "ttl": 0}}

    @rpc.register
    def timestamp() -> Generator[int, None, None]:
        while True:
            yield int(time.time())
            time.sleep(1)

    assert rpc.get_openapi_docs() == OPENAPI_DOCS

    with httpx.Client(app=rpc, base_url="http://testServer/") as client:
        assert client.get("/openapi-docs").status_code == 200
        assert client.get("/get-openapi-docs").status_code == 200

        assert (
            client.post(
                "/sayhi",
                content=json.dumps({"name0": "Aber"}).encode("utf8"),
                headers={"content-type": "", "serializer": "json"},
            )
        ).status_code == 422


@pytest.mark.skipif("pydantic" not in sys.modules, reason="Missing pydantic")
@pytest.mark.asyncio
async def test_asgi_openapi():
    rpc = RPC(
        mode="ASGI",
        openapi={"title": "Title", "description": "Description", "version": "v1"},
    )

    @rpc.register
    async def none() -> None:
        return None

    @rpc.register
    async def sayhi(name: str) -> str:
        """
        say hi with name
        """
        return f"hi {name}"

    DNSRecord = TypedDict("DNSRecord", {"record": str, "ttl": int})
    DNS = TypedDict("DNS", {"dns_type": str, "host": str, "result": DNSRecord})

    @rpc.register
    async def query_dns(dns_type: str, host: str) -> DNS:
        return {"dns_type": dns_type, "host": host, "result": {"record": "", "ttl": 0}}

    @rpc.register
    async def timestamp() -> AsyncGenerator[int, None]:
        while True:
            yield int(time.time())
            await asyncio.sleep(1)

    assert rpc.get_openapi_docs() == OPENAPI_DOCS

    async with httpx.AsyncClient(app=rpc, base_url="http://testServer/") as client:
        assert (await client.get("/openapi-docs")).status_code == 200
        assert (await client.get("/get-openapi-docs")).status_code == 200

        assert (
            await client.post(
                "/sayhi",
                content=json.dumps({"name0": "Aber"}).encode("utf8"),
                headers={"content-type": "", "serializer": "json"},
            )
        ).status_code == 422


DEFAULT_PARAMETERS = [
    {
        "name": "content-type",
        "in": "header",
        "description": "At least one of serializer and content-type must be used"
        " so that the server can know which serializer is used to parse the data.",
        "required": True,
        "schema": {
            "type": "string",
            "enum": [serializer_type for serializer_type in SERIALIZER_TYPES],
        },
    },
    {
        "name": "serializer",
        "in": "header",
        "description": "At least one of serializer and content-type must be used"
        " so that the server can know which serializer is used to parse the data.",
        "required": True,
        "schema": {
            "type": "string",
            "enum": [serializer_name for serializer_name in SERIALIZER_NAMES],
        },
    },
]

OPENAPI_DOCS = {
    "openapi": "3.0.0",
    "info": {"title": "Title", "description": "Description", "version": "v1"},
    "paths": {
        "/none": {
            "post": {
                "parameters": DEFAULT_PARAMETERS,
                "responses": {
                    200: {
                        "content": {
                            "application/json": {
                                "schema": {"type": "object", "properties": {}},
                            }
                        },
                        "headers": {
                            "serializer": {
                                "schema": {
                                    "type": "string",
                                    "enum": ["json"],
                                },
                                "description": "Serializer Name",
                            }
                        },
                    }
                },
            }
        },
        "/sayhi": {
            "post": {
                "summary": "say hi with name",
                "parameters": DEFAULT_PARAMETERS,
                "requestBody": {
                    "required": True,
                    "content": {
                        serializer_type: {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "title": "Name",
                                        "type": "string",
                                    }
                                },
                                "required": ["name"],
                            }
                        }
                        for serializer_type in SERIALIZER_TYPES
                    },
                },
                "responses": {
                    200: {
                        "content": {"application/json": {"schema": {"type": "string"}}},
                        "headers": {
                            "serializer": {
                                "schema": {
                                    "type": "string",
                                    "enum": ["json"],
                                },
                                "description": "Serializer Name",
                            }
                        },
                    }
                },
            }
        },
        "/query_dns": {
            "post": {
                "parameters": DEFAULT_PARAMETERS,
                "requestBody": {
                    "required": True,
                    "content": {
                        serializer_type: {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "dns_type": {
                                        "title": "Dns Type",
                                        "type": "string",
                                    },
                                    "host": {
                                        "title": "Host",
                                        "type": "string",
                                    },
                                },
                                "required": ["dns_type", "host"],
                            }
                        }
                        for serializer_type in SERIALIZER_TYPES
                    },
                },
                "responses": {
                    200: {
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "dns_type": {
                                            "title": "Dns Type",
                                            "type": "string",
                                        },
                                        "host": {
                                            "title": "Host",
                                            "type": "string",
                                        },
                                        "result": {"$ref": "#/definitions/DNSRecord"},
                                    },
                                    "required": ["dns_type", "host", "result"],
                                }
                            }
                        },
                        "headers": {
                            "serializer": {
                                "schema": {
                                    "type": "string",
                                    "enum": ["json"],
                                },
                                "description": "Serializer Name",
                            }
                        },
                    }
                },
            }
        },
        "/timestamp": {
            "post": {
                "parameters": DEFAULT_PARAMETERS,
                "responses": {
                    200: {
                        "content": {"text/event-stream": {"schema": {"type": "integer"}}},
                        "headers": {
                            "serializer": {
                                "schema": {"type": "string", "enum": ["json"]},
                                "description": "Serializer Name",
                            }
                        },
                    }
                },
            }
        },
    },
    "definitions": {
        "DNSRecord": {
            "title": "DNSRecord",
            "type": "object",
            "properties": {
                "record": {"title": "Record", "type": "string"},
                "ttl": {"title": "Ttl", "type": "integer"},
            },
            "required": ["record", "ttl"],
        }
    },
}


================================================
FILE: tests/test_client.py
================================================
import asyncio
import time
from typing import AsyncGenerator, Generator

import httpx
import pytest

from rpcpy import RPC
from rpcpy.client import Client, ServerSentEventsParser
from rpcpy.exceptions import RemoteCallError


@pytest.fixture
def wsgi_app():
    app = RPC()

    @app.register
    def none() -> None:
        return

    @app.register
    def sayhi(name: str) -> str:
        return f"hi {name}"

    @app.register
    def yield_data(max_num: int) -> Generator[int, None, None]:
        for i in range(max_num):
            time.sleep(1)
            yield i

    @app.register
    def exception() -> str:
        raise ValueError("Message")

    @app.register
    def exception_in_g() -> Generator[str, None, None]:
        yield "Message"
        raise TypeError("Message")

    return app


@pytest.fixture
def asgi_app():
    app = RPC(mode="ASGI")

    @app.register
    async def none() -> None:
        return

    @app.register
    async def sayhi(name: str) -> str:
        return f"hi {name}"

    @app.register
    async def yield_data(max_num: int) -> AsyncGenerator[int, None]:
        for i in range(max_num):
            await asyncio.sleep(1)
            yield i

    @app.register
    async def exception() -> str:
        raise ValueError("Message")

    @app.register
    async def exception_in_g() -> AsyncGenerator[str, None]:
        yield "Message"
        raise TypeError("Message")

    return app


@pytest.fixture
def sync_client(wsgi_app):
    httpx_client = httpx.Client(app=wsgi_app)
    try:
        yield Client(httpx_client, base_url="http://testserver/")
    finally:
        httpx_client.close()


@pytest.fixture
def async_client(asgi_app):
    httpx_client = httpx.AsyncClient(app=asgi_app)
    try:
        yield Client(httpx_client, base_url="http://testserver/")
    finally:
        asyncio.get_event_loop().run_until_complete(httpx_client.aclose())


def test_sync_client(sync_client):
    @sync_client.remote_call
    def sayhi(name: str) -> str:
        ...

    assert sayhi("rpc.py") == "hi rpc.py"

    with pytest.raises(
        TypeError,
        match="Synchronization Client can only register synchronization functions.",
    ):

        @sync_client.remote_call
        async def sayhi(name: str) -> str:
            ...

    @sync_client.remote_call
    def yield_data(max_num: int):
        yield

    index = 0
    for i in yield_data(5):
        assert index == i
        index += 1

    @sync_client.remote_call
    def exception() -> str:
        ...

    with pytest.raises(RemoteCallError, match="ValueError: Message"):
        exception()

    @sync_client.remote_call
    def exception_in_g() -> Generator[str, None, None]:
        yield  # type: ignore

    with pytest.raises(RemoteCallError, match="TypeError: Message"):
        for msg in exception_in_g():
            assert msg == "Message"


@pytest.mark.asyncio
async def test_async_client(async_client):
    @async_client.remote_call
    async def sayhi(name: str) -> str:
        ...

    assert await sayhi("rpc.py") == "hi rpc.py"

    with pytest.raises(
        TypeError,
        match="Asynchronous Client can only register asynchronous functions.",
    ):

        @async_client.remote_call
        def sayhi(name: str) -> str:
            ...

    @async_client.remote_call
    async def yield_data(max_num: int):
        yield

    index = 0
    async for i in yield_data(5):
        assert index == i
        index += 1

    @async_client.remote_call
    async def exception() -> str:
        ...

    with pytest.raises(RemoteCallError, match="ValueError: Message"):
        await exception()

    @async_client.remote_call
    async def exception_in_g() -> AsyncGenerator[str, None]:
        yield  # type: ignore

    with pytest.raises(RemoteCallError, match="TypeError: Message"):
        async for msg in exception_in_g():
            assert msg == "Message"


def test_none(sync_client):
    @sync_client.remote_call
    def none() -> None:
        ...

    assert none() is None

    with pytest.raises(TypeError):
        none("hi")


@pytest.mark.asyncio
async def test_async_none(async_client):
    @async_client.remote_call
    async def none() -> None:
        ...

    assert await none() is None

    with pytest.raises(TypeError):
        await none("hi")


def test_invalid_client():
    with pytest.raises(
        TypeError,
        match="The parameter `client` must be an httpx.Client or httpx.AsyncClient object.",
    ):
        Client(0)


def test_sse_parser():
    parser = ServerSentEventsParser()

    parser.feed("data: hello\n")
    assert parser.feed("\n") == {"data": "hello"}

    parser.feed("data: hello\n")
    parser.feed("data: world\n")
    assert parser.feed("\n") == {"data": "hello\nworld"}

    parser.feed(": ping\n")
    assert parser.feed("\n") == {}

    parser.feed("retry: 1\n")
    assert parser.feed("\n") == {"retry": 1}

    parser.feed("retry: p1\n")
    assert parser.feed("\n") == {}

    parser.feed("undefined")
    assert parser.feed("\n") == {}

    parser.feed("event")
    assert parser.feed("\n") == {"event": ""}


================================================
FILE: tests/test_serializers.py
================================================
import pytest

from rpcpy.serializers import (
    CBORSerializer,
    JSONSerializer,
    MsgpackSerializer,
    PickleSerializer,
)


@pytest.mark.parametrize(
    "serializer",
    [JSONSerializer(), PickleSerializer(), MsgpackSerializer(), CBORSerializer()],
)
@pytest.mark.parametrize(
    "data",
    ["123", "中文", 1, 0, 1239.123, ["123", 1, 123.98], {"a": 1}],
)
def test_serializer(serializer, data):
    _ = serializer.encode(data)
    assert isinstance(_, bytes)
    assert serializer.decode(_) == data


================================================
FILE: tests/test_version.py
================================================
from rpcpy.__version__ import VERSION, __version__


def test_version():
    VERSION[0]
    VERSION[1]
    VERSION[2]

    assert isinstance(__version__, str)
Download .txt
gitextract_2emp0qll/

├── .editorconfig
├── .gitattributes
├── .github/
│   ├── FUNDING.yml
│   └── workflows/
│       └── ci.yml
├── .gitignore
├── LICENSE
├── README.md
├── examples/
│   ├── async_client.py
│   ├── async_server.py
│   ├── sync_client.py
│   └── sync_server.py
├── pyproject.toml
├── rpcpy/
│   ├── __init__.py
│   ├── __version__.py
│   ├── application.py
│   ├── client.py
│   ├── exceptions.py
│   ├── openapi.py
│   └── serializers.py
├── script/
│   ├── check.py
│   └── upload.py
└── tests/
    ├── __init__.py
    ├── test_application.py
    ├── test_client.py
    ├── test_serializers.py
    └── test_version.py
Download .txt
SYMBOL INDEX (102 symbols across 14 files)

FILE: examples/async_client.py
  function none (line 12) | async def none() -> None:
  function sayhi (line 17) | async def sayhi(name: str) -> str:
  function yield_data (line 22) | async def yield_data(max_num: int) -> AsyncGenerator[int, None]:
  function query_dict (line 30) | async def query_dict(value: str) -> D:

FILE: examples/async_server.py
  function none (line 12) | async def none() -> None:
  function sayhi (line 17) | async def sayhi(name: str) -> str:
  function yield_data (line 22) | async def yield_data(max_num: int) -> AsyncGenerator[int, None]:
  function query_dict (line 31) | async def query_dict(value: str) -> D:

FILE: examples/sync_client.py
  function none (line 12) | def none() -> None:
  function sayhi (line 17) | def sayhi(name: str) -> str:
  function yield_data (line 22) | def yield_data(max_num: int) -> Generator[int, None, None]:
  function query_dict (line 30) | def query_dict(value: str) -> D:

FILE: examples/sync_server.py
  function none (line 12) | def none() -> None:
  function sayhi (line 17) | def sayhi(name: str) -> str:
  function yield_data (line 22) | def yield_data(max_num: int) -> Generator[int, None, None]:
  function query_dict (line 31) | def query_dict(value: str) -> D:

FILE: rpcpy/application.py
  class RPCMeta (line 55) | class RPCMeta(type):
    method __call__ (line 56) | def __call__(cls, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
  class RPC (line 73) | class RPC(metaclass=RPCMeta):
    method __init__ (line 74) | def __init__(
    method register (line 88) | def register(self, func: Callable) -> Callable:
    method get_openapi_docs (line 93) | def get_openapi_docs(self) -> dict:
    method return_response_class (line 191) | def return_response_class(self, request: WsgiRequest) -> typing.Type[W...
    method return_response_class (line 195) | def return_response_class(self, request: AsgiRequest) -> typing.Type[A...
    method return_response_class (line 198) | def return_response_class(self, request):
    method respond_openapi (line 202) | def respond_openapi(self, request: WsgiRequest) -> WsgiResponse | None:
    method respond_openapi (line 206) | def respond_openapi(self, request: AsgiRequest) -> AsgiResponse | None:
    method respond_openapi (line 209) | def respond_openapi(self, request):
    method preprocess (line 223) | def preprocess(
    method preprocess_body (line 246) | def preprocess_body(
    method format_exception (line 270) | def format_exception(self, exception: Exception) -> bytes:
  class WsgiRPC (line 276) | class WsgiRPC(RPC):
    method register (line 277) | def register(self, func: Callable) -> Callable:
    method create_generator (line 282) | def create_generator(
    method on_call (line 299) | def on_call(
    method __call__ (line 329) | def __call__(
  class AsgiRPC (line 350) | class AsgiRPC(RPC):
    method register (line 351) | def register(self, func: Callable) -> Callable:
    method create_generator (line 356) | async def create_generator(
    method on_call (line 373) | async def on_call(
    method __call__ (line 406) | async def __call__(self, scope: Scope, receive: Receive, send: Send) -...

FILE: rpcpy/client.py
  class ClientMeta (line 22) | class ClientMeta(type):
    method __call__ (line 23) | def __call__(cls, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
  class Client (line 38) | class Client(metaclass=ClientMeta):
    method __init__ (line 39) | def __init__(
    method remote_call (line 51) | def remote_call(self, func: Callable) -> Callable:
    method _get_url (line 54) | def _get_url(self, func: typing.Callable) -> str:
    method _get_content (line 57) | def _get_content(
  class AsyncClient (line 69) | class AsyncClient(Client):
    method remote_call (line 73) | def remote_call(self, func: Callable) -> Callable:
  class SyncClient (line 140) | class SyncClient(Client):
    method remote_call (line 144) | def remote_call(self, func: Callable) -> Callable:
  class ServerSentEventsParser (line 211) | class ServerSentEventsParser:
    method __init__ (line 212) | def __init__(self) -> None:
    method feed (line 215) | def feed(self, line: str) -> ServerSentEvent | None:

FILE: rpcpy/exceptions.py
  class SerializerNotFound (line 6) | class SerializerNotFound(Exception):
  class CallbackError (line 12) | class CallbackError(HTTPException[str]):
  class RemoteCallError (line 18) | class RemoteCallError(Exception):

FILE: rpcpy/openapi.py
  function validate_arguments (line 25) | def validate_arguments(function: Callable) -> Callable:
  function create_model (line 44) | def create_model(*args, **kwargs):  # type: ignore
  function validate_arguments (line 47) | def validate_arguments(function: Callable) -> Callable:
  class ValidationError (line 50) | class ValidationError(Exception):  # type: ignore
  function set_type_model (line 59) | def set_type_model(func: Callable) -> Callable:
  function is_typed_dict_type (line 91) | def is_typed_dict_type(type_) -> bool:
  function parse_typed_dict (line 99) | def parse_typed_dict(typed_dict) -> typing.Type[BaseModel]:

FILE: rpcpy/serializers.py
  class BaseSerializer (line 19) | class BaseSerializer(metaclass=ABCMeta):
    method encode (line 28) | def encode(self, data: typing.Any) -> bytes:
    method decode (line 32) | def decode(self, raw_data: bytes) -> typing.Any:
  class JSONSerializer (line 36) | class JSONSerializer(BaseSerializer):
    method __init__ (line 40) | def __init__(
    method encode (line 48) | def encode(self, data: typing.Any) -> bytes:
    method decode (line 55) | def decode(self, data: bytes) -> typing.Any:
  class PickleSerializer (line 62) | class PickleSerializer(BaseSerializer):
    method encode (line 66) | def encode(self, data: typing.Any) -> bytes:
    method decode (line 69) | def decode(self, data: bytes) -> typing.Any:
  class MsgpackSerializer (line 73) | class MsgpackSerializer(BaseSerializer):
    method __init__ (line 81) | def __init__(
    method encode (line 89) | def encode(self, data: typing.Any) -> bytes:
    method decode (line 92) | def decode(self, data: bytes) -> typing.Any:
  class CBORSerializer (line 96) | class CBORSerializer(BaseSerializer):
    method encode (line 104) | def encode(self, data: typing.Any) -> bytes:
    method decode (line 107) | def decode(self, data: bytes) -> typing.Any:
  function get_serializer (line 131) | def get_serializer(headers: typing.Mapping) -> BaseSerializer:

FILE: script/upload.py
  function get_version (line 9) | def get_version(package: str = package_name) -> str:

FILE: tests/test_application.py
  function test_wsgirpc (line 19) | def test_wsgirpc():
  function test_asgirpc (line 69) | async def test_asgirpc():
  function test_wsgi_openapi_without_pydantic (line 114) | def test_wsgi_openapi_without_pydantic():
  function test_asgi_openapi_without_pydantic (line 130) | async def test_asgi_openapi_without_pydantic():
  function test_wsgi_openapi (line 148) | def test_wsgi_openapi():
  function test_asgi_openapi (line 198) | async def test_asgi_openapi():

FILE: tests/test_client.py
  function wsgi_app (line 14) | def wsgi_app():
  function asgi_app (line 44) | def asgi_app():
  function sync_client (line 74) | def sync_client(wsgi_app):
  function async_client (line 83) | def async_client(asgi_app):
  function test_sync_client (line 91) | def test_sync_client(sync_client):
  function test_async_client (line 133) | async def test_async_client(async_client):
  function test_none (line 174) | def test_none(sync_client):
  function test_async_none (line 186) | async def test_async_none(async_client):
  function test_invalid_client (line 197) | def test_invalid_client():
  function test_sse_parser (line 205) | def test_sse_parser():

FILE: tests/test_serializers.py
  function test_serializer (line 19) | def test_serializer(serializer, data):

FILE: tests/test_version.py
  function test_version (line 4) | def test_version():
Condensed preview — 26 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (85K chars).
[
  {
    "path": ".editorconfig",
    "chars": 260,
    "preview": "# EditorConfig is awesome: https://EditorConfig.org\n\n# top-most EditorConfig file\nroot = true\n\n[*]\nend_of_line = crlf\nin"
  },
  {
    "path": ".gitattributes",
    "chars": 12,
    "preview": "* text=auto\n"
  },
  {
    "path": ".github/FUNDING.yml",
    "chars": 157,
    "preview": "# These are supported funding model platforms\n\ncustom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'l"
  },
  {
    "path": ".github/workflows/ci.yml",
    "chars": 2761,
    "preview": "name: CI\n\non:\n  push:\n    branches:\n      - master\n    tags:\n      - \"*\"\n    paths:\n      - \"**.py\"\n      - \"!rpcpy/__ve"
  },
  {
    "path": ".gitignore",
    "chars": 1138,
    "preview": "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packagi"
  },
  {
    "path": "LICENSE",
    "chars": 11341,
    "preview": "                                 Apache License\n                           Version 2.0, January 2004\n                   "
  },
  {
    "path": "README.md",
    "chars": 5945,
    "preview": "# rpc.py\n\n[![Codecov](https://img.shields.io/codecov/c/github/abersheeran/rpc.py?style=flat-square)](https://codecov.io/"
  },
  {
    "path": "examples/async_client.py",
    "chars": 520,
    "preview": "from typing import AsyncGenerator\n\nimport httpx\nfrom typing_extensions import TypedDict\n\nfrom rpcpy.client import Client"
  },
  {
    "path": "examples/async_server.py",
    "chars": 626,
    "preview": "from typing import AsyncGenerator\n\nimport uvicorn\nfrom typing_extensions import TypedDict\n\nfrom rpcpy import RPC\n\napp = "
  },
  {
    "path": "examples/sync_client.py",
    "chars": 487,
    "preview": "from typing import Generator\n\nimport httpx\nfrom typing_extensions import TypedDict\n\nfrom rpcpy.client import Client\n\napp"
  },
  {
    "path": "examples/sync_server.py",
    "chars": 586,
    "preview": "from typing import Generator\n\nimport uvicorn\nfrom typing_extensions import TypedDict\n\nfrom rpcpy import RPC\n\napp = RPC()"
  },
  {
    "path": "pyproject.toml",
    "chars": 1832,
    "preview": "[tool.poetry]\nauthors = [\"abersheeran <me@abersheeran.com>\"]\ndescription = \"An fast and powerful RPC framework based on "
  },
  {
    "path": "rpcpy/__init__.py",
    "chars": 48,
    "preview": "from .application import RPC\n\n__all__ = [\"RPC\"]\n"
  },
  {
    "path": "rpcpy/__version__.py",
    "chars": 63,
    "preview": "VERSION = (0, 6, 0)\n\n__version__ = \".\".join(map(str, VERSION))\n"
  },
  {
    "path": "rpcpy/application.py",
    "chars": 15386,
    "preview": "from __future__ import annotations\n\nimport copy\nimport inspect\nimport json\nimport sys\nimport typing\nfrom base64 import b"
  },
  {
    "path": "rpcpy/client.py",
    "chars": 8794,
    "preview": "from __future__ import annotations\n\nimport functools\nimport inspect\nimport typing\nfrom base64 import b64decode\n\nimport h"
  },
  {
    "path": "rpcpy/exceptions.py",
    "chars": 311,
    "preview": "from __future__ import annotations\n\nfrom baize.exceptions import HTTPException\n\n\nclass SerializerNotFound(Exception):\n  "
  },
  {
    "path": "rpcpy/openapi.py",
    "chars": 4393,
    "preview": "from __future__ import annotations\n\nimport functools\nimport inspect\nimport typing\nimport warnings\n\n__all__ = [\n    \"crea"
  },
  {
    "path": "rpcpy/serializers.py",
    "chars": 4057,
    "preview": "import json\nimport pickle\nimport typing\nfrom abc import ABCMeta, abstractmethod\n\ntry:\n    import msgpack\nexcept ImportEr"
  },
  {
    "path": "script/check.py",
    "chars": 363,
    "preview": "import subprocess\n\nsource_dirs = \"rpcpy tests\"\nsubprocess.check_call(f\"isort --check --diff {source_dirs}\", shell=True)\n"
  },
  {
    "path": "script/upload.py",
    "chars": 790,
    "preview": "import os\nimport subprocess\n\nhere = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\npackage_name = \"rpcpy\"\n"
  },
  {
    "path": "tests/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "tests/test_application.py",
    "chars": 13981,
    "preview": "import asyncio\nimport json\nimport sys\nimport time\nfrom typing import AsyncGenerator, Generator\n\nif sys.version_info[:2] "
  },
  {
    "path": "tests/test_client.py",
    "chars": 5125,
    "preview": "import asyncio\nimport time\nfrom typing import AsyncGenerator, Generator\n\nimport httpx\nimport pytest\n\nfrom rpcpy import R"
  },
  {
    "path": "tests/test_serializers.py",
    "chars": 513,
    "preview": "import pytest\n\nfrom rpcpy.serializers import (\n    CBORSerializer,\n    JSONSerializer,\n    MsgpackSerializer,\n    Pickle"
  },
  {
    "path": "tests/test_version.py",
    "chars": 159,
    "preview": "from rpcpy.__version__ import VERSION, __version__\n\n\ndef test_version():\n    VERSION[0]\n    VERSION[1]\n    VERSION[2]\n\n "
  }
]

About this extraction

This page contains the full source code of the abersheeran/rpc.py GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 26 files (77.8 KB), approximately 17.7k tokens, and a symbol index with 102 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!