How to add global metadata for Python client and how to check metadata for server like middleware?

708 views
Skip to first unread message

yan...@chaitin.com

unread,
Feb 14, 2017, 7:16:42 AM2/14/17
to grpc.io
I'd like to add some metadata like token or version at each call, and server should check it.

I know how to add meta for each call (metadata argument), however, Is there a way to add metadata globally for client?

I know how to get metadata in each server context (context.invocation_metadata()), however, is there something like middleware to check it globally?

Thanks!

joest...@gmail.com

unread,
Feb 15, 2017, 1:31:48 PM2/15/17
to grpc.io, yan...@chaitin.com
You can apply CallCredentials to ChannelCredentials through grpc.composite_channel_credentials. I believe its use is for authorization but it essentially adds metadata for every call. You need to connect to a secure channel to use it though.

transport_creds = grpc.ssl_channel_credentials(open('roots.pem').read())
def credentials(context, callback):
 
callback([('key','value')], None)

auth_creds = grpc.metadata_call_credentials(credentials)
channel_creds = grpc.composite_channel_credentials(transport_creds, auth_creds)
channel = grpc.secure_channel('greeter.googleapis.com:443', channel_creds)

A better option might be to create a shim layer that just passes on the call while adding some metadata. Could probably achieve that with some __getattr__ magic

joest...@gmail.com

unread,
Feb 15, 2017, 1:36:15 PM2/15/17
to grpc.io, yan...@chaitin.com
As for middleware, i would keep an eye on this issue: https://github.com/grpc/grpc/issues/8767


On Tuesday, February 14, 2017 at 12:16:42 PM UTC, yan...@chaitin.com wrote:
Reply all
Reply to author
Forward
0 new messages