-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request grpc#13745 from mehrdada/upmerge180
Upmerge v1.8.x into master
- Loading branch information
Showing
46 changed files
with
2,487 additions
and
626 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
examples/python/interceptors/default_value/default_value_client_interceptor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright 2017 gRPC authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Interceptor that adds headers to outgoing requests.""" | ||
|
||
import collections | ||
|
||
import grpc | ||
|
||
|
||
class _ConcreteValue(grpc.Future): | ||
|
||
def __init__(self, result): | ||
self._result = result | ||
|
||
def cancel(self): | ||
return False | ||
|
||
def cancelled(self): | ||
return False | ||
|
||
def running(self): | ||
return False | ||
|
||
def done(self): | ||
return True | ||
|
||
def result(self, timeout=None): | ||
return self._result | ||
|
||
def exception(self, timeout=None): | ||
return None | ||
|
||
def traceback(self, timeout=None): | ||
return None | ||
|
||
def add_done_callback(self, fn): | ||
fn(self._result) | ||
|
||
|
||
class DefaultValueClientInterceptor(grpc.UnaryUnaryClientInterceptor, | ||
grpc.StreamUnaryClientInterceptor): | ||
|
||
def __init__(self, value): | ||
self._default = _ConcreteValue(value) | ||
|
||
def _intercept_call(self, continuation, client_call_details, | ||
request_or_iterator): | ||
response = continuation(client_call_details, request_or_iterator) | ||
return self._default if response.exception() else response | ||
|
||
def intercept_unary_unary(self, continuation, client_call_details, request): | ||
return self._intercept_call(continuation, client_call_details, request) | ||
|
||
def intercept_stream_unary(self, continuation, client_call_details, | ||
request_iterator): | ||
return self._intercept_call(continuation, client_call_details, | ||
request_iterator) |
38 changes: 38 additions & 0 deletions
38
examples/python/interceptors/default_value/greeter_client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2017 gRPC authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""The Python implementation of the gRPC helloworld.Greeter client.""" | ||
|
||
from __future__ import print_function | ||
|
||
import grpc | ||
|
||
import helloworld_pb2 | ||
import helloworld_pb2_grpc | ||
import default_value_client_interceptor | ||
|
||
|
||
def run(): | ||
default_value = helloworld_pb2.HelloReply( | ||
message='Hello from your local interceptor!') | ||
default_value_interceptor = default_value_client_interceptor.DefaultValueClientInterceptor( | ||
default_value) | ||
channel = grpc.insecure_channel('localhost:50051') | ||
channel = grpc.intercept_channel(channel, default_value_interceptor) | ||
stub = helloworld_pb2_grpc.GreeterStub(channel) | ||
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) | ||
print("Greeter client received: " + response.message) | ||
|
||
|
||
if __name__ == '__main__': | ||
run() |
134 changes: 134 additions & 0 deletions
134
examples/python/interceptors/default_value/helloworld_pb2.py
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
examples/python/interceptors/default_value/helloworld_pb2_grpc.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! | ||
import grpc | ||
|
||
import helloworld_pb2 as helloworld__pb2 | ||
|
||
|
||
class GreeterStub(object): | ||
"""The greeting service definition. | ||
""" | ||
|
||
def __init__(self, channel): | ||
"""Constructor. | ||
Args: | ||
channel: A grpc.Channel. | ||
""" | ||
self.SayHello = channel.unary_unary( | ||
'/helloworld.Greeter/SayHello', | ||
request_serializer=helloworld__pb2.HelloRequest.SerializeToString, | ||
response_deserializer=helloworld__pb2.HelloReply.FromString, | ||
) | ||
|
||
|
||
class GreeterServicer(object): | ||
"""The greeting service definition. | ||
""" | ||
|
||
def SayHello(self, request, context): | ||
"""Sends a greeting | ||
""" | ||
context.set_code(grpc.StatusCode.UNIMPLEMENTED) | ||
context.set_details('Method not implemented!') | ||
raise NotImplementedError('Method not implemented!') | ||
|
||
|
||
def add_GreeterServicer_to_server(servicer, server): | ||
rpc_method_handlers = { | ||
'SayHello': grpc.unary_unary_rpc_method_handler( | ||
servicer.SayHello, | ||
request_deserializer=helloworld__pb2.HelloRequest.FromString, | ||
response_serializer=helloworld__pb2.HelloReply.SerializeToString, | ||
), | ||
} | ||
generic_handler = grpc.method_handlers_generic_handler( | ||
'helloworld.Greeter', rpc_method_handlers) | ||
server.add_generic_rpc_handlers((generic_handler,)) |
55 changes: 55 additions & 0 deletions
55
examples/python/interceptors/headers/generic_client_interceptor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2017 gRPC authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Base class for interceptors that operate on all RPC types.""" | ||
|
||
import grpc | ||
|
||
|
||
class _GenericClientInterceptor( | ||
grpc.UnaryUnaryClientInterceptor, grpc.UnaryStreamClientInterceptor, | ||
grpc.StreamUnaryClientInterceptor, grpc.StreamStreamClientInterceptor): | ||
|
||
def __init__(self, interceptor_function): | ||
self._fn = interceptor_function | ||
|
||
def intercept_unary_unary(self, continuation, client_call_details, request): | ||
new_details, new_request_iterator, postprocess = self._fn( | ||
client_call_details, iter((request,)), False, False) | ||
response = continuation(new_details, next(new_request_iterator)) | ||
return postprocess(response) if postprocess else response | ||
|
||
def intercept_unary_stream(self, continuation, client_call_details, | ||
request): | ||
new_details, new_request_iterator, postprocess = self._fn( | ||
client_call_details, iter((request,)), False, True) | ||
response_it = continuation(new_details, new_request_iterator) | ||
return postprocess(response_it) if postprocess else response_it | ||
|
||
def intercept_stream_unary(self, continuation, client_call_details, | ||
request_iterator): | ||
new_details, new_request_iterator, postprocess = self._fn( | ||
client_call_details, request_iterator, True, False) | ||
response = continuation(new_details, next(new_request_iterator)) | ||
return postprocess(response) if postprocess else response | ||
|
||
def intercept_stream_stream(self, continuation, client_call_details, | ||
request_iterator): | ||
new_details, new_request_iterator, postprocess = self._fn( | ||
client_call_details, request_iterator, True, True) | ||
response_it = continuation(new_details, new_request_iterator) | ||
return postprocess(response_it) if postprocess else response_it | ||
|
||
|
||
def create(intercept_call): | ||
return _GenericClientInterceptor(intercept_call) |
Oops, something went wrong.