[#48] client: Refactor ContainerList()

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-04-13 08:55:01 +03:00
parent b2c66cb99e
commit 8852b262f2

View file

@ -8,6 +8,9 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
rpcapi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc" rpcapi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature"
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
) )
@ -27,7 +30,7 @@ func (x *PrmContainerList) SetAccount(id user.ID) {
x.ownerSet = true x.ownerSet = true
} }
func (x *PrmContainerList) formRequest(_ *Client) (*v2container.ListRequest, error) { func (x *PrmContainerList) formRequest(c *Client) (*v2container.ListRequest, error) {
if !x.ownerSet { if !x.ownerSet {
return nil, errorAccountNotSet return nil, errorAccountNotSet
} }
@ -40,6 +43,7 @@ func (x *PrmContainerList) formRequest(_ *Client) (*v2container.ListRequest, err
var req v2container.ListRequest var req v2container.ListRequest
req.SetBody(reqBody) req.SetBody(reqBody)
c.prepareRequest(&req, new(v2session.RequestMetaHeader))
return &req, nil return &req, nil
} }
@ -76,38 +80,27 @@ func (c *Client) ContainerList(ctx context.Context, prm PrmContainerList) (*ResC
return nil, err return nil, err
} }
// init call context if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
return nil, fmt.Errorf("sign request: %w", err)
var (
cc contextCall
res ResContainerList
)
c.initCallContext(&cc)
cc.meta = prm.prmCommonMeta
cc.req = req
cc.statusRes = &res
cc.call = func() (responseV2, error) {
return rpcapi.ListContainers(&c.c, req, client.WithContext(ctx))
} }
cc.result = func(r responseV2) {
resp := r.(*v2container.ListResponse)
res.ids = make([]cid.ID, len(resp.GetBody().GetContainerIDs())) resp, err := rpcapi.ListContainers(&c.c, req, client.WithContext(ctx))
if err != nil {
return nil, err
}
for i, cidV2 := range resp.GetBody().GetContainerIDs() { var res ResContainerList
cc.err = res.ids[i].ReadFromV2(cidV2) res.st, err = c.processResponse(resp)
if cc.err != nil { if err != nil || !apistatus.IsSuccessful(res.st) {
cc.err = fmt.Errorf("invalid ID in the response: %w", cc.err) return &res, err
return }
}
res.ids = make([]cid.ID, len(resp.GetBody().GetContainerIDs()))
for i, cidV2 := range resp.GetBody().GetContainerIDs() {
if err := res.ids[i].ReadFromV2(cidV2); err != nil {
return &res, fmt.Errorf("invalid ID in the response: %w", err)
} }
} }
// process call
if !cc.processCall() {
return nil, cc.err
}
return &res, nil return &res, nil
} }