Codebase list golang-github-go-kit-kit / 1f9491d addsvc / client / grpc / client.go
1f9491d

Tree @1f9491d (Download .tar.gz)

client.go @1f9491draw · history · blame

package grpc

import (
	"golang.org/x/net/context"
	"google.golang.org/grpc"

	"github.com/go-kit/kit/addsvc/pb"
	"github.com/go-kit/kit/addsvc/reqrep"
	"github.com/go-kit/kit/endpoint"
)

// NewClient takes a gRPC ClientConn that should point to an instance of an
// addsvc. It returns an endpoint that wraps and invokes that ClientConn.
func NewClient(cc *grpc.ClientConn) endpoint.Endpoint {
	client := pb.NewAddClient(cc)
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		addReq, ok := request.(reqrep.AddRequest)
		if !ok {
			return nil, endpoint.ErrBadCast
		}
		reply, err := client.Add(ctx, &pb.AddRequest{A: addReq.A, B: addReq.B})
		if err != nil {
			return nil, err
		}
		return reqrep.AddResponse{V: reply.V}, nil
	}
}