forked from TrueCloudLab/frostfs-node
[#11] accounting: Define service executor
Defines ServiceExecutor interface of the executor that handles service request bodies and returns service response bodies. Adds Service implementation that writes the result of ServiceExecutor to response body and attaches the internal ResponseMetaHeader. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
5cf622881f
commit
7cc1a2a85a
1 changed files with 42 additions and 0 deletions
42
pkg/services/accounting/executor.go
Normal file
42
pkg/services/accounting/executor.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package accounting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServiceExecutor interface {
|
||||||
|
Balance(context.Context, *accounting.BalanceRequestBody) (*accounting.BalanceResponseBody, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type executorSvc struct {
|
||||||
|
exec ServiceExecutor
|
||||||
|
|
||||||
|
metaHeader *session.ResponseMetaHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewExecutionService wraps ServiceExecutor and returns Accounting Service interface.
|
||||||
|
//
|
||||||
|
// Passed meta header is attached to all responses.
|
||||||
|
func NewExecutionService(exec ServiceExecutor, metaHdr *session.ResponseMetaHeader) accounting.Service {
|
||||||
|
return &executorSvc{
|
||||||
|
exec: exec,
|
||||||
|
metaHeader: metaHdr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *executorSvc) Balance(ctx context.Context, req *accounting.BalanceRequest) (*accounting.BalanceResponse, error) {
|
||||||
|
respBody, err := s.exec.Balance(ctx, req.GetBody())
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "could not execute Balance request")
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := new(accounting.BalanceResponse)
|
||||||
|
resp.SetBody(respBody)
|
||||||
|
resp.SetMetaHeader(s.metaHeader)
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
Loading…
Reference in a new issue