2020-08-25 11:51:00 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2020-09-15 10:51:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-08-25 11:51:00 +00:00
|
|
|
v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
|
|
|
v2container "github.com/nspcc-dev/neofs-api-go/v2/container"
|
2020-09-04 10:05:57 +00:00
|
|
|
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-09-15 10:51:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-08-25 11:51:00 +00:00
|
|
|
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
CallOption interface {
|
|
|
|
apply(*callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientOption interface {
|
|
|
|
apply(*clientOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
xHeader struct {
|
|
|
|
v2session.XHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
callOptions struct {
|
2020-09-10 11:29:11 +00:00
|
|
|
version *pkg.Version
|
2020-08-25 11:51:00 +00:00
|
|
|
xHeaders []xHeader
|
|
|
|
ttl uint32
|
|
|
|
epoch uint64
|
2020-09-15 10:51:36 +00:00
|
|
|
session *token.SessionToken
|
2020-08-25 11:51:00 +00:00
|
|
|
// add bearer token
|
|
|
|
}
|
|
|
|
|
|
|
|
clientOptions struct {
|
|
|
|
addr string
|
|
|
|
|
|
|
|
grpcOpts *grpcOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcOptions struct {
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
v2ContainerClient *v2container.Client
|
|
|
|
v2AccountingClient *v2accounting.Client
|
2020-09-15 10:49:20 +00:00
|
|
|
v2SessionClient *v2session.Client
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
objectClientV2 *v2object.Client
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
2020-09-15 10:51:36 +00:00
|
|
|
|
|
|
|
v2SessionReqInfo struct {
|
|
|
|
addr *refs.Address
|
|
|
|
verb v2session.ObjectSessionVerb
|
|
|
|
|
|
|
|
exp, nbf, iat uint64
|
|
|
|
}
|
2020-08-25 11:51:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func defaultCallOptions() callOptions {
|
|
|
|
return callOptions{
|
|
|
|
ttl: 2,
|
2020-09-10 11:29:11 +00:00
|
|
|
version: pkg.SDKVersion(),
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type funcCallOption struct {
|
|
|
|
f func(*callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fco *funcCallOption) apply(co *callOptions) {
|
|
|
|
fco.f(co)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFuncCallOption(f func(option *callOptions)) *funcCallOption {
|
|
|
|
return &funcCallOption{
|
|
|
|
f: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithXHeader(key, value string) CallOption {
|
|
|
|
return newFuncCallOption(func(option *callOptions) {
|
|
|
|
xhdr := new(v2session.XHeader)
|
|
|
|
xhdr.SetKey(key)
|
|
|
|
xhdr.SetValue(value)
|
|
|
|
|
|
|
|
option.xHeaders = append(option.xHeaders, xHeader{
|
|
|
|
XHeader: *xhdr,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithTTL(ttl uint32) CallOption {
|
|
|
|
return newFuncCallOption(func(option *callOptions) {
|
|
|
|
option.ttl = ttl
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithEpoch(epoch uint64) CallOption {
|
|
|
|
return newFuncCallOption(func(option *callOptions) {
|
|
|
|
option.epoch = epoch
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
func WithSession(token *token.SessionToken) CallOption {
|
|
|
|
return newFuncCallOption(func(option *callOptions) {
|
|
|
|
option.session = token
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-25 11:51:00 +00:00
|
|
|
func v2MetaHeaderFromOpts(options callOptions) *v2session.RequestMetaHeader {
|
|
|
|
meta := new(v2session.RequestMetaHeader)
|
2020-09-10 11:29:11 +00:00
|
|
|
meta.SetVersion(options.version.ToV2())
|
2020-08-25 11:51:00 +00:00
|
|
|
meta.SetTTL(options.ttl)
|
|
|
|
meta.SetEpoch(options.epoch)
|
|
|
|
|
|
|
|
xhdrs := make([]*v2session.XHeader, len(options.xHeaders))
|
|
|
|
for i := range options.xHeaders {
|
|
|
|
xhdrs[i] = &options.xHeaders[i].XHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.SetXHeaders(xhdrs)
|
|
|
|
|
|
|
|
return meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultClientOptions() *clientOptions {
|
|
|
|
return &clientOptions{
|
|
|
|
grpcOpts: new(grpcOptions),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type funcClientOption struct {
|
|
|
|
f func(*clientOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fco *funcClientOption) apply(co *clientOptions) {
|
|
|
|
fco.f(co)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFuncClientOption(f func(option *clientOptions)) *funcClientOption {
|
|
|
|
return &funcClientOption{
|
|
|
|
f: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithAddress(addr string) ClientOption {
|
|
|
|
return newFuncClientOption(func(option *clientOptions) {
|
|
|
|
option.addr = addr
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithGRPCConnection(grpcConn *grpc.ClientConn) ClientOption {
|
|
|
|
return newFuncClientOption(func(option *clientOptions) {
|
|
|
|
option.grpcOpts.conn = grpcConn
|
|
|
|
})
|
|
|
|
}
|