Skip to content

Commit

Permalink
Remove verloop's branding (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofpiyush authored May 18, 2020
1 parent f1b8d3e commit de2d4bd
Show file tree
Hide file tree
Showing 37 changed files with 741 additions and 123 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/pypi-publish.yml
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/*
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,10 @@ venv.bak/

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.vscode

version.txt

# generated Go binary
protoc-gen-twirpy/protoc-gen-twirpy
87 changes: 86 additions & 1 deletion README.md
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)
13 changes: 13 additions & 0 deletions example/Pipfile
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"
181 changes: 181 additions & 0 deletions example/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions example/README.md
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.
12 changes: 12 additions & 0 deletions example/client.py
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.
Loading

0 comments on commit de2d4bd

Please sign in to comment.