Skip to content

Commit

Permalink
refactor: DRY the code
Browse files Browse the repository at this point in the history
  • Loading branch information
amalthundiyil committed May 9, 2022
1 parent cbb61b8 commit be5ab2e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
71 changes: 36 additions & 35 deletions internal/grpc/services/gateway/groupprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ import (
group "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)

func (s *svc) GetGroup(ctx context.Context, req *group.GetGroupRequest) (*group.GetGroupResponse, error) {
c, err := pool.GetGroupProviderServiceClient(
pool.Endpoint(s.c.GroupProviderEndpoint),
pool.Insecure(s.c.Insecure),
pool.SkipVerify(s.c.SkipVerify),
pool.CACertFile(s.c.CACertFile),
pool.MaxCallRecvMsgSize(s.c.MaxCallRecvMsgSize),
)
var poolConf map[string]interface{}
err := mapstructure.Decode(s.c, poolConf)
if err != nil {
return nil, errors.Wrap(err, "error decoding pool config")
}
options := pool.NewOptions(poolConf, pool.Endpoint(s.c.GroupProviderEndpoint))
c, err := pool.GetGroupProviderServiceClient(options)
if err != nil {
return &group.GetGroupResponse{
Status: status.NewInternal(ctx, err, "error getting auth client"),
Expand All @@ -53,13 +54,13 @@ func (s *svc) GetGroupByClaim(
ctx context.Context,
req *group.GetGroupByClaimRequest,
) (*group.GetGroupByClaimResponse, error) {
c, err := pool.GetGroupProviderServiceClient(
pool.Endpoint(s.c.GroupProviderEndpoint),
pool.Insecure(s.c.Insecure),
pool.SkipVerify(s.c.SkipVerify),
pool.CACertFile(s.c.CACertFile),
pool.MaxCallRecvMsgSize(s.c.MaxCallRecvMsgSize),
)
var poolConf map[string]interface{}
err := mapstructure.Decode(s.c, poolConf)
if err != nil {
return nil, errors.Wrap(err, "error decoding pool config")
}
options := pool.NewOptions(poolConf, pool.Endpoint(s.c.GroupProviderEndpoint))
c, err := pool.GetGroupProviderServiceClient(options)
if err != nil {
return &group.GetGroupByClaimResponse{
Status: status.NewInternal(ctx, err, "error getting auth client"),
Expand All @@ -75,13 +76,13 @@ func (s *svc) GetGroupByClaim(
}

func (s *svc) FindGroups(ctx context.Context, req *group.FindGroupsRequest) (*group.FindGroupsResponse, error) {
c, err := pool.GetGroupProviderServiceClient(
pool.Endpoint(s.c.GroupProviderEndpoint),
pool.Insecure(s.c.Insecure),
pool.SkipVerify(s.c.SkipVerify),
pool.CACertFile(s.c.CACertFile),
pool.MaxCallRecvMsgSize(s.c.MaxCallRecvMsgSize),
)
var poolConf map[string]interface{}
err := mapstructure.Decode(s.c, poolConf)
if err != nil {
return nil, errors.Wrap(err, "error decoding pool config")
}
options := pool.NewOptions(poolConf, pool.Endpoint(s.c.GroupProviderEndpoint))
c, err := pool.GetGroupProviderServiceClient(options)
if err != nil {
return &group.FindGroupsResponse{
Status: status.NewInternal(ctx, err, "error getting auth client"),
Expand All @@ -97,13 +98,13 @@ func (s *svc) FindGroups(ctx context.Context, req *group.FindGroupsRequest) (*gr
}

func (s *svc) GetMembers(ctx context.Context, req *group.GetMembersRequest) (*group.GetMembersResponse, error) {
c, err := pool.GetGroupProviderServiceClient(
pool.Endpoint(s.c.GroupProviderEndpoint),
pool.Insecure(s.c.Insecure),
pool.SkipVerify(s.c.SkipVerify),
pool.CACertFile(s.c.CACertFile),
pool.MaxCallRecvMsgSize(s.c.MaxCallRecvMsgSize),
)
var poolConf map[string]interface{}
err := mapstructure.Decode(s.c, poolConf)
if err != nil {
return nil, errors.Wrap(err, "error decoding pool config")
}
options := pool.NewOptions(poolConf, pool.Endpoint(s.c.GroupProviderEndpoint))
c, err := pool.GetGroupProviderServiceClient(options)
if err != nil {
return &group.GetMembersResponse{
Status: status.NewInternal(ctx, err, "error getting auth client"),
Expand All @@ -119,13 +120,13 @@ func (s *svc) GetMembers(ctx context.Context, req *group.GetMembersRequest) (*gr
}

func (s *svc) HasMember(ctx context.Context, req *group.HasMemberRequest) (*group.HasMemberResponse, error) {
c, err := pool.GetGroupProviderServiceClient(
pool.Endpoint(s.c.GroupProviderEndpoint),
pool.Insecure(s.c.Insecure),
pool.SkipVerify(s.c.SkipVerify),
pool.CACertFile(s.c.CACertFile),
pool.MaxCallRecvMsgSize(s.c.MaxCallRecvMsgSize),
)
var poolConf map[string]interface{}
err := mapstructure.Decode(s.c, poolConf)
if err != nil {
return nil, errors.Wrap(err, "error decoding pool config")
}
options := pool.NewOptions(poolConf, pool.Endpoint(s.c.GroupProviderEndpoint))
c, err := pool.GetGroupProviderServiceClient(options)
if err != nil {
return &group.HasMemberResponse{
Status: status.NewInternal(ctx, err, "error getting auth client"),
Expand Down
14 changes: 14 additions & 0 deletions pkg/rgrpc/todo/pool/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ func newOptions(opts ...Option) Options {
return opt
}

// NewOptions initializes the available default options.
func NewOptions(conf map[string]interface{}, opts ...Option) Options {
opt := Options{
MaxCallRecvMsgSize: defaultMaxCallRecvMsgSize,
Insecure: true,
}

for _, o := range opts {
o(&opt)
}

return opt
}

// Endpoint provides a function to set the endpoint option.
func Endpoint(val string) Option {
return func(o *Options) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/rgrpc/todo/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,10 @@ func GetUserProviderServiceClient(opts ...Option) (user.UserAPIClient, error) {
}

// GetGroupProviderServiceClient returns a GroupProviderServiceClient.
func GetGroupProviderServiceClient(opts ...Option) (group.GroupAPIClient, error) {
func GetGroupProviderServiceClient(options Options) (group.GroupAPIClient, error) {
groupProviders.m.Lock()
defer groupProviders.m.Unlock()

options := newOptions(opts...)
if val, ok := groupProviders.conn[options.Endpoint]; ok {
return val.(group.GroupAPIClient), nil
}
Expand Down

0 comments on commit be5ab2e

Please sign in to comment.