Skip to content

Commit

Permalink
examples: Fix gRPC server
Browse files Browse the repository at this point in the history
Addresses part of #1189
  • Loading branch information
Acconut committed Sep 19, 2024
1 parent 3f6e8b7 commit 9b86376
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_modules/
./tusd
tusd_*_*
__pycache__/
venv/
examples/hooks/plugin/hook_handler
.idea/
.vscode/
4 changes: 2 additions & 2 deletions examples/hooks/grpc/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
hook_pb2.py: ../../../cmd/tusd/cli/hooks/proto/v2/hook.proto
python3 -m grpc_tools.protoc --proto_path=../../../cmd/tusd/cli/hooks/proto/v2/ hook.proto --python_out=. --grpc_python_out=.
hook_pb2.py: ../../../pkg/hooks/grpc/proto/hook.proto
python3 -m grpc_tools.protoc --proto_path=../../../pkg/hooks/grpc/proto/ hook.proto --python_out=. --grpc_python_out=.
19 changes: 19 additions & 0 deletions examples/hooks/grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Example gRPC hook server with Python

Use the following commands to run this example in a virtual environment:

```sh
# Setup virtual environment and install dependencies
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt

# Build gRPC code, if necessary
make -B hook_pb2.py

# Start gRPC server (listening at localhost:8000)
python3 server.py

# In a separate terminal you can now run tusd and point it to the gRPC server
tusd -hooks-grpc localhost:8000
```
106 changes: 59 additions & 47 deletions examples/hooks/grpc/hook_pb2.py

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

43 changes: 37 additions & 6 deletions examples/hooks/grpc/hook_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
import warnings

import hook_pb2 as hook__pb2

GRPC_GENERATED_VERSION = '1.66.1'
GRPC_VERSION = grpc.__version__
_version_not_supported = False

try:
from grpc._utilities import first_version_is_lower
_version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION)
except ImportError:
_version_not_supported = True

if _version_not_supported:
raise RuntimeError(
f'The grpc package installed is at version {GRPC_VERSION},'
+ f' but the generated code in hook_pb2_grpc.py depends on'
+ f' grpcio>={GRPC_GENERATED_VERSION}.'
+ f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}'
+ f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.'
)


class HookHandlerStub(object):
"""The hook service definition.
Expand All @@ -16,10 +36,10 @@ def __init__(self, channel):
channel: A grpc.Channel.
"""
self.InvokeHook = channel.unary_unary(
'/v2.HookHandler/InvokeHook',
'/proto.HookHandler/InvokeHook',
request_serializer=hook__pb2.HookRequest.SerializeToString,
response_deserializer=hook__pb2.HookResponse.FromString,
)
_registered_method=True)


class HookHandlerServicer(object):
Expand Down Expand Up @@ -47,8 +67,9 @@ def add_HookHandlerServicer_to_server(servicer, server):
),
}
generic_handler = grpc.method_handlers_generic_handler(
'v2.HookHandler', rpc_method_handlers)
'proto.HookHandler', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
server.add_registered_method_handlers('proto.HookHandler', rpc_method_handlers)


# This class is part of an EXPERIMENTAL API.
Expand All @@ -67,8 +88,18 @@ def InvokeHook(request,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/v2.HookHandler/InvokeHook',
return grpc.experimental.unary_unary(
request,
target,
'/proto.HookHandler/InvokeHook',
hook__pb2.HookRequest.SerializeToString,
hook__pb2.HookResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
options,
channel_credentials,
insecure,
call_credentials,
compression,
wait_for_ready,
timeout,
metadata,
_registered_method=True)
4 changes: 4 additions & 0 deletions examples/hooks/grpc/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
grpcio==1.66.1
grpcio-tools==1.66.1
protobuf==5.28.2
setuptools==75.1.0

0 comments on commit 9b86376

Please sign in to comment.