Skip to content

Commit

Permalink
[#131] client: Add GetFullObject helper function
Browse files Browse the repository at this point in the history
Add helper function which accepts container and object identifiers and
returns object instance read to the memory.

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
Leonard Lyubich authored and cthulhu-rider committed Feb 18, 2022
1 parent 32458ba commit b0f7f75
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions client/object_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,44 @@ func (c *Client) ObjectGetInit(ctx context.Context, prm PrmObjectGet) (*ObjectRe
return &r, nil
}

// GetFullObject reads object header and full payload to the memory and returns
// the result.
//
// Function behavior equivalent to Client.ObjectGetInit, see documentation for details.
func GetFullObject(ctx context.Context, c *Client, idCnr cid.ID, idObj oid.ID) (*object.Object, error) {
var prm PrmObjectGet

prm.FromContainer(idCnr)
prm.ByID(idObj)

rdr, err := c.ObjectGetInit(ctx, prm)
if err != nil {
return nil, fmt.Errorf("init object reading: %w", err)
}

var obj object.Object

if rdr.ReadHeader(&obj) {
payload, err := io.ReadAll(rdr)
if err != nil {
return nil, fmt.Errorf("read payload: %w", err)
}

object.NewRawFrom(&obj).SetPayload(payload)
}

res, err := rdr.Close()
if err == nil {
err = apistatus.ErrFromStatus(res.Status())
}

if err != nil {
return nil, fmt.Errorf("finish object reading: %w", err)
}

return &obj, nil
}

// PrmObjectHead groups parameters of ObjectHead operation.
type PrmObjectHead struct {
prmObjectRead
Expand Down

0 comments on commit b0f7f75

Please sign in to comment.