From 28d8acc58a171202825179c3978f7eea6d25e718 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 19 Aug 2020 16:21:30 +0300 Subject: [PATCH] v2/accounting: Implement thin NeoFS gRPC client Defines thin wrapper over the generated gRPC Accounting service client to fix some of its options. Signed-off-by: Leonard Lyubich --- v2/accounting/grpc/client.go | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 v2/accounting/grpc/client.go diff --git a/v2/accounting/grpc/client.go b/v2/accounting/grpc/client.go new file mode 100644 index 0000000..6a626b1 --- /dev/null +++ b/v2/accounting/grpc/client.go @@ -0,0 +1,62 @@ +package accounting + +import ( + "context" + + "github.com/pkg/errors" + "google.golang.org/grpc" +) + +// Client wraps AccountingServiceClient +// with pre-defined configurations. +type Client struct { + *cfg + + client AccountingServiceClient +} + +// Option represents Client option. +type Option func(*cfg) + +type cfg struct { + callOpts []grpc.CallOption +} + +// ErrNilAccountingServiceClient is returned by functions that expect +// a non-nil AccountingServiceClient, but received nil. +var ErrNilAccountingServiceClient = errors.New("accounting 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 AccountingServiceClient, opts ...Option) (*Client, error) { + if c == nil { + return nil, ErrNilAccountingServiceClient + } + + cfg := defaultCfg() + for i := range opts { + opts[i](cfg) + } + + return &Client{ + cfg: cfg, + client: c, + }, nil +} + +func (c *Client) Balance(ctx context.Context, req *BalanceRequest) (*BalanceResponse, error) { + return c.client.Balance(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 + } +}