def run():

    # NOTE(gRPC Python Team): .close() is possible on a channel and should be
    # used in circumstances in which the with statement does not fit the needs
    # of the code.
    with grpc.insecure_channel("localhost:50051") as channel:
        channel = intercept_channel(channel, client_interceptor())

        stub = route_guide_pb2_grpc.RouteGuideStub(channel)

        # Unary
        print("-------------- GetFeature --------------")
        guide_get_feature(stub)

        # Server streaming
        print("-------------- ListFeatures --------------")
        guide_list_features(stub)

        # Client streaming
        print("-------------- RecordRoute --------------")
        guide_record_route(stub)

        # Bidirectional streaming
        print("-------------- RouteChat --------------")
        guide_route_chat(stub)
def run():
    # NOTE(gRPC Python Team): .close() is possible on a channel and should be
    # used in circumstances in which the with statement does not fit the needs
    # of the code.
    with grpc.insecure_channel("localhost:50051") as channel:

        channel = intercept_channel(channel, client_interceptor())

        stub = helloworld_pb2_grpc.GreeterStub(channel)

        # stub.SayHello is a _InterceptorUnaryUnaryMultiCallable
        response = stub.SayHello(helloworld_pb2.HelloRequest(name="YOU"))

    print("Greeter client received: " + response.message)
from opentelemetry.propagators.cloud_trace_propagator import CloudTraceFormatPropagator
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

import demo_pb2
import demo_pb2_grpc
from logger import get_json_logger

logger = get_json_logger("emailservice-client")

try:
    trace.set_tracer_provider(TracerProvider())
    trace.get_tracer_provider().add_span_processor(
        SimpleSpanProcessor(CloudTraceSpanExporter()))
    propagate.set_global_textmap(CloudTraceFormatPropagator())
    tracer_interceptor = client_interceptor(trace.get_tracer_provider())
except Exception as e:
    raise Exception("{}".format(e))


def send_confirmation_email(email, order):
    channel = grpc.insecure_channel("0.0.0.0:8080")
    channel = grpc.intercept_channel(channel, tracer_interceptor)
    stub = demo_pb2_grpc.EmailServiceStub(channel)
    try:
        response = stub.SendOrderConfirmation(
            demo_pb2.SendOrderConfirmationRequest(email=email, order=order))
        logger.info("Request sent. response: {}".format(response))
    except grpc.RpcError as err:
        logger.error(err.details())
        logger.error("{}, {}".format(err.code().name, err.code().value))
    )
    propagate.set_global_textmap(CloudTraceFormatPropagator())

    port = os.environ.get('PORT', "8080")
    catalog_addr = os.environ.get('PRODUCT_CATALOG_SERVICE_ADDR', '')
    if catalog_addr == "":
        raise Exception(
            'PRODUCT_CATALOG_SERVICE_ADDR environment variable not set')
    logger.info("product catalog address: " + catalog_addr)

    # Create the gRPC client channel to ProductCatalog (server).
    channel = grpc.insecure_channel(catalog_addr)

    # OpenTelemetry client interceptor passes trace contexts to the server.
    channel = intercept_channel(
        channel, client_interceptor(trace.get_tracer_provider()))
    product_catalog_stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)

    # Create the gRPC server for accepting ListRecommendations Requests from frontend (client).
    interceptor = server_interceptor(trace.get_tracer_provider())
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                         interceptors=(interceptor,))

    # Add RecommendationService class to gRPC server.
    service = RecommendationService()
    demo_pb2_grpc.add_RecommendationServiceServicer_to_server(service, server)
    health_pb2_grpc.add_HealthServicer_to_server(service, server)

    # start server
    logger.info("listening on port: " + port)
    server.add_insecure_port('[::]:'+port)