[#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 (
|
if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
|
||||||
cc contextCall
|
return nil, fmt.Errorf("sign request: %w", err)
|
||||||
res ResNetworkInfo
|
}
|
||||||
)
|
|
||||||
|
resp, err := rpcapi.NetworkInfo(&c.c, req, client.WithContext(ctx))
|
||||||
c.initCallContext(&cc)
|
if err != nil {
|
||||||
cc.meta = prm.prmCommonMeta
|
return nil, err
|
||||||
cc.req = &req
|
}
|
||||||
cc.statusRes = &res
|
|
||||||
cc.call = func() (responseV2, error) {
|
var res ResNetworkInfo
|
||||||
return rpcapi.NetworkInfo(&c.c, &req, client.WithContext(ctx))
|
res.st, err = c.processResponse(resp)
|
||||||
|
if err != nil || !apistatus.IsSuccessful(res.st) {
|
||||||
|
return &res, err
|
||||||
}
|
}
|
||||||
cc.result = func(r responseV2) {
|
|
||||||
resp := r.(*v2netmap.NetworkInfoResponse)
|
|
||||||
|
|
||||||
const fieldNetInfo = "network info"
|
const fieldNetInfo = "network info"
|
||||||
|
|
||||||
netInfoV2 := resp.GetBody().GetNetworkInfo()
|
netInfoV2 := resp.GetBody().GetNetworkInfo()
|
||||||
if netInfoV2 == nil {
|
if netInfoV2 == nil {
|
||||||
cc.err = newErrMissingResponseField(fieldNetInfo)
|
return nil, newErrMissingResponseField(fieldNetInfo)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
if err := res.info.ReadFromV2(*netInfoV2); err != nil {
|
||||||
cc.err = res.info.ReadFromV2(*netInfoV2)
|
return nil, newErrInvalidResponseField(fieldNetInfo, err)
|
||||||
if cc.err != nil {
|
|
||||||
cc.err = newErrInvalidResponseField(fieldNetInfo, cc.err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// process call
|
|
||||||
if !cc.processCall() {
|
|
||||||
return nil, cc.err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &res, nil
|
return &res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue