[#243] services/object: Share common parameters across services
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
a01262d8bd
commit
fb50362dcc
12 changed files with 89 additions and 163 deletions
|
@ -1,6 +1,9 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
||||
)
|
||||
|
@ -8,8 +11,13 @@ import (
|
|||
type CommonPrm struct {
|
||||
local bool
|
||||
|
||||
token *token.SessionToken
|
||||
token *token.SessionToken
|
||||
|
||||
bearer *token.BearerToken
|
||||
|
||||
key *ecdsa.PrivateKey
|
||||
|
||||
callOpts []client.CallOption
|
||||
}
|
||||
|
||||
func (p *CommonPrm) WithLocalOnly(v bool) *CommonPrm {
|
||||
|
@ -44,6 +52,42 @@ func (p *CommonPrm) WithBearerToken(token *token.BearerToken) *CommonPrm {
|
|||
return p
|
||||
}
|
||||
|
||||
// WithPrivateKey sets private key to use during execution.
|
||||
func (p *CommonPrm) WithPrivateKey(key *ecdsa.PrivateKey) *CommonPrm {
|
||||
if p != nil {
|
||||
p.key = key
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// PrivateKey returns private key to use during execution.
|
||||
func (p *CommonPrm) PrivateKey() *ecdsa.PrivateKey {
|
||||
if p != nil {
|
||||
return p.key
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithRemoteCallOptions sets call options remote remote client calls.
|
||||
func (p *CommonPrm) WithRemoteCallOptions(opts ...client.CallOption) *CommonPrm {
|
||||
if p != nil {
|
||||
p.callOpts = opts
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// RemoteCallOptions return call options for remote client calls.
|
||||
func (p *CommonPrm) RemoteCallOptions() []client.CallOption {
|
||||
if p != nil {
|
||||
return p.callOpts
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *CommonPrm) SessionToken() *token.SessionToken {
|
||||
if p != nil {
|
||||
return p.token
|
||||
|
@ -65,9 +109,24 @@ func CommonPrmFromV2(req interface {
|
|||
}) *CommonPrm {
|
||||
meta := req.GetMetaHeader()
|
||||
|
||||
return &CommonPrm{
|
||||
local: meta.GetTTL() <= 1, // FIXME: use constant
|
||||
token: token.NewSessionTokenFromV2(meta.GetSessionToken()),
|
||||
bearer: token.NewBearerTokenFromV2(meta.GetBearerToken()),
|
||||
prm := &CommonPrm{
|
||||
local: meta.GetTTL() <= 1, // FIXME: use constant
|
||||
token: nil,
|
||||
bearer: nil,
|
||||
callOpts: make([]client.CallOption, 0, 3),
|
||||
}
|
||||
|
||||
prm.callOpts = append(prm.callOpts, client.WithTTL(meta.GetTTL()-1))
|
||||
|
||||
if tok := meta.GetSessionToken(); tok != nil {
|
||||
prm.token = token.NewSessionTokenFromV2(tok)
|
||||
prm.callOpts = append(prm.callOpts, client.WithSession(prm.token))
|
||||
}
|
||||
|
||||
if tok := meta.GetBearerToken(); tok != nil {
|
||||
prm.bearer = token.NewBearerTokenFromV2(tok)
|
||||
prm.callOpts = append(prm.callOpts, client.WithBearer(prm.bearer))
|
||||
}
|
||||
|
||||
return prm
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue