github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/session/grpc/client.go (about) 1 package session 2 3 import ( 4 "context" 5 "errors" 6 7 "google.golang.org/grpc" 8 ) 9 10 // Client wraps SessionServiceClient 11 // with pre-defined configurations. 12 type Client struct { 13 *cfg 14 15 client SessionServiceClient 16 } 17 18 // Option represents Client option. 19 type Option func(*cfg) 20 21 type cfg struct { 22 callOpts []grpc.CallOption 23 } 24 25 // ErrNilSessionServiceClient is returned by functions that expect 26 // a non-nil SessionServiceClient, but received nil. 27 var ErrNilSessionServiceClient = errors.New("session gRPC client is nil") 28 29 func defaultCfg() *cfg { 30 return new(cfg) 31 } 32 33 // NewClient creates, initializes and returns a new Client instance. 34 // 35 // Options are applied one by one in order. 36 func NewClient(c SessionServiceClient, opts ...Option) (*Client, error) { 37 if c == nil { 38 return nil, ErrNilSessionServiceClient 39 } 40 41 cfg := defaultCfg() 42 for i := range opts { 43 opts[i](cfg) 44 } 45 46 return &Client{ 47 cfg: cfg, 48 client: c, 49 }, nil 50 } 51 52 func (c *Client) Create(ctx context.Context, req *CreateRequest) (*CreateResponse, error) { 53 return c.client.Create(ctx, req, c.callOpts...) 54 } 55 56 // WithCallOptions returns Option that configures 57 // Client to attach call options to each rpc call. 58 func WithCallOptions(opts []grpc.CallOption) Option { 59 return func(c *cfg) { 60 c.callOpts = opts 61 } 62 }