Skip to content

Commit

Permalink
Refactor HAL template generation in root endpoint (#4145)
Browse files Browse the repository at this point in the history
and fix the liquidity pools and claimable balances templates as a result.
  • Loading branch information
2opremio authored Dec 17, 2021
1 parent e946310 commit 3795a6a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type AccountsQuery struct {

// URITemplate returns a rfc6570 URI template the query struct
func (q AccountsQuery) URITemplate() string {
return "/accounts{?" + strings.Join(getURIParams(&q, true), ",") + "}"
return getURITemplate(&q, "accounts", true)
}

var invalidAccountsParams = problem.P{
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/claimable_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (q ClaimableBalancesQuery) claimant() *xdr.AccountId {

// URITemplate returns a rfc6570 URI template the query struct
func (q ClaimableBalancesQuery) URITemplate() string {
return "/claimable_balances?{asset,claimant,sponsor}"
return getURITemplate(&q, "claimable_balances", true)
}

type GetClaimableBalancesHandler struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func TestCursorAndOrderValidation(t *testing.T) {

func TestClaimableBalancesQueryURLTemplate(t *testing.T) {
tt := assert.New(t)
expected := "/claimable_balances?{asset,claimant,sponsor}"
expected := "/claimable_balances{?asset,sponsor,claimant,cursor,limit,order}"
q := ClaimableBalancesQuery{}
tt.Equal(expected, q.URITemplate())
}
10 changes: 7 additions & 3 deletions services/horizon/internal/actions/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,19 @@ func getURIParams(query interface{}, paginated bool) []string {
params := getSchemaTags(reflect.ValueOf(query).Elem())
if paginated {
pagingParams := []string{
"cursor",
"limit",
"order",
ParamCursor,
ParamLimit,
ParamOrder,
}
params = append(params, pagingParams...)
}
return params
}

func getURITemplate(query interface{}, basePath string, paginated bool) string {
return "/" + basePath + "{?" + strings.Join(getURIParams(query, paginated), ",") + "}"
}

func getSchemaTags(v reflect.Value) []string {
qt := v.Type()
fields := make([]string, 0, v.NumField())
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/liquidity_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type LiquidityPoolsQuery struct {

// URITemplate returns a rfc6570 URI template the query struct
func (q LiquidityPoolsQuery) URITemplate() string {
return "/liquidity_pools?{?reserves,account}"
return getURITemplate(&q, "liquidity_pools", true)
}

// Validate validates and parses the query
Expand Down
5 changes: 2 additions & 3 deletions services/horizon/internal/actions/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"fmt"
"net/http"
"strings"

"github.com/stellar/go/amount"
"github.com/stellar/go/protocols/horizon"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (q StrictReceivePathsQuery) DestinationAsset() xdr.Asset {

// URITemplate returns a rfc6570 URI template for the query struct
func (q StrictReceivePathsQuery) URITemplate() string {
return "/paths/strict-receive{?" + strings.Join(getURIParams(&q, false), ",") + "}"
return getURITemplate(&q, "paths/strict-receive", false)
}

// Validate runs custom validations.
Expand Down Expand Up @@ -214,7 +213,7 @@ type FindFixedPathsQuery struct {

// URITemplate returns a rfc6570 URI template for the query struct
func (q FindFixedPathsQuery) URITemplate() string {
return "/paths/strict-send{?" + strings.Join(getURIParams(&q, false), ",") + "}"
return getURITemplate(&q, "paths/strict-send", false)
}

// Validate runs custom validations.
Expand Down

0 comments on commit 3795a6a

Please sign in to comment.