[#259] netmap: Add NetworkInfo call to Client

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-17 20:39:29 +03:00 committed by Alex Vanin
parent 8cb1f960f4
commit 88d6857a3c

View file

@ -12,6 +12,8 @@ import (
// Client represents universal netmap transport client. // Client represents universal netmap transport client.
type Client struct { type Client struct {
cLocalNodeInfo *localNodeInfoClient cLocalNodeInfo *localNodeInfoClient
cNetworkInfo *networkInfoClient
} }
// Option represents Client option. // Option represents Client option.
@ -43,6 +45,14 @@ type localNodeInfoClient struct {
responseConverter func(interface{}) *LocalNodeInfoResponse responseConverter func(interface{}) *LocalNodeInfoResponse
} }
type networkInfoClient struct {
requestConverter func(*NetworkInfoRequest) interface{}
caller func(context.Context, interface{}) (interface{}, error)
responseConverter func(interface{}) *NetworkInfoResponse
}
// LocalNodeInfo sends LocalNodeInfoRequest over the network. // LocalNodeInfo sends LocalNodeInfoRequest over the network.
func (c *Client) LocalNodeInfo(ctx context.Context, req *LocalNodeInfoRequest) (*LocalNodeInfoResponse, error) { func (c *Client) LocalNodeInfo(ctx context.Context, req *LocalNodeInfoRequest) (*LocalNodeInfoResponse, error) {
resp, err := c.cLocalNodeInfo.caller(ctx, c.cLocalNodeInfo.requestConverter(req)) resp, err := c.cLocalNodeInfo.caller(ctx, c.cLocalNodeInfo.requestConverter(req))
@ -53,6 +63,16 @@ func (c *Client) LocalNodeInfo(ctx context.Context, req *LocalNodeInfoRequest) (
return c.cLocalNodeInfo.responseConverter(resp), nil return c.cLocalNodeInfo.responseConverter(resp), nil
} }
// NetworkInfo sends NetworkInfoRequest over the network.
func (c *Client) NetworkInfo(ctx context.Context, req *NetworkInfoRequest) (*NetworkInfoResponse, error) {
resp, err := c.cNetworkInfo.caller(ctx, c.cNetworkInfo.requestConverter(req))
if err != nil {
return nil, errors.Wrap(err, "could not send network info request")
}
return c.cNetworkInfo.responseConverter(resp), nil
}
func defaultCfg() *cfg { func defaultCfg() *cfg {
return &cfg{ return &cfg{
proto: client.ProtoGRPC, proto: client.ProtoGRPC,
@ -88,6 +108,17 @@ func NewClient(opts ...Option) (*Client, error) {
return LocalNodeInfoResponseFromGRPCMessage(resp.(*netmap.LocalNodeInfoResponse)) return LocalNodeInfoResponseFromGRPCMessage(resp.(*netmap.LocalNodeInfoResponse))
}, },
}, },
cNetworkInfo: &networkInfoClient{
requestConverter: func(req *NetworkInfoRequest) interface{} {
return NetworkInfoRequestToGRPCMessage(req)
},
caller: func(ctx context.Context, req interface{}) (interface{}, error) {
return c.NetworkInfo(ctx, req.(*netmap.NetworkInfoRequest))
},
responseConverter: func(resp interface{}) *NetworkInfoResponse {
return NetworkInfoResponseFromGRPCMessage(resp.(*netmap.NetworkInfoResponse))
},
},
}, nil }, nil
default: default:
err = client.ErrProtoUnsupported err = client.ErrProtoUnsupported