-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
741 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# This workflows will upload a Python Package using Twine when a release is created | ||
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries | ||
|
||
name: Upload Python Package | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
deploy: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.x' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install setuptools wheel twine | ||
- name: Build and publish | ||
env: | ||
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} | ||
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} | ||
run: | | ||
echo ${{github.event.release.tag_name}} > version.txt | ||
python setup.py sdist bdist_wheel | ||
twine upload dist/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,86 @@ | ||
# py-tools | ||
# Twirpy | ||
|
||
Python implementation of Twirp RPC framework. | ||
|
||
This repo contains a protoc plugin that generates sever and client code and a pypi package with common implementation details. | ||
|
||
For details about the twirp project, check https://github.com/twitchtv/twirp | ||
|
||
## Installation | ||
|
||
Grab the protoc plugin to generate files with | ||
|
||
``` | ||
go get -u github.com/verloop/twirpy/protoc-gen-twirpy | ||
``` | ||
|
||
Add the twirp package to your project | ||
``` | ||
pip install twirp | ||
``` | ||
|
||
You'll also need [uvicorn](https://www.uvicorn.org/) to run the server. | ||
|
||
## Generate and run | ||
Use the protoc plugin to generate twirp server and client code. | ||
|
||
We'll assume familiarity with the example from the docs. https://twitchtv.github.io/twirp/docs/example.html | ||
|
||
``` | ||
protoc --python_out=./ --twirpy_out=./ ./haberdasher.proto | ||
``` | ||
|
||
### Server code | ||
```python | ||
# server.py | ||
import random | ||
|
||
from twirp.asgi import TwirpASGIApp | ||
from twirp.exceptions import InvalidArgument | ||
|
||
from . import haberdasher_twirp, haberdasher_pb2 | ||
|
||
class HaberdasherService(object): | ||
def MakeHat(self, context, size): | ||
if size.inches <= 0: | ||
raise InvalidArgument(argument="inches", error="I can't make a hat that small!") | ||
return haberdasher_pb2.Hat( | ||
size=size.inches, | ||
color= random.choice(["white", "black", "brown", "red", "blue"]), | ||
name=random.choice(["bowler", "baseball cap", "top hat", "derby"]) | ||
) | ||
|
||
|
||
service = haberdasher_twirp.HaberdasherServer(service=HaberdasherService()) | ||
app = TwirpASGIApp() | ||
app.add_service(service) | ||
``` | ||
|
||
Run the server with | ||
``` | ||
uvicorn twirp_server:app --port=3000 | ||
``` | ||
|
||
### Client code | ||
|
||
```python | ||
# client.py | ||
from twirp.context import Context | ||
from twirp.exceptions import TwirpServerException | ||
|
||
from . import haberdasher_twirp, haberdasher_pb2 | ||
|
||
client = haberdasher_twirp.HaberdasherClient("http://localhost:3000") | ||
|
||
try: | ||
response = client.MakeHat(ctx=Context(), request=haberdasher_pb2.Size(inches=12)) | ||
print(response) | ||
except TwirpServerException as e: | ||
print(e.code, e.message, e.meta, e.to_dict()) | ||
``` | ||
|
||
|
||
## Support and community | ||
Python: [#twirp](https://python-community.slack.com/messages/twirp). Join Python community slack [here](https://pythoncommunity.herokuapp.com) | ||
|
||
Go: [#twirp](https://gophers.slack.com/messages/twirp). Join Gophers community slack [here](https://invite.slack.golangbridge.org) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[[source]] | ||
name = "pypi" | ||
url = "https://pypi.org/simple" | ||
verify_ssl = true | ||
|
||
[dev-packages] | ||
|
||
[packages] | ||
twirp = "==0.0.1.dev03" | ||
uvicorn = "==0.11.5" | ||
|
||
[requires] | ||
python_version = "3.8" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## Twirpy Examples | ||
|
||
## Requirements | ||
You can install requirements for running the examples: | ||
- using `pip install -r requirements.txt` | ||
- using `pipenv install` | ||
|
||
## Running server | ||
You can run the Twirpy ASGI application using servers such as uvicorn. | ||
Example uvicorn command : `uvicorn --port 3000 server:app` | ||
|
||
## Running client | ||
After server has started, you can make request using the example client with the following command : | ||
`python client.py` | ||
|
||
Example output : | ||
``` | ||
size: 12 | ||
color: "black" | ||
name: "bowler" | ||
``` | ||
|
||
## Generated code | ||
The example server and client makes use of code generated by `protoc-gen-twirpy` plugin. You can find the code in `generated` folder. To generate the code yourself for `haberdasher`, use the steps given below: | ||
1. Install `protoc-gen-twirpy` plugin using steps given [here](/README.md) | ||
2. Generate code for `haberdasher` using twirpy plugin : | ||
`protoc --python_out=./generated --twirpy_out=./generated haberdasher.proto` | ||
- python_out : The directory where generated Protobuf Python code needs to be saved. | ||
- twirpy_out : The directory where generated Twirp Python server and client code needs to be saved. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from twirp.context import Context | ||
from twirp.exceptions import TwirpServerException | ||
|
||
from generated import haberdasher_twirp, haberdasher_pb2 | ||
|
||
client = haberdasher_twirp.HaberdasherClient("http://localhost:3000") | ||
|
||
try: | ||
response = client.MakeHat(ctx=Context(), request=haberdasher_pb2.Size(inches=12)) | ||
print(response) | ||
except TwirpServerException as e: | ||
print(e.code, e.message, e.meta, e.to_dict()) |
File renamed without changes.
Oops, something went wrong.