python-betterproto icon indicating copy to clipboard operation
python-betterproto copied to clipboard

Expose request metadata in the generated ```ServiceBase```

Open bettdouglas opened this issue 3 years ago • 8 comments

service Echo {
  rpc Echo(EchoRequest) returns (EchoResponse);
  // rpc EchoStream(EchoRequest) returns (stream EchoStreamResponse);
}

The generated servicer code generates the code but does not expose the request metadata as a parameter.

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

The request metadata is really important since it works like headers in rest-api requests, so this is where a client could add authentication tokens, and also where other metadata could be injected by interceptors for instance.

bettdouglas avatar May 14 '21 08:05 bettdouglas

I have a PR for this: #234 Feedback welcome.

iamnoah avatar May 19 '21 00:05 iamnoah

@iamnoah I've trying generating the echo service, using the example above gives this error

python -m grpc_tools.protoc -I . --python_betterproto_out=lib echo.proto

gives this exception

--python_betterproto_out: protoc-gen-python_betterproto: Plugin failed with status code 1.

I am on python 3.8 and environment variables are

betterproto @ git+https://github.com/hack-edu/python-betterproto.git@02e41afd09f0050a10fea764b15279b82cdd6e6b
grpcio==1.37.1
grpcio-tools==1.37.1
grpclib==0.4.1
h2==4.0.0
hpack==4.0.0
hyperframe==6.0.1
multidict==5.1.0
protobuf==3.17.0
python-dateutil==2.8.1
six==1.16.0

which i installed via pip install git+https://github.com/hack-edu/python-betterproto.git

bettdouglas avatar May 19 '21 11:05 bettdouglas

@bettdouglas try pip install git+https://github.com/hack-edu/python-betterproto.git@with-stream-hooks black jinja2

My PR is from a branch, it looks like you installed master, which is unchanged.

Also, installing the plugin from the repo doesn't pickup the compiler dependencies:

> protoc-gen-python_betterproto
Unable to import `black` from betterproto plugin! Please ensure that you've installed betterproto as `pip install "betterproto[compiler]"` so that compiler dependencies are included.

Which is why I added black and jinja2.

iamnoah avatar May 20 '21 03:05 iamnoah

Are there any other suggestions on this issue? I forked betterproto and did something similar to @iamnoah's solution, but if there is a better practice suggested by the community, I'd love to learn about it.

Thanks all!

jbkoh avatar Apr 06 '22 06:04 jbkoh

I guess I should switch back to grpclib

parhawm avatar Dec 26 '23 15:12 parhawm

I guess I should switch back to grpclib

@parhawm This solution works great: https://github.com/danielgtaylor/python-betterproto/pull/234#issuecomment-1089905019

jbkoh avatar Dec 27 '23 23:12 jbkoh

This feature is essential, I agree!

No progress for it since 2021?

gurland avatar Apr 28 '24 13:04 gurland

Also would like to see support for the missing metadata on the messages used in server base.

Using this solution ATM: https://github.com/danielgtaylor/python-betterproto/pull/234#issuecomment-1089905019

greemo avatar May 01 '24 04:05 greemo