[#263] pkg/client: Refactor the client to use raw protobuf client

Make `Client` to be the wrapper over raw protobuf client. Provide public
method to get the underlying raw client. Change implementations of all
methods with the new approach of the RPC execution.

Additional changes:
 * key replaced from `New` argument to `WithDefaultPrivateKey` option;
 * `GetSelfBalance` is removed as non-viable;
 * `GetEACLWithSignature` is removed, `GetEACL` returns `EACLWithSignature`;
 * `AttachSessionToken` / `AttachBearerToken` are removed as non-viable;
 * redundant options are removed.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-03-12 16:07:52 +03:00 committed by Alex Vanin
parent 5a9dd7ab3f
commit c819909906
9 changed files with 380 additions and 1068 deletions

View file

@ -5,54 +5,29 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/accounting"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/rpc/client"
v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-api-go/v2/client"
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
"github.com/pkg/errors"
)
// Accounting contains methods related to balance querying.
type Accounting interface {
// GetSelfBalance returns balance of the account deduced from client's key.
GetSelfBalance(context.Context, ...CallOption) (*accounting.Decimal, error)
// GetBalance returns balance of provided account.
GetBalance(context.Context, *owner.ID, ...CallOption) (*accounting.Decimal, error)
}
func (c clientImpl) GetSelfBalance(ctx context.Context, opts ...CallOption) (*accounting.Decimal, error) {
return c.GetBalance(ctx, nil, opts...)
}
func (c clientImpl) GetBalance(ctx context.Context, owner *owner.ID, opts ...CallOption) (*accounting.Decimal, error) {
// check remote node version
switch c.remoteNode.Version.Major() {
case 2:
return c.getBalanceV2(ctx, owner, opts...)
default:
return nil, errUnsupportedProtocol
}
}
func (c clientImpl) getBalanceV2(ctx context.Context, ownerID *owner.ID, opts ...CallOption) (*accounting.Decimal, error) {
func (c *clientImpl) GetBalance(ctx context.Context, owner *owner.ID, opts ...CallOption) (*accounting.Decimal, error) {
// apply all available options
callOptions := c.defaultCallOptions()
for i := range opts {
opts[i].apply(&callOptions)
}
if ownerID == nil {
w, err := owner.NEO3WalletFromPublicKey(&callOptions.key.PublicKey)
if err != nil {
return nil, err
}
ownerID = new(owner.ID)
ownerID.SetNeo3Wallet(w)
opts[i](callOptions)
}
reqBody := new(v2accounting.BalanceRequestBody)
reqBody.SetOwnerID(ownerID.ToV2())
reqBody.SetOwnerID(owner.ToV2())
req := new(v2accounting.BalanceRequest)
req.SetBody(reqBody)
@ -63,56 +38,15 @@ func (c clientImpl) getBalanceV2(ctx context.Context, ownerID *owner.ID, opts ..
return nil, err
}
switch c.remoteNode.Protocol {
case GRPC:
cli, err := v2AccountingClientFromOptions(c.opts)
if err != nil {
return nil, errors.Wrap(err, "can't create grpc client")
}
resp, err := cli.Balance(ctx, req)
if err != nil {
return nil, errors.Wrap(err, "transport error")
}
err = v2signature.VerifyServiceMessage(resp)
if err != nil {
return nil, errors.Wrap(err, "can't verify response message")
}
return accounting.NewDecimalFromV2(resp.GetBody().GetBalance()), nil
default:
return nil, errUnsupportedProtocol
}
}
func v2AccountingClientFromOptions(opts *clientOptions) (cli *v2accounting.Client, err error) {
switch {
case opts.grpcOpts.v2AccountingClient != nil:
// return value from client cache
return opts.grpcOpts.v2AccountingClient, nil
case opts.grpcOpts.conn != nil:
cli, err = v2accounting.NewClient(v2accounting.WithGlobalOpts(
client.WithGRPCConn(opts.grpcOpts.conn)),
)
case opts.addr != "":
cli, err = v2accounting.NewClient(v2accounting.WithGlobalOpts(
client.WithNetworkAddress(opts.addr),
client.WithDialTimeout(opts.dialTimeout),
))
default:
return nil, errOptionsLack("Accounting")
}
// check if client correct and save in cache
resp, err := rpcapi.Balance(c.Raw(), req, client.WithContext(ctx))
if err != nil {
return nil, err
return nil, errors.Wrap(err, "transport error")
}
opts.grpcOpts.v2AccountingClient = cli
err = v2signature.VerifyServiceMessage(resp)
if err != nil {
return nil, errors.Wrap(err, "can't verify response message")
}
return cli, nil
return accounting.NewDecimalFromV2(resp.GetBody().GetBalance()), nil
}