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

horizonclient: next and prev methods #1278

Merged
merged 3 commits into from
May 16, 2019
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
28 changes: 26 additions & 2 deletions clients/horizonclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,17 +527,41 @@ func (c *Client) Version() string {
return version
}

// NextAssetsPage returns the next page of assets
// NextAssetsPage returns the next page of assets.
func (c *Client) NextAssetsPage(page hProtocol.AssetsPage) (assets hProtocol.AssetsPage, err error) {
err = c.sendRequestURL(page.Links.Next.Href, "get", &assets)
return
}

// PrevAssetsPage returns the previous page of assets
// PrevAssetsPage returns the previous page of assets.
func (c *Client) PrevAssetsPage(page hProtocol.AssetsPage) (assets hProtocol.AssetsPage, err error) {
err = c.sendRequestURL(page.Links.Prev.Href, "get", &assets)
return
}

// NextLedgersPage returns the next page of ledgers.
func (c *Client) NextLedgersPage(page hProtocol.LedgersPage) (ledgers hProtocol.LedgersPage, err error) {
err = c.sendRequestURL(page.Links.Next.Href, "get", &ledgers)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are simple and repetitive I think you could just inline all these next/prev returns, and lose the err variable.

return
}

// PrevLedgersPage returns the previous page of ledgers.
func (c *Client) PrevLedgersPage(page hProtocol.LedgersPage) (ledgers hProtocol.LedgersPage, err error) {
err = c.sendRequestURL(page.Links.Prev.Href, "get", &ledgers)
return
}

// NextEffectsPage returns the next page of effects.
func (c *Client) NextEffectsPage(page effects.EffectsPage) (efp effects.EffectsPage, err error) {
err = c.sendRequestURL(page.Links.Next.Href, "get", &efp)
return
}

// PrevEffectsPage returns the previous page of effects.
func (c *Client) PrevEffectsPage(page effects.EffectsPage) (efp effects.EffectsPage, err error) {
err = c.sendRequestURL(page.Links.Prev.Href, "get", &efp)
return
}

// ensure that the horizon client implements ClientInterface
var _ ClientInterface = &Client{}
176 changes: 176 additions & 0 deletions clients/horizonclient/effect_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,73 @@ func TestEffectRequestBuildUrl(t *testing.T) {

}

func ExampleClient_NextEffectsPage() {
client := DefaultPublicNetClient
// all effects
effectRequest := EffectRequest{Limit: 20}
efp, err := client.Effects(effectRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(efp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be ftm.Println?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any will do actually. Oped for fmt.Print here because the response will not be a simple string.


// get next pages.
recordsFound := false
if len(efp.Embedded.Records) > 0 {
recordsFound = true
}
page := efp
// get the next page of records if recordsFound is true
for recordsFound {
// next page
nextPage, err := client.NextEffectsPage(page)
if err != nil {
fmt.Println(err)
return
}

page = nextPage
if len(nextPage.Embedded.Records) == 0 {
recordsFound = false
}
fmt.Println(nextPage)
}
}

func ExampleClient_PrevEffectsPage() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as for previous example

client := DefaultPublicNetClient
// all effects
effectRequest := EffectRequest{Limit: 20}
efp, err := client.Effects(effectRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(efp)

// get prev pages.
recordsFound := false
if len(efp.Embedded.Records) > 0 {
recordsFound = true
}
page := efp
// get the prev page of records if recordsFound is true
for recordsFound {
// prev page
prevPage, err := client.PrevEffectsPage(page)
if err != nil {
fmt.Println(err)
return
}

page = prevPage
if len(prevPage.Embedded.Records) == 0 {
recordsFound = false
}
fmt.Println(prevPage)
}
}
func ExampleClient_StreamEffects() {
client := DefaultTestNetClient
// all effects
Expand Down Expand Up @@ -147,5 +214,114 @@ func TestEffectRequestStreamEffects(t *testing.T) {
}
}

func TestNextEffectsPage(t *testing.T) {
hmock := httptest.NewClient()
client := &Client{
HorizonURL: "https://localhost/",
HTTP: hmock,
}

// Account effects
effectRequest := EffectRequest{ForAccount: "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD"}

hmock.On(
"GET",
"https://localhost/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects",
).ReturnString(200, firstEffectsPage)

efp, err := client.Effects(effectRequest)

if assert.NoError(t, err) {
assert.Equal(t, len(efp.Embedded.Records), 2)
}

hmock.On(
"GET",
"https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-3&limit=10&order=asc",
).ReturnString(200, emptyEffectsPage)

nextPage, err := client.NextEffectsPage(efp)
if assert.NoError(t, err) {
assert.Equal(t, len(nextPage.Embedded.Records), 0)
}
}

var effectStreamResponse = `data: {"_links":{"operation":{"href":"https://horizon-testnet.stellar.org/operations/2531135896703017"},"succeeds":{"href":"https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=2531135896703017-1"},"precedes":{"href":"https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=2531135896703017-1"}},"id":"0002531135896703017-0000000001","paging_token":"2531135896703017-1","account":"GBNZN27NAOHRJRCMHQF2ZN2F6TAPVEWKJIGZIRNKIADWIS2HDENIS6CI","type":"account_credited","type_i":2,"created_at":"2019-04-03T10:14:17Z","asset_type":"credit_alphanum4","asset_code":"qwop","asset_issuer":"GBM4HXXNDBWWQBXOL4QCTZIUQAP6XFUI3FPINUGUPBMULMTEHJPIKX6T","amount":"0.0460000"}
`

var firstEffectsPage = `{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=&limit=10&order=asc"
},
"next": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-3&limit=10&order=asc"
},
"prev": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-1&limit=10&order=desc"
}
},
"_embedded": {
"records": [
{
"_links": {
"operation": {
"href": "https://horizon-testnet.stellar.org/operations/1557363731492865"
},
"succeeds": {
"href": "https://horizon-testnet.stellar.org/effects?order=desc&cursor=1557363731492865-1"
},
"precedes": {
"href": "https://horizon-testnet.stellar.org/effects?order=asc&cursor=1557363731492865-1"
}
},
"id": "0001557363731492865-0000000001",
"paging_token": "1557363731492865-1",
"account": "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD",
"type": "account_created",
"type_i": 0,
"created_at": "2019-05-16T07:13:25Z",
"starting_balance": "10000.0000000"
},
{
"_links": {
"operation": {
"href": "https://horizon-testnet.stellar.org/operations/1557363731492865"
},
"succeeds": {
"href": "https://horizon-testnet.stellar.org/effects?order=desc&cursor=1557363731492865-3"
},
"precedes": {
"href": "https://horizon-testnet.stellar.org/effects?order=asc&cursor=1557363731492865-3"
}
},
"id": "0001557363731492865-0000000003",
"paging_token": "1557363731492865-3",
"account": "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD",
"type": "signer_created",
"type_i": 10,
"created_at": "2019-05-16T07:13:25Z",
"weight": 1,
"public_key": "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD",
"key": ""
}
]
}
}`

var emptyEffectsPage = `{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-3&limit=10&order=asc"
},
"next": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-3&limit=10&order=asc"
},
"prev": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-3&limit=10&order=desc"
}
},
"_embedded": {
"records": []
}
}`
Loading