From cb7831b00cfb58280d491643764273749491dfa9 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 20 Aug 2020 13:21:55 +0300 Subject: [PATCH] v2/container: Implement thin NeoFS gRPC client Signed-off-by: Leonard Lyubich --- v2/container/grpc/client.go | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 v2/container/grpc/client.go diff --git a/v2/container/grpc/client.go b/v2/container/grpc/client.go new file mode 100644 index 00000000..8e3cdf99 --- /dev/null +++ b/v2/container/grpc/client.go @@ -0,0 +1,82 @@ +package container + +import ( + "context" + + "github.com/pkg/errors" + "google.golang.org/grpc" +) + +// Client wraps ContainerServiceClient +// with pre-defined configurations. +type Client struct { + *cfg + + client ContainerServiceClient +} + +// Option represents Client option. +type Option func(*cfg) + +type cfg struct { + callOpts []grpc.CallOption +} + +// ErrNilContainerServiceClient is returned by functions that expect +// a non-nil ContainerServiceClient, but received nil. +var ErrNilContainerServiceClient = errors.New("container gRPC client is nil") + +func defaultCfg() *cfg { + return new(cfg) +} + +// NewClient creates, initializes and returns a new Client instance. +// +// Options are applied one by one in order. +func NewClient(c ContainerServiceClient, opts ...Option) (*Client, error) { + if c == nil { + return nil, ErrNilContainerServiceClient + } + + cfg := defaultCfg() + for i := range opts { + opts[i](cfg) + } + + return &Client{ + cfg: cfg, + client: c, + }, nil +} + +func (c *Client) Put(ctx context.Context, req *PutRequest) (*PutResponse, error) { + return c.client.Put(ctx, req, c.callOpts...) +} + +func (c *Client) Get(ctx context.Context, req *GetRequest) (*GetResponse, error) { + return c.client.Get(ctx, req, c.callOpts...) +} + +func (c *Client) Delete(ctx context.Context, req *DeleteRequest) (*DeleteResponse, error) { + return c.client.Delete(ctx, req, c.callOpts...) +} + +func (c *Client) List(ctx context.Context, req *ListRequest) (*ListResponse, error) { + return c.client.List(ctx, req, c.callOpts...) +} + +func (c *Client) SetExtendedACL(ctx context.Context, req *SetExtendedACLRequest) (*SetExtendedACLResponse, error) { + return c.client.SetExtendedACL(ctx, req, c.callOpts...) +} + +func (c *Client) GetExtendedACL(ctx context.Context, req *GetExtendedACLRequest) (*GetExtendedACLResponse, error) { + return c.client.GetExtendedACL(ctx, req, c.callOpts...) +} + +// WithCallOptions returns Option that configures +// Client to attach call options to each rpc call. +func WithCallOptions(opts []grpc.CallOption) Option { + return func(c *cfg) { + c.callOpts = opts + } +}