This repository has been archived on 2024-09-11. You can view files and clone it, but cannot push or open issues or pull requests.
frostfs-rest-gw/handlers/balance.go
Alex Vanin d2d88ba21b Rename package name
Due to source code relocation from GitHub.

Signed-off-by: Alex Vanin <a.vanin@yadro.com>
2023-03-13 09:47:55 +03:00

39 lines
1.3 KiB
Go

package handlers
import (
"strconv"
"git.frostfs.info/TrueCloudLab/frostfs-rest-gw/gen/models"
"git.frostfs.info/TrueCloudLab/frostfs-rest-gw/gen/restapi/operations"
"git.frostfs.info/TrueCloudLab/frostfs-rest-gw/internal/util"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
"github.com/go-openapi/runtime/middleware"
)
// Balance handler that get balance from FrostFS.
func (a *API) Balance(params operations.GetBalanceParams) middleware.Responder {
var ownerID user.ID
if err := ownerID.DecodeString(params.Address); err != nil {
resp := a.logAndGetErrorResponse("parse address", err)
return operations.NewGetBalanceBadRequest().WithPayload(resp)
}
var prm pool.PrmBalanceGet
prm.SetAccount(ownerID)
frostfsBalance, err := a.pool.Balance(params.HTTPRequest.Context(), prm)
if err != nil {
resp := a.logAndGetErrorResponse("get balance", err)
return operations.NewGetBalanceBadRequest().WithPayload(resp)
}
var resp models.Balance
resp.Address = util.NewString(params.Address)
resp.Value = util.NewString(strconv.FormatInt(frostfsBalance.Value(), 10))
resp.Precision = util.NewInteger(int64(frostfsBalance.Precision()))
return operations.NewGetBalanceOK().
WithPayload(&resp).
WithAccessControlAllowOrigin("*")
}