Skip to content

Commit

Permalink
change to a generic authentication example (#42)
Browse files Browse the repository at this point in the history
* change to a generic authentication example

* Update examples/authentication/main.go

Co-authored-by: Anush  <anushshetty90@gmail.com>

---------

Co-authored-by: Bastian Hofmann <mail@bastianhofmann.de>
Co-authored-by: Anush <anushshetty90@gmail.com>
  • Loading branch information
3 people committed Jun 13, 2024
1 parent d52f5ca commit ef81f2c
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions examples/authentication/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -32,12 +32,15 @@ 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 {
log.Fatalf("could not get collections: %v", err)
}
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...)
}

0 comments on commit ef81f2c

Please sign in to comment.