Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor HAL template generation in root endpoint #4145

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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