1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Dušan Borovčanin 7dc6b0d879
NOISSUE - Update dependencies (#1218)
* Update Go version

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Update dependencies

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
2020-07-13 15:24:55 +02:00

125 lines
3.5 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package grpc
import (
"context"
"time"
"github.com/go-kit/kit/endpoint"
kitot "github.com/go-kit/kit/tracing/opentracing"
kitgrpc "github.com/go-kit/kit/transport/grpc"
"github.com/golang/protobuf/ptypes/empty"
"github.com/mainflux/mainflux"
opentracing "github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
)
var _ mainflux.ThingsServiceClient = (*grpcClient)(nil)
type grpcClient struct {
timeout time.Duration
canAccessByKey endpoint.Endpoint
canAccessByID endpoint.Endpoint
identify endpoint.Endpoint
}
// NewClient returns new gRPC client instance.
func NewClient(conn *grpc.ClientConn, tracer opentracing.Tracer, timeout time.Duration) mainflux.ThingsServiceClient {
svcName := "mainflux.ThingsService"
return &grpcClient{
timeout: timeout,
canAccessByKey: kitot.TraceClient(tracer, "can_access")(kitgrpc.NewClient(
conn,
svcName,
"CanAccessByKey",
encodeCanAccessByKeyRequest,
decodeIdentityResponse,
mainflux.ThingID{},
).Endpoint()),
canAccessByID: kitot.TraceClient(tracer, "can_access_by_id")(kitgrpc.NewClient(
conn,
svcName,
"CanAccessByID",
encodeCanAccessByIDRequest,
decodeEmptyResponse,
empty.Empty{},
).Endpoint()),
identify: kitot.TraceClient(tracer, "identify")(kitgrpc.NewClient(
conn,
svcName,
"Identify",
encodeIdentifyRequest,
decodeIdentityResponse,
mainflux.ThingID{},
).Endpoint()),
}
}
func (client grpcClient) CanAccessByKey(ctx context.Context, req *mainflux.AccessByKeyReq, _ ...grpc.CallOption) (*mainflux.ThingID, error) {
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
ar := AccessByKeyReq{
thingKey: req.GetToken(),
chanID: req.GetChanID(),
}
res, err := client.canAccessByKey(ctx, ar)
if err != nil {
return nil, err
}
ir := res.(identityRes)
return &mainflux.ThingID{Value: ir.id}, ir.err
}
func (client grpcClient) CanAccessByID(ctx context.Context, req *mainflux.AccessByIDReq, _ ...grpc.CallOption) (*empty.Empty, error) {
ar := accessByIDReq{thingID: req.GetThingID(), chanID: req.GetChanID()}
res, err := client.canAccessByID(ctx, ar)
if err != nil {
return nil, err
}
er := res.(emptyRes)
return &empty.Empty{}, er.err
}
func (client grpcClient) Identify(ctx context.Context, req *mainflux.Token, _ ...grpc.CallOption) (*mainflux.ThingID, error) {
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
res, err := client.identify(ctx, identifyReq{key: req.GetValue()})
if err != nil {
return nil, err
}
ir := res.(identityRes)
return &mainflux.ThingID{Value: ir.id}, ir.err
}
func encodeCanAccessByKeyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(AccessByKeyReq)
return &mainflux.AccessByKeyReq{Token: req.thingKey, ChanID: req.chanID}, nil
}
func encodeCanAccessByIDRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(accessByIDReq)
return &mainflux.AccessByIDReq{ThingID: req.thingID, ChanID: req.chanID}, nil
}
func encodeIdentifyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(identifyReq)
return &mainflux.Token{Value: req.key}, nil
}
func decodeIdentityResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(*mainflux.ThingID)
return identityRes{id: res.GetValue(), err: nil}, nil
}
func decodeEmptyResponse(_ context.Context, _ interface{}) (interface{}, error) {
return emptyRes{}, nil
}