1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00

84 lines
2.1 KiB
Go
Raw Normal View History

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
2018-05-10 23:53:25 +02:00
package grpc
import (
"github.com/go-kit/kit/endpoint"
kitgrpc "github.com/go-kit/kit/transport/grpc"
"github.com/mainflux/mainflux"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var _ mainflux.ThingsServiceClient = (*grpcClient)(nil)
2018-05-10 23:53:25 +02:00
type grpcClient struct {
canAccess endpoint.Endpoint
identify endpoint.Endpoint
2018-05-10 23:53:25 +02:00
}
// NewClient returns new gRPC client instance.
func NewClient(conn *grpc.ClientConn) mainflux.ThingsServiceClient {
svcName := "mainflux.ThingsService"
return &grpcClient{
canAccess: kitgrpc.NewClient(
conn,
svcName,
"CanAccess",
encodeCanAccessRequest,
decodeIdentityResponse,
mainflux.ThingID{},
).Endpoint(),
identify: kitgrpc.NewClient(
conn,
svcName,
"Identify",
encodeIdentifyRequest,
decodeIdentityResponse,
mainflux.ThingID{},
).Endpoint(),
}
2018-05-10 23:53:25 +02:00
}
func (client grpcClient) CanAccess(ctx context.Context, req *mainflux.AccessReq, _ ...grpc.CallOption) (*mainflux.ThingID, error) {
ar := accessReq{thingKey: req.GetToken(), chanID: req.GetChanID()}
res, err := client.canAccess(ctx, ar)
2018-05-10 23:53:25 +02:00
if err != nil {
return nil, err
}
ir := res.(identityRes)
return &mainflux.ThingID{Value: ir.id}, ir.err
}
func (client grpcClient) Identify(ctx context.Context, req *mainflux.Token, _ ...grpc.CallOption) (*mainflux.ThingID, error) {
res, err := client.identify(ctx, identifyReq{req.GetValue()})
if err != nil {
return nil, err
}
ir := res.(identityRes)
return &mainflux.ThingID{Value: ir.id}, ir.err
2018-05-10 23:53:25 +02:00
}
func encodeCanAccessRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(accessReq)
return &mainflux.AccessReq{Token: req.thingKey, ChanID: req.chanID}, nil
2018-05-10 23:53:25 +02:00
}
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
2018-05-10 23:53:25 +02:00
}