Skip to content

Commit

Permalink
Update Twirpy for Protocol v7 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
avinassh authored Nov 13, 2020
1 parent 4b40c3a commit bfc6e0a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 31 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Twirpy

Python implementation of Twirp RPC framework.
Python implementation of Twirp RPC framework (supports [Twirp Wire Protocol v7](https://twitchtv.github.io/twirp/docs/spec_v7.html)).

This repo contains a protoc plugin that generates sever and client code and a pypi package with common implementation details.

Expand Down Expand Up @@ -51,6 +51,8 @@ class HaberdasherService(object):
)


# if you are using a custom prefix, then pass it as `server_path_prefix`
# param to `HaberdasherServer` class.
service = haberdasher_twirp.HaberdasherServer(service=HaberdasherService())
app = TwirpASGIApp()
app.add_service(service)
Expand All @@ -72,13 +74,23 @@ from . import haberdasher_twirp, haberdasher_pb2

client = haberdasher_twirp.HaberdasherClient("http://localhost:3000")

# if you are using a custom prefix, then pass it as `server_path_prefix`
# param to `MakeHat` class.
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())
```

## Twirp Wire Protocol (v7)

Twirpy generates the code based on the protocol v7. This is a breaking change from the previous v5 and you can see the changes [here](https://twitchtv.github.io/twirp/docs/spec_v7.html#differences-with-v5).

This new version comes with flexibility to use any prefix for the server URLs and defaults to `/twirp`. To use an empty prefix or any custom prefix like `/my/custom/prefix`, pass it as a `server_path_prefix` param to server and clients. Check the example directory, which uses `/twirpy` as a custom prefix.

If you want to use the server and clients of v5, then use the [0.0.1](https://github.com/verloop/twirpy/releases/tag/0.0.1) release.

## Support and community
Python: [#twirp](https://python-community.slack.com/messages/twirp). Join Python community slack [here](https://pythoncommunity.herokuapp.com)

Expand Down
3 changes: 2 additions & 1 deletion example/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
client = haberdasher_twirp.HaberdasherClient("http://localhost:3000")

try:
response = client.MakeHat(ctx=Context(), request=haberdasher_pb2.Size(inches=12))
response = client.MakeHat(
ctx=Context(), request=haberdasher_pb2.Size(inches=12), server_path_prefix="/twirpy")
print(response)
except TwirpServerException as e:
print(e.code, e.message, e.meta, e.to_dict())
41 changes: 22 additions & 19 deletions example/generated/haberdasher_pb2.py

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

8 changes: 4 additions & 4 deletions example/generated/haberdasher_twirp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

class HaberdasherServer(TwirpServer):

def __init__(self, *args, service):
def __init__(self, *args, service, server_path_prefix="/twirp"):
super().__init__(service=service)
self._prefix = "/twirp/twitch.twirp.example.Haberdasher"
self._prefix = F"{server_path_prefix}/twitch.twirp.example.Haberdasher"
self._endpoints = {
"MakeHat": Endpoint(
service_name="Haberdasher",
Expand All @@ -27,9 +27,9 @@ def __init__(self, *args, service):

class HaberdasherClient(TwirpClient):

def MakeHat(self, *args, ctx, request, **kwargs):
def MakeHat(self, *args, ctx, request, server_path_prefix="/twirp", **kwargs):
return self._make_request(
url="/twirp/twitch.twirp.example.Haberdasher/MakeHat",
url=F"{server_path_prefix}/twitch.twirp.example.Haberdasher/MakeHat",
ctx=ctx,
request=request,
response_obj=_sym_db.GetSymbol("twitch.twirp.example.Hat"),
Expand Down
3 changes: 2 additions & 1 deletion example/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def MakeHat(self, context, size):
)


service = haberdasher_twirp.HaberdasherServer(service=HaberdasherService())
service = haberdasher_twirp.HaberdasherServer(
service=HaberdasherService(), server_path_prefix="/twirpy")
app = TwirpASGIApp()
app.add_service(service)
8 changes: 4 additions & 4 deletions protoc-gen-twirpy/generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ _sym_db = _symbol_database.Default()
{{range .Services}}
class {{.Name}}Server(TwirpServer):
def __init__(self, *args, service):
def __init__(self, *args, service, server_path_prefix="/twirp"):
super().__init__(service=service)
self._prefix = "/twirp/{{.ServiceURL}}"
self._prefix = F"{server_path_prefix}/{{.ServiceURL}}"
self._endpoints = { {{- range .Methods }}
"{{.Name}}": Endpoint(
service_name="{{.ServiceName}}",
Expand All @@ -58,9 +58,9 @@ class {{.Name}}Server(TwirpServer):
class {{.Name}}Client(TwirpClient):
{{range .Methods}}
def {{.Name}}(self, *args, ctx, request, **kwargs):
def {{.Name}}(self, *args, ctx, request, server_path_prefix="/twirp", **kwargs):
return self._make_request(
url="/twirp/{{.ServiceURL}}/{{.Name}}",
url=F"{server_path_prefix}/{{.ServiceURL}}/{{.Name}}",
ctx=ctx,
request=request,
response_obj=_sym_db.GetSymbol("{{.Output}}"),
Expand Down
2 changes: 1 addition & 1 deletion twirp/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_status_code(code):
Errors.AlreadyExists: 409,
Errors.PermissionDenied: 403,
Errors.Unauthenticated: 401,
Errors.ResourceExhausted: 403,
Errors.ResourceExhausted: 429,
Errors.FailedPrecondition: 412,
Errors.Aborted: 409,
Errors.OutOfRange: 400,
Expand Down

0 comments on commit bfc6e0a

Please sign in to comment.