From a16fc40c3914efbccda70b9808e8fad0d40d3f3f Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 14 Apr 2023 16:32:22 +0300 Subject: [PATCH] [#48] client: Refactor EndpointInfo() Signed-off-by: Evgenii Stratonikov --- client/netmap.go | 99 +++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/client/netmap.go b/client/netmap.go index 9c9c44b..b63ba65 100644 --- a/client/netmap.go +++ b/client/netmap.go @@ -19,6 +19,16 @@ type PrmEndpointInfo struct { prmCommonMeta } +func (x *PrmEndpointInfo) buildRequest(c *Client) (*v2netmap.LocalNodeInfoRequest, error) { + meta := new(v2session.RequestMetaHeader) + writeXHeadersToMeta(x.xHeaders, meta) + + req := new(v2netmap.LocalNodeInfoRequest) + req.SetBody(new(v2netmap.LocalNodeInfoRequestBody)) + c.prepareRequest(req, meta) + return req, nil +} + // ResEndpointInfo group resulting values of EndpointInfo operation. type ResEndpointInfo struct { statusRes @@ -56,62 +66,47 @@ func (x ResEndpointInfo) NodeInfo() netmap.NodeInfo { // Return statuses: // - global (see Client docs). func (c *Client) EndpointInfo(ctx context.Context, prm PrmEndpointInfo) (*ResEndpointInfo, error) { - // form request - var req v2netmap.LocalNodeInfoRequest - - // init call context - - var ( - cc contextCall - res ResEndpointInfo - ) - - c.initCallContext(&cc) - cc.meta = prm.prmCommonMeta - cc.req = &req - cc.statusRes = &res - cc.call = func() (responseV2, error) { - return rpcapi.LocalNodeInfo(&c.c, &req, client.WithContext(ctx)) - } - cc.result = func(r responseV2) { - resp := r.(*v2netmap.LocalNodeInfoResponse) - - body := resp.GetBody() - - const fieldVersion = "version" - - verV2 := body.GetVersion() - if verV2 == nil { - cc.err = newErrMissingResponseField(fieldVersion) - return - } - - cc.err = res.version.ReadFromV2(*verV2) - if cc.err != nil { - cc.err = newErrInvalidResponseField(fieldVersion, cc.err) - return - } - - const fieldNodeInfo = "node info" - - nodeInfoV2 := body.GetNodeInfo() - if nodeInfoV2 == nil { - cc.err = newErrMissingResponseField(fieldNodeInfo) - return - } - - cc.err = res.ni.ReadFromV2(*nodeInfoV2) - if cc.err != nil { - cc.err = newErrInvalidResponseField(fieldNodeInfo, 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.LocalNodeInfo(&c.c, req, client.WithContext(ctx)) + if err != nil { + return nil, err + } + + var res ResEndpointInfo + res.st, err = c.processResponse(resp) + if err != nil || !apistatus.IsSuccessful(res.st) { + return &res, err + } + + body := resp.GetBody() + + const fieldVersion = "version" + + verV2 := body.GetVersion() + if verV2 == nil { + return nil, newErrMissingResponseField(fieldVersion) + } + if err := res.version.ReadFromV2(*verV2); err != nil { + return nil, newErrInvalidResponseField(fieldVersion, err) + } + + const fieldNodeInfo = "node info" + + nodeInfoV2 := body.GetNodeInfo() + if nodeInfoV2 == nil { + return nil, newErrMissingResponseField(fieldNodeInfo) + } + if err := res.ni.ReadFromV2(*nodeInfoV2); err != nil { + return nil, newErrInvalidResponseField(fieldNodeInfo, err) + } return &res, nil }