frostfs-node/cmd/neofs-cli/internal/client/prm.go
Leonard Lyubich bbc2b873ab [#950] cli: Refactor usage of NeoFS API client
The client needs of the CLI application are limited and change not often.
Interface changes of the client library should not affect the operation of
various application packages, if they do not change their requirements for
the provided functionality. To localize the use of the base client and
facilitate further support, an auxiliary package is implemented that will
only be used by the CLI application.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2021-11-03 18:26:36 +03:00

114 lines
2.4 KiB
Go

package internal
import (
"crypto/ecdsa"
"io"
"github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/pkg/session"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
)
// here are small structures with public setters to share between parameter structures
type commonPrm struct {
cli client.Client
privKey *ecdsa.PrivateKey
}
// SetClient sets base client for NeoFS API communication.
func (x *commonPrm) SetClient(cli client.Client) {
x.cli = cli
}
// SetKey sets private key to sign the request(s).
func (x *commonPrm) SetKey(key *ecdsa.PrivateKey) {
x.privKey = key
}
type ownerIDPrm struct {
ownerID *owner.ID
}
// SetOwner sets identifier of NeoFS user.
func (x *ownerIDPrm) SetOwner(id *owner.ID) {
x.ownerID = id
}
type containerIDPrm struct {
cnrID *cid.ID
}
// SetContainerID sets container identifier.
func (x *containerIDPrm) SetContainerID(id *cid.ID) {
x.cnrID = id
}
type sessionTokenPrm struct {
sessionToken *session.Token
}
// SetSessionToken sets token of the session within which request should be sent.
func (x *sessionTokenPrm) SetSessionToken(tok *session.Token) {
x.sessionToken = tok
}
type bearerTokenPrm struct {
bearerToken *token.BearerToken
}
// SetBearerToken sets bearer token to be attached to the request.
func (x *bearerTokenPrm) SetBearerToken(tok *token.BearerToken) {
x.bearerToken = tok
}
type objectAddressPrm struct {
objAddr *object.Address
}
func (x *objectAddressPrm) SetAddress(addr *object.Address) {
x.objAddr = addr
}
type rawPrm struct {
raw bool
}
// SetRawFlag sets flag of raw request.
func (x *rawPrm) SetRawFlag(raw bool) {
x.raw = raw
}
type payloadWriterPrm struct {
wrt io.Writer
}
// SetPayloadWriter sets writer of the object payload.
func (x *payloadWriterPrm) SetPayloadWriter(wrt io.Writer) {
x.wrt = wrt
}
type commonObjectPrm struct {
commonPrm
sessionTokenPrm
bearerTokenPrm
opts []client.CallOption
}
// SetTTL sets request TTL value.
func (x *commonObjectPrm) SetTTL(ttl uint32) {
x.opts = append(x.opts, client.WithTTL(ttl))
}
// SetXHeaders sets request X-Headers.
func (x *commonObjectPrm) SetXHeaders(xhdrs []*pkg.XHeader) {
for _, xhdr := range xhdrs {
x.opts = append(x.opts, client.WithXHeader(xhdr))
}
}