Coordinators methods for API

This commit is contained in:
ToniRamirezM
2020-10-21 12:24:58 +02:00
parent 8047582de9
commit f314498a26
10 changed files with 273 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ package api
import (
"database/sql"
"errors"
"net/http"
"github.com/gin-gonic/gin"
@@ -23,6 +24,11 @@ const (
maxUint32 = 4294967295
)
var (
// ErrNillBidderAddr is used when a nil bidderAddr is received in the getCoordinator method
ErrNillBidderAddr = errors.New("biderAddr can not be nil")
)
func postAccountCreationAuth(c *gin.Context) {
}
@@ -285,11 +291,49 @@ func getRecommendedFee(c *gin.Context) {
}
func getCoordinators(c *gin.Context) {
// Pagination
fromItem, order, limit, err := parsePagination(c)
if err != nil {
retBadReq(err, c)
return
}
// Fetch coordinators from historyDB
coordinators, pagination, err := h.GetCoordinators(fromItem, limit, order)
if err != nil {
retSQLErr(err, c)
return
}
// Build succesfull response
apiCoordinators := coordinatorsToAPI(coordinators)
c.JSON(http.StatusOK, &coordinatorsAPI{
Coordinators: apiCoordinators,
Pagination: pagination,
})
}
func getCoordinator(c *gin.Context) {
// Get bidderAddr
const name = "bidderAddr"
bidderAddr, err := parseEthAddr(c, name)
if err != nil {
retBadReq(err, c)
return
} else if bidderAddr == nil {
retBadReq(ErrNillBidderAddr, c)
return
}
coordinator, err := h.GetCoordinator(*bidderAddr)
if err != nil {
retSQLErr(err, c)
return
}
apiCoordinator := coordinatorsToAPI([]historydb.HistoryCoordinator{*coordinator})
c.JSON(http.StatusOK, apiCoordinator[0])
}
func retSQLErr(err error, c *gin.Context) {