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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file py_grpcio-1.2.1.tar.gz
.
File metadata
- Download URL: py_grpcio-1.2.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02aa3e025d10def531f341ae6d1eddf78eb3daa2c92bf1e76b4d5f6207f84f4b |
|
MD5 | 938fb021ba9dd32bdefa0f2188eabc70 |
|
BLAKE2b-256 | 9d854badaf5f5453389f93cda2d8564ec8c1ffc0298b026b14899ab5732081c5 |
File details
Details for the file py_grpcio-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: py_grpcio-1.2.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 779ae804b110f553e2661d97c4d6b2c525654a5b4a19e2be3aeef7d49b5ba2d1 |
|
MD5 | b767185297f605ebeed1fc48183eed99 |
|
BLAKE2b-256 | 062cac12eb022bdda99229d429cabd475ae4615abbc790f2924f7f88a798192d |