2021-11-09 08:07:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"time"
|
|
|
|
|
2021-11-16 18:15:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
2021-11-09 08:07:49 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Option func(*clientOptions)
|
|
|
|
|
|
|
|
clientOptions struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
rawOpts []client.Option
|
|
|
|
|
|
|
|
cbRespInfo func(ResponseMetaInfo) error
|
2022-01-11 12:32:29 +00:00
|
|
|
|
|
|
|
// defines if client parses erroneous NeoFS
|
|
|
|
// statuses and returns them as `error`
|
|
|
|
//
|
|
|
|
// default is false
|
|
|
|
parseNeoFSErrors bool
|
2022-01-14 10:38:51 +00:00
|
|
|
|
|
|
|
netMagic uint64
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func defaultClientOptions() *clientOptions {
|
|
|
|
return &clientOptions{
|
|
|
|
rawOpts: make([]client.Option, 0, 4),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress returns option to specify
|
|
|
|
// network address of the remote server.
|
|
|
|
//
|
|
|
|
// Ignored if WithGRPCConnection is provided.
|
|
|
|
func WithAddress(addr string) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithNetworkAddress(addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDialTimeout returns option to set connection timeout to the remote node.
|
|
|
|
//
|
|
|
|
// Ignored if WithGRPCConn is provided.
|
|
|
|
func WithDialTimeout(dur time.Duration) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithDialTimeout(dur))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 11:24:13 +00:00
|
|
|
// WithRWTimeout returns option to set timeout for single read and write
|
|
|
|
// operation on protobuf message.
|
|
|
|
func WithRWTimeout(dur time.Duration) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithRWTimeout(dur))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 08:07:49 +00:00
|
|
|
// WithTLSConfig returns option to set connection's TLS config to the remote node.
|
|
|
|
//
|
|
|
|
// Ignored if WithGRPCConnection is provided.
|
|
|
|
func WithTLSConfig(cfg *tls.Config) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithTLSCfg(cfg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDefaultPrivateKey returns option to set default private key
|
|
|
|
// used for the work.
|
|
|
|
func WithDefaultPrivateKey(key *ecdsa.PrivateKey) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.key = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithURIAddress returns option to specify
|
|
|
|
// network address of a remote server and connection
|
|
|
|
// scheme for it.
|
|
|
|
//
|
|
|
|
// Format of the URI:
|
|
|
|
//
|
|
|
|
// [scheme://]host:port
|
|
|
|
//
|
|
|
|
// Supported schemes:
|
|
|
|
// - grpc;
|
|
|
|
// - grpcs.
|
|
|
|
//
|
|
|
|
// tls.Cfg second argument is optional and is taken into
|
|
|
|
// account only in case of `grpcs` scheme.
|
|
|
|
//
|
|
|
|
// Falls back to WithNetworkAddress if address is not a valid URI.
|
|
|
|
//
|
|
|
|
// Do not use along with WithAddress and WithTLSConfig.
|
|
|
|
//
|
|
|
|
// Ignored if WithGRPCConnection is provided.
|
|
|
|
func WithURIAddress(addr string, tlsCfg *tls.Config) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithNetworkURIAddress(addr, tlsCfg)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithGRPCConnection returns option to set GRPC connection to
|
|
|
|
// the remote node.
|
|
|
|
func WithGRPCConnection(grpcConn *grpc.ClientConn) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithGRPCConn(grpcConn))
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 12:32:29 +00:00
|
|
|
|
|
|
|
// WithNeoFSErrorParsing returns option that makes client parse
|
|
|
|
// erroneous NeoFS statuses and return them as `error` of the method
|
|
|
|
// call.
|
|
|
|
func WithNeoFSErrorParsing() Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.parseNeoFSErrors = true
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 10:38:51 +00:00
|
|
|
|
|
|
|
// WithNetworkMagic returns option to specify NeoFS network magic.
|
|
|
|
func WithNetworkMagic(magic uint64) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.netMagic = magic
|
|
|
|
}
|
|
|
|
}
|