1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-06 19:29:15 +08:00

65 lines
1.6 KiB
Go
Raw Normal View History

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
2018-05-10 23:53:25 +02:00
package grpc
import (
2019-07-18 15:01:09 +02:00
"time"
2018-05-10 23:53:25 +02:00
"github.com/go-kit/kit/endpoint"
2019-07-18 15:01:09 +02:00
kitot "github.com/go-kit/kit/tracing/opentracing"
2018-05-10 23:53:25 +02:00
kitgrpc "github.com/go-kit/kit/transport/grpc"
2019-07-18 15:01:09 +02:00
opentracing "github.com/opentracing/opentracing-go"
2018-05-10 23:53:25 +02:00
"github.com/mainflux/mainflux"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var _ mainflux.UsersServiceClient = (*grpcClient)(nil)
type grpcClient struct {
identify endpoint.Endpoint
2019-07-18 15:01:09 +02:00
timeout time.Duration
2018-05-10 23:53:25 +02:00
}
// NewClient returns new gRPC client instance.
2019-07-18 15:01:09 +02:00
func NewClient(tracer opentracing.Tracer, conn *grpc.ClientConn, timeout time.Duration) mainflux.UsersServiceClient {
endpoint := kitot.TraceClient(tracer, "identify")(kitgrpc.NewClient(
2018-05-10 23:53:25 +02:00
conn,
"mainflux.UsersService",
"Identify",
encodeIdentifyRequest,
decodeIdentifyResponse,
mainflux.UserID{},
2019-07-18 15:01:09 +02:00
).Endpoint())
2018-05-10 23:53:25 +02:00
2019-07-18 15:01:09 +02:00
return &grpcClient{
identify: endpoint,
timeout: timeout,
}
2018-05-10 23:53:25 +02:00
}
func (client grpcClient) Identify(ctx context.Context, token *mainflux.Token, _ ...grpc.CallOption) (*mainflux.UserID, error) {
2019-07-18 15:01:09 +02:00
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
2018-05-10 23:53:25 +02:00
res, err := client.identify(ctx, identityReq{token.GetValue()})
if err != nil {
return nil, err
}
ir := res.(identityRes)
return &mainflux.UserID{Value: ir.id}, ir.err
2018-05-10 23:53:25 +02:00
}
func encodeIdentifyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(identityReq)
return &mainflux.Token{Value: req.token}, nil
}
func decodeIdentifyResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(*mainflux.UserID)
2018-05-10 23:53:25 +02:00
return identityRes{res.GetValue(), nil}, nil
}