From 8e2f77890fe4d83981951626f123227b27225639 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 13 Apr 2023 08:58:14 +0300 Subject: [PATCH] [#48] client: Refactor ContainerGet() Signed-off-by: Evgenii Stratonikov --- client/container_get.go | 54 ++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/client/container_get.go b/client/container_get.go index e91ccdf..2d2d2cb 100644 --- a/client/container_get.go +++ b/client/container_get.go @@ -9,6 +9,9 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs" rpcapi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc" "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" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container" cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" ) @@ -28,7 +31,7 @@ func (x *PrmContainerGet) SetContainer(id cid.ID) { x.idSet = true } -func (x *PrmContainerGet) formRequest(_ *Client) (*v2container.GetRequest, error) { +func (x *PrmContainerGet) formRequest(c *Client) (*v2container.GetRequest, error) { if !x.idSet { return nil, errorMissingContainer } @@ -41,6 +44,7 @@ func (x *PrmContainerGet) formRequest(_ *Client) (*v2container.GetRequest, error var req v2container.GetRequest req.SetBody(reqBody) + c.prepareRequest(&req, new(v2session.RequestMetaHeader)) return &req, nil } @@ -78,39 +82,27 @@ func (c *Client) ContainerGet(ctx context.Context, prm PrmContainerGet) (*ResCon return nil, err } - // init call context - - var ( - cc contextCall - res ResContainerGet - ) - - c.initCallContext(&cc) - cc.meta = prm.prmCommonMeta - cc.req = req - cc.statusRes = &res - cc.call = func() (responseV2, error) { - return rpcapi.GetContainer(&c.c, req, client.WithContext(ctx)) - } - cc.result = func(r responseV2) { - resp := r.(*v2container.GetResponse) - - cnrV2 := resp.GetBody().GetContainer() - if cnrV2 == nil { - cc.err = errors.New("missing container in response") - return - } - - cc.err = res.cnr.ReadFromV2(*cnrV2) - if cc.err != nil { - cc.err = fmt.Errorf("invalid container in response: %w", cc.err) - } + if err := signature.SignServiceMessage(&c.prm.key, req); err != nil { + return nil, fmt.Errorf("sign request: %w", err) } - // process call - if !cc.processCall() { - return nil, cc.err + resp, err := rpcapi.GetContainer(&c.c, req, client.WithContext(ctx)) + if err != nil { + return nil, err } + var res ResContainerGet + res.st, err = c.processResponse(resp) + if err != nil || !apistatus.IsSuccessful(res.st) { + return &res, err + } + + cnrV2 := resp.GetBody().GetContainer() + if cnrV2 == nil { + return &res, errors.New("missing container in response") + } + if err := res.cnr.ReadFromV2(*cnrV2); err != nil { + return &res, fmt.Errorf("invalid container in response: %w", err) + } return &res, nil }