Skip to main content

gRPC with autogen by Pydantic models

Project description

Pydantic & gRPC

py-grpcio is a microframework and high-level wrapper of grpcio to simplify work with the original library using abstractions, useful python objects and pydantic models.

Examples of use are given below and also duplicated in the example directory.


Install latest

pip install py-grpcio

Example

Models

Pydantic models that describe messages for client-server interaction.

from uuid import UUID, uuid4
from datetime import datetime

from pydantic import Field

from py_grpcio import Message


class PingRequest(Message):
    id: UUID = Field(default_factory=uuid4)


class PingResponse(Message):
    id: UUID
    timestamp: datetime = Field(default_factory=datetime.now)

Server

Basic implementation of gRPC services on the server side.

You need to describe the service abstractly and duplicate this service on the client side.

from abc import abstractmethod

from py_grpcio import BaseService

from example.server.core.models import PingRequest, PingResponse


class BaseExampleService(BaseService):
    @abstractmethod
    async def ping(self, request: PingRequest) -> PingResponse:
        ...

Full implementation of the gRPC service with methods.

from example.server.core import BaseExampleService, PingRequest, PingResponse

from py_grpcio import BaseServer


class ExampleService(BaseExampleService):
    async def ping(self, request: PingRequest) -> PingResponse:
        return PingResponse(id=request.id)


if __name__ == '__main__':
    server: BaseServer = BaseServer()
    server.add_service(service=ExampleService)
    server.run()

Note that on the client side, this class must be named the same as it is named in the full server-side implementation.

That is, if on the server we call the base class as BaseExampleService and the class with the implementation of methods as ExampleService, then on the client side the abstract service should be called ExampleService.

Client

from abc import abstractmethod

from py_grpcio import BaseService

from example.client.core.models import PingRequest, PingResponse


class ExampleService(BaseService):
    @abstractmethod
    async def ping(self, request: PingRequest) -> PingResponse:
        ...

Notes

  • You can use the library on the client side even if the server is implemented differently by simply describing it as an abstract service

  • The client can also be implemented using other libraries, the server that uses py-grpcio will still be able to accept such requests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_grpcio-1.1.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

py_grpcio-1.1.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file py_grpcio-1.1.1.tar.gz.

File metadata

  • Download URL: py_grpcio-1.1.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for py_grpcio-1.1.1.tar.gz
Algorithm Hash digest
SHA256 b56a59b29a46649a2bab00ecc0cc6b20457bde329f5c14f4ad1e7c6314d5945a
MD5 f930d37719b36dcdee380a2ec62164cf
BLAKE2b-256 42914463e734a34a93d51b48e42d7165288463b10a541b39c3e1d35391aa9614

See more details on using hashes here.

File details

Details for the file py_grpcio-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: py_grpcio-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for py_grpcio-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad46a44bccce8abfdb26fadead5eac64f019f040fb39a8fc97ff09272853eb6a
MD5 9714bb1e4304d7c9695be2930b5581f0
BLAKE2b-256 2ea3878d7369dca4f508537a3a4b43136f8adf68fdbde04012de24da101836e6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page