From 89124d442dd75b862276d5209379943c899ee5dc Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 20 Sep 2022 15:28:21 +0400 Subject: [PATCH] [#312] client/netmap: Simplify `NetMapSnapshot` implementaiton Make flat set of instructions similar to `ObjectDelete` operation. Signed-off-by: Leonard Lyubich --- client/netmap.go | 76 ++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/client/netmap.go b/client/netmap.go index 98d01a0..bacba89 100644 --- a/client/netmap.go +++ b/client/netmap.go @@ -2,10 +2,14 @@ package client import ( "context" + "fmt" v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap" rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc" "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" + v2session "github.com/nspcc-dev/neofs-api-go/v2/session" + "github.com/nspcc-dev/neofs-api-go/v2/signature" + apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" "github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/nspcc-dev/neofs-sdk-go/version" ) @@ -199,7 +203,6 @@ func (c *Client) NetworkInfo(ctx context.Context, prm PrmNetworkInfo) (*ResNetwo // PrmNetMapSnapshot groups parameters of NetMapSnapshot operation. type PrmNetMapSnapshot struct { - prmCommonMeta } // ResNetMapSnapshot groups resulting values of NetMapSnapshot operation. @@ -228,50 +231,53 @@ func (x ResNetMapSnapshot) NetMap() netmap.NetMap { // // Return statuses: // - global (see Client docs). -func (c *Client) NetMapSnapshot(ctx context.Context, prm PrmNetMapSnapshot) (*ResNetMapSnapshot, error) { +func (c *Client) NetMapSnapshot(ctx context.Context, _ PrmNetMapSnapshot) (*ResNetMapSnapshot, error) { // check context if ctx == nil { panic(panicMsgMissingContext) } + // form request body + var body v2netmap.SnapshotRequestBody + + // form meta header + var meta v2session.RequestMetaHeader + // form request var req v2netmap.SnapshotRequest + req.SetBody(&body) + c.prepareRequest(&req, &meta) - // init call context - - var ( - cc contextCall - res ResNetMapSnapshot - ) - - c.initCallContext(&cc) - cc.meta = prm.prmCommonMeta - cc.req = &req - cc.statusRes = &res - cc.call = func() (responseV2, error) { - return rpcapi.NetMapSnapshot(&c.c, &req, client.WithContext(ctx)) - } - cc.result = func(r responseV2) { - resp := r.(*v2netmap.SnapshotResponse) - - const fieldNetMap = "network map" - - netMapV2 := resp.GetBody().NetMap() - if netMapV2 == nil { - cc.err = newErrMissingResponseField(fieldNetMap) - return - } - - cc.err = res.netMap.ReadFromV2(*netMapV2) - if cc.err != nil { - cc.err = newErrInvalidResponseField(fieldNetMap, cc.err) - return - } + err := signature.SignServiceMessage(&c.prm.key, &req) + if err != nil { + return nil, fmt.Errorf("sign request: %w", err) } - // process call - if !cc.processCall() { - return nil, cc.err + resp, err := rpcapi.NetMapSnapshot(&c.c, &req, client.WithContext(ctx)) + if err != nil { + return nil, fmt.Errorf("rpc failure: %w", err) + } + + var res ResNetMapSnapshot + res.st, err = c.processResponse(resp) + if err != nil { + return nil, err + } + + if !apistatus.IsSuccessful(res.st) { + return &res, nil + } + + const fieldNetMap = "network map" + + netMapV2 := resp.GetBody().NetMap() + if netMapV2 == nil { + return nil, newErrMissingResponseField(fieldNetMap) + } + + err = res.netMap.ReadFromV2(*netMapV2) + if err != nil { + return nil, newErrInvalidResponseField(fieldNetMap, err) } return &res, nil