[#312] client/netmap: Simplify NetMapSnapshot implementaiton

Make flat set of instructions similar to `ObjectDelete` operation.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-09-20 15:28:21 +04:00 committed by LeL
parent 664392afc2
commit 89124d442d

View file

@ -2,10 +2,14 @@ package client
import ( import (
"context" "context"
"fmt"
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap" v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc" rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "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/netmap"
"github.com/nspcc-dev/neofs-sdk-go/version" "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. // PrmNetMapSnapshot groups parameters of NetMapSnapshot operation.
type PrmNetMapSnapshot struct { type PrmNetMapSnapshot struct {
prmCommonMeta
} }
// ResNetMapSnapshot groups resulting values of NetMapSnapshot operation. // ResNetMapSnapshot groups resulting values of NetMapSnapshot operation.
@ -228,50 +231,53 @@ func (x ResNetMapSnapshot) NetMap() netmap.NetMap {
// //
// Return statuses: // Return statuses:
// - global (see Client docs). // - 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 // check context
if ctx == nil { if ctx == nil {
panic(panicMsgMissingContext) panic(panicMsgMissingContext)
} }
// form request body
var body v2netmap.SnapshotRequestBody
// form meta header
var meta v2session.RequestMetaHeader
// form request // form request
var req v2netmap.SnapshotRequest var req v2netmap.SnapshotRequest
req.SetBody(&body)
c.prepareRequest(&req, &meta)
// init call context err := signature.SignServiceMessage(&c.prm.key, &req)
if err != nil {
var ( return nil, fmt.Errorf("sign request: %w", err)
cc contextCall }
res ResNetMapSnapshot
) resp, err := rpcapi.NetMapSnapshot(&c.c, &req, client.WithContext(ctx))
if err != nil {
c.initCallContext(&cc) return nil, fmt.Errorf("rpc failure: %w", err)
cc.meta = prm.prmCommonMeta }
cc.req = &req
cc.statusRes = &res var res ResNetMapSnapshot
cc.call = func() (responseV2, error) { res.st, err = c.processResponse(resp)
return rpcapi.NetMapSnapshot(&c.c, &req, client.WithContext(ctx)) if err != nil {
return nil, err
}
if !apistatus.IsSuccessful(res.st) {
return &res, nil
} }
cc.result = func(r responseV2) {
resp := r.(*v2netmap.SnapshotResponse)
const fieldNetMap = "network map" const fieldNetMap = "network map"
netMapV2 := resp.GetBody().NetMap() netMapV2 := resp.GetBody().NetMap()
if netMapV2 == nil { if netMapV2 == nil {
cc.err = newErrMissingResponseField(fieldNetMap) return nil, newErrMissingResponseField(fieldNetMap)
return
} }
cc.err = res.netMap.ReadFromV2(*netMapV2) err = res.netMap.ReadFromV2(*netMapV2)
if cc.err != nil { if err != nil {
cc.err = newErrInvalidResponseField(fieldNetMap, cc.err) return nil, newErrInvalidResponseField(fieldNetMap, err)
return
}
}
// process call
if !cc.processCall() {
return nil, cc.err
} }
return &res, nil return &res, nil