Skip to content

Commit

Permalink
added documentation for server-facing stubs (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
w4rum authored Jan 24, 2021
1 parent 1d54ef8 commit 8eea5fe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,36 @@ EchoStreamResponse(value='hello')
EchoStreamResponse(value='hello')
```

This project also produces server-facing stubs that can be used to implement a Python
gRPC server.
To use them, simply subclass the base class in the generated files and override the
service methods:

```python
from echo import EchoBase
from grpclib.server import Server
from typing import AsyncIterator


class EchoService(EchoBase):
async def echo(self, value: str, extra_times: int) -> "EchoResponse":
return value

async def echo_stream(
self, value: str, extra_times: int
) -> AsyncIterator["EchoStreamResponse"]:
for _ in range(extra_times):
yield value


async def start_server():
HOST = "127.0.0.1"
PORT = 1337
server = Server([EchoService()])
await server.start(HOST, PORT)
await server.serve_forever()
```

### JSON

Both serializing and parsing are supported to/from JSON and Python dictionaries using the following methods:
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Features:
- Generated messages are both binary & JSON serializable
- Messages use relevant python types, e.g. ``Enum``, ``datetime`` and ``timedelta``
objects
- ``async``/``await`` support for gRPC Clients
- ``async``/``await`` support for gRPC Clients and Servers
- Generates modern, readable, idiomatic python code

Contents:
Expand Down
32 changes: 31 additions & 1 deletion docs/quick-start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Async gRPC Support
++++++++++++++++++

The generated code includes `grpclib <https://grpclib.readthedocs.io/en/latest>`_ based
stub (client) classes for rpc services declared in the input proto files.
stub (client and server) classes for rpc services declared in the input proto files.
It is enabled by default.


Expand Down Expand Up @@ -160,6 +160,36 @@ The generated client can be used like so:
EchoStreamResponse(value='hello')
The server-facing stubs can be used to implement a Python
gRPC server.
To use them, simply subclass the base class in the generated files and override the
service methods:

.. code-block:: python
from echo import EchoBase
from grpclib.server import Server
from typing import AsyncIterator
class EchoService(EchoBase):
async def echo(self, value: str, extra_times: int) -> "EchoResponse":
return value
async def echo_stream(
self, value: str, extra_times: int
) -> AsyncIterator["EchoStreamResponse"]:
for _ in range(extra_times):
yield value
async def start_server():
HOST = "127.0.0.1"
PORT = 1337
server = Server([EchoService()])
await server.start(HOST, PORT)
await server.serve_forever()
JSON
++++
Message objects include :meth:`betterproto.Message.to_json` and
Expand Down

0 comments on commit 8eea5fe

Please sign in to comment.