diff --git a/examples/authentication/main.go b/examples/authentication/main.go index a5bb6a2..b927492 100644 --- a/examples/authentication/main.go +++ b/examples/authentication/main.go @@ -22,7 +22,7 @@ func main() { flag.Parse() // Set up a connection to the server. config := &tls.Config{} - conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(credentials.NewTLS(config))) + conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(credentials.NewTLS(config)), grpc.WithUnaryInterceptor(interceptor)) if err != nil { log.Fatalf("did not connect: %v", err) } @@ -32,8 +32,6 @@ func main() { // Contact the server and print out its response. ctx, cancel := context.WithTimeout(context.Background(), time.Second) - md := metadata.New(map[string]string{"api-key": "secret-key-*******"}) - ctx = metadata.NewOutgoingContext(ctx, md) defer cancel() r, err := collections_client.List(ctx, &pb.ListCollectionsRequest{}) if err != nil { @@ -41,3 +39,8 @@ func main() { } log.Printf("List of collections: %s", r.GetCollections()) } + +func interceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + newCtx := metadata.AppendToOutgoingContext(ctx, "api-key", "secret-key-*******") + return invoker(newCtx, method, req, reply, cc, opts...) +}