[#48] client: Refactor NetworkInfo()
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
d0c5d837d2
commit
40d966bec2
1 changed files with 35 additions and 36 deletions
|
@ -120,6 +120,16 @@ type PrmNetworkInfo struct {
|
||||||
prmCommonMeta
|
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.
|
// ResNetworkInfo groups resulting values of NetworkInfo operation.
|
||||||
type ResNetworkInfo struct {
|
type ResNetworkInfo struct {
|
||||||
statusRes
|
statusRes
|
||||||
|
@ -148,46 +158,35 @@ func (x ResNetworkInfo) Info() netmap.NetworkInfo {
|
||||||
// Return statuses:
|
// Return statuses:
|
||||||
// - global (see Client docs).
|
// - global (see Client docs).
|
||||||
func (c *Client) NetworkInfo(ctx context.Context, prm PrmNetworkInfo) (*ResNetworkInfo, error) {
|
func (c *Client) NetworkInfo(ctx context.Context, prm PrmNetworkInfo) (*ResNetworkInfo, error) {
|
||||||
// form request
|
req, err := prm.buildRequest(c)
|
||||||
var req v2netmap.NetworkInfoRequest
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// process call
|
if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
|
||||||
if !cc.processCall() {
|
return nil, fmt.Errorf("sign request: %w", err)
|
||||||
return nil, cc.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
|
return &res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue