Skip to content

Commit

Permalink
[nspcc-dev#1194] client: Support request X-headers
Browse files Browse the repository at this point in the history
Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
Leonard Lyubich authored and aprasolova committed Mar 5, 2022
1 parent 372ff06 commit 4ed627b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
14 changes: 14 additions & 0 deletions cmd/neofs-cli/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ func PutObject(prm PutObjectPrm) (*PutObjectRes, error) {
wrt.MarkLocal()
}

wrt.WithXHeaders(prm.xHeadersPrm()...)

if wrt.WriteHeader(*prm.hdr) {
sz := prm.hdr.PayloadSize()

Expand Down Expand Up @@ -434,6 +436,8 @@ func DeleteObject(prm DeleteObjectPrm) (*DeleteObjectRes, error) {
delPrm.WithBearerToken(*prm.bearerToken)
}

delPrm.WithXHeaders(prm.xHeadersPrm()...)

cliRes, err := prm.cli.ObjectDelete(context.Background(), delPrm)
if err != nil {
return nil, fmt.Errorf("remove object via client: %w", err)
Expand Down Expand Up @@ -510,6 +514,8 @@ func GetObject(prm GetObjectPrm) (*GetObjectRes, error) {
getPrm.MarkLocal()
}

getPrm.WithXHeaders(prm.xHeadersPrm()...)

rdr, err := prm.cli.ObjectGetInit(context.Background(), getPrm)
if err != nil {
return nil, fmt.Errorf("init object reading on client: %w", err)
Expand Down Expand Up @@ -586,6 +592,8 @@ func HeadObject(prm HeadObjectPrm) (*HeadObjectRes, error) {
cliPrm.MarkLocal()
}

cliPrm.WithXHeaders(prm.xHeadersPrm()...)

res, err := prm.cli.ObjectHead(context.Background(), cliPrm)
if err != nil {
return nil, fmt.Errorf("read object header via client: %w", err)
Expand Down Expand Up @@ -649,6 +657,8 @@ func SearchObjects(prm SearchObjectsPrm) (*SearchObjectsRes, error) {
cliPrm.MarkLocal()
}

cliPrm.WithXHeaders(prm.xHeadersPrm()...)

rdr, err := prm.cli.ObjectSearchInit(context.Background(), cliPrm)
if err != nil {
return nil, fmt.Errorf("init object search: %w", err)
Expand Down Expand Up @@ -760,6 +770,8 @@ func HashPayloadRanges(prm HashPayloadRangesPrm) (*HashPayloadRangesRes, error)
cliPrm.WithBearerToken(*prm.bearerToken)
}

cliPrm.WithXHeaders(prm.xHeadersPrm()...)

res, err := prm.cli.ObjectHash(context.Background(), cliPrm)
if err != nil {
return nil, fmt.Errorf("read payload hashes via client: %w", err)
Expand Down Expand Up @@ -816,6 +828,8 @@ func PayloadRange(prm PayloadRangePrm) (*PayloadRangeRes, error) {
cliPrm.SetOffset(prm.rng.GetOffset())
cliPrm.SetLength(prm.rng.GetLength())

cliPrm.WithXHeaders(prm.xHeadersPrm()...)

rdr, err := prm.cli.ObjectRangeInit(context.Background(), cliPrm)
if err != nil {
return nil, fmt.Errorf("init payload reading: %w", err)
Expand Down
19 changes: 17 additions & 2 deletions cmd/neofs-cli/internal/client/prm.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type commonObjectPrm struct {
bearerTokenPrm

local bool

xHeaders []*session.XHeader
}

// SetTTL sets request TTL value.
Expand All @@ -88,6 +90,19 @@ func (x *commonObjectPrm) SetTTL(ttl uint32) {
}

// SetXHeaders sets request X-Headers.
func (x *commonObjectPrm) SetXHeaders(_ []*session.XHeader) {
// FIXME: (neofs-node#1194) not supported by client
func (x *commonObjectPrm) SetXHeaders(hs []*session.XHeader) {
x.xHeaders = hs
}

func (x commonObjectPrm) xHeadersPrm() (res []string) {
if x.xHeaders != nil {
res = make([]string, len(x.xHeaders)*2)

for i := range x.xHeaders {
res[2*i] = x.xHeaders[i].Key()
res[2*i+1] = x.xHeaders[i].Value()
}
}

return
}
28 changes: 26 additions & 2 deletions pkg/services/object/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type commonPrm struct {
tokenBearer *token.BearerToken

local bool

xHeaders []*session.XHeader
}

// SetClient sets base client for NeoFS API communication.
Expand Down Expand Up @@ -75,8 +77,21 @@ func (x *commonPrm) SetTTL(ttl uint32) {
// SetXHeaders sets request X-Headers.
//
// By default X-Headers will not be attached to the request.
func (x *commonPrm) SetXHeaders(_ []*session.XHeader) {
// FIXME: (neofs-node#1194) not supported by client
func (x *commonPrm) SetXHeaders(hs []*session.XHeader) {
x.xHeaders = hs
}

func (x commonPrm) xHeadersPrm() (res []string) {
if len(x.xHeaders) > 0 {
res = make([]string, len(x.xHeaders)*2)

for i := range x.xHeaders {
res[2*i] = x.xHeaders[i].Key()
res[2*i+1] = x.xHeaders[i].Value()
}
}

return
}

type readPrmCommon struct {
Expand Down Expand Up @@ -148,6 +163,8 @@ func GetObject(prm GetObjectPrm) (*GetObjectRes, error) {
prm.cliPrm.MarkLocal()
}

prm.cliPrm.WithXHeaders(prm.xHeadersPrm()...)

rdr, err := prm.cli.ObjectGetInit(prm.ctx, prm.cliPrm)
if err == nil {
return nil, fmt.Errorf("init object reading: %w", err)
Expand Down Expand Up @@ -243,6 +260,8 @@ func HeadObject(prm HeadObjectPrm) (*HeadObjectRes, error) {
prm.cliPrm.WithBearerToken(*prm.tokenBearer)
}

prm.cliPrm.WithXHeaders(prm.xHeadersPrm()...)

cliRes, err := prm.cli.ObjectHead(prm.ctx, prm.cliPrm)
if err == nil {
// pull out an error from status
Expand Down Expand Up @@ -335,6 +354,7 @@ func PayloadRange(prm PayloadRangePrm) (*PayloadRangeRes, error) {
}

prm.cliPrm.SetLength(prm.ln)
prm.cliPrm.WithXHeaders(prm.xHeadersPrm()...)

rdr, err := prm.cli.ObjectRangeInit(prm.ctx, prm.cliPrm)
if err != nil {
Expand Down Expand Up @@ -406,6 +426,8 @@ func PutObject(prm PutObjectPrm) (*PutObjectRes, error) {
w.WithBearerToken(*prm.tokenBearer)
}

w.WithXHeaders(prm.xHeadersPrm()...)

if w.WriteHeader(*prm.obj) {
w.WritePayloadChunk(prm.obj.Payload())
}
Expand Down Expand Up @@ -476,6 +498,8 @@ func SearchObjects(prm SearchObjectsPrm) (*SearchObjectsRes, error) {
prm.cliPrm.WithBearerToken(*prm.tokenBearer)
}

prm.cliPrm.WithXHeaders(prm.xHeadersPrm()...)

rdr, err := prm.cli.ObjectSearchInit(prm.ctx, prm.cliPrm)
if err != nil {
return nil, fmt.Errorf("init object searching in client: %w", err)
Expand Down

0 comments on commit 4ed627b

Please sign in to comment.