From 40d966bec2a2cbf3d3a0962f692117a633f96ece Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 14 Apr 2023 15:56:40 +0300 Subject: [PATCH] [#48] client: Refactor NetworkInfo() Signed-off-by: Evgenii Stratonikov --- client/netmap.go | 71 ++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/client/netmap.go b/client/netmap.go index 3fb57b8..9c9c44b 100644 --- a/client/netmap.go +++ b/client/netmap.go @@ -120,6 +120,16 @@ type PrmNetworkInfo struct { prmCommonMeta } +func (x PrmNetworkInfo) buildRequest(c *Client) (*v2netmap.NetworkInfoRequest, error) { + meta := new(v2session.RequestMetaHeader) + writeXHeadersToMeta(x.xHeaders, meta) + + var req v2netmap.NetworkInfoRequest + req.SetBody(new(v2netmap.NetworkInfoRequestBody)) + c.prepareRequest(&req, meta) + return &req, nil +} + // ResNetworkInfo groups resulting values of NetworkInfo operation. type ResNetworkInfo struct { statusRes @@ -148,46 +158,35 @@ func (x ResNetworkInfo) Info() netmap.NetworkInfo { // Return statuses: // - global (see Client docs). func (c *Client) NetworkInfo(ctx context.Context, prm PrmNetworkInfo) (*ResNetworkInfo, error) { - // form request - var req v2netmap.NetworkInfoRequest - - // init call context - - var ( - cc contextCall - res ResNetworkInfo - ) - - c.initCallContext(&cc) - cc.meta = prm.prmCommonMeta - cc.req = &req - cc.statusRes = &res - cc.call = func() (responseV2, error) { - return rpcapi.NetworkInfo(&c.c, &req, client.WithContext(ctx)) - } - cc.result = func(r responseV2) { - resp := r.(*v2netmap.NetworkInfoResponse) - - const fieldNetInfo = "network info" - - netInfoV2 := resp.GetBody().GetNetworkInfo() - if netInfoV2 == nil { - cc.err = newErrMissingResponseField(fieldNetInfo) - return - } - - cc.err = res.info.ReadFromV2(*netInfoV2) - if cc.err != nil { - cc.err = newErrInvalidResponseField(fieldNetInfo, cc.err) - return - } + req, err := prm.buildRequest(c) + if err != nil { + return nil, err } - // process call - if !cc.processCall() { - return nil, cc.err + if err := signature.SignServiceMessage(&c.prm.key, req); err != nil { + return nil, fmt.Errorf("sign request: %w", err) } + resp, err := rpcapi.NetworkInfo(&c.c, req, client.WithContext(ctx)) + if err != nil { + return nil, err + } + + var res ResNetworkInfo + res.st, err = c.processResponse(resp) + if err != nil || !apistatus.IsSuccessful(res.st) { + return &res, err + } + + const fieldNetInfo = "network info" + + netInfoV2 := resp.GetBody().GetNetworkInfo() + if netInfoV2 == nil { + return nil, newErrMissingResponseField(fieldNetInfo) + } + if err := res.info.ReadFromV2(*netInfoV2); err != nil { + return nil, newErrInvalidResponseField(fieldNetInfo, err) + } return &res, nil }