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 2 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
24 changes: 24 additions & 0 deletions clients/horizonclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,5 +539,29 @@ func (c *Client) PrevAssetsPage(page hProtocol.AssetsPage) (assets hProtocol.Ass
return
}

// NextLedgersPage returns the next page of ledgers
Copy link
Member

Choose a reason for hiding this comment

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

Add a period to the end of all the godoc strings here.

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{}
39 changes: 39 additions & 0 deletions clients/horizonclient/effect_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,45 @@ 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.


// next page
nextPage, err := client.NextEffectsPage(efp)
Copy link
Member

Choose a reason for hiding this comment

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

Might be cool to have the example use a loop, to show calling NextEffectsPage repeatedly. How do we know when we reach the end? Would be good to show that too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that's a good point. will need to confirm what the indicates the end of a collection of pages here.

if err != nil {
fmt.Println(err)
return
}
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)

// prev page
prevPage, err := client.PrevEffectsPage(efp)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(prevPage)
}
func ExampleClient_StreamEffects() {
client := DefaultTestNetClient
// all effects
Expand Down
41 changes: 40 additions & 1 deletion clients/horizonclient/ledger_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,46 @@ func TestLedgerRequestBuildUrl(t *testing.T) {
assert.Equal(t, "ledgers?cursor=now&order=desc", endpoint)
}

func ExampleClient_NextLedgersPage() {
client := DefaultPublicNetClient
// all ledgers
ledgerRequest := LedgerRequest{Limit: 20}
ledger, err := client.Ledgers(ledgerRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(ledger)

// next page
nextPage, err := client.NextLedgersPage(ledger)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(nextPage)
}

func ExampleClient_PrevLedgersPage() {
client := DefaultPublicNetClient
// all ledgers
ledgerRequest := LedgerRequest{Limit: 20}
ledger, err := client.Ledgers(ledgerRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(ledger)

// prev page
prevPage, err := client.PrevLedgersPage(ledger)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(prevPage)
}

func TestLedgerDetail(t *testing.T) {
hmock := httptest.NewClient()
client := &Client{
Expand Down Expand Up @@ -171,7 +211,6 @@ func TestLedgerRequestStreamLedgers(t *testing.T) {
assert.Contains(t, err.Error(), "got bad HTTP status code 500")

}

}

var ledgerStreamResponse = `data: {"_links":{"self":{"href":"https://horizon-testnet.stellar.org/ledgers/560339"},"transactions":{"href":"https://horizon-testnet.stellar.org/ledgers/560339/transactions{?cursor,limit,order}","templated":true},"operations":{"href":"https://horizon-testnet.stellar.org/ledgers/560339/operations{?cursor,limit,order}","templated":true},"payments":{"href":"https://horizon-testnet.stellar.org/ledgers/560339/payments{?cursor,limit,order}","templated":true},"effects":{"href":"https://horizon-testnet.stellar.org/ledgers/560339/effects{?cursor,limit,order}","templated":true}},"id":"66f4d95dab22dbc422585cc4b011716014e81df3599cee8db9c776cfc3a31e93","paging_token":"2406637679673344","hash":"66f4d95dab22dbc422585cc4b011716014e81df3599cee8db9c776cfc3a31e93","prev_hash":"6071f1e52a6bf37aba3f7437081577eafe69f78593c465fc5028c46a4746dda3","sequence":560339,"successful_transaction_count":5,"failed_transaction_count":1,"operation_count":44,"closed_at":"2019-04-01T16:47:05Z","total_coins":"100057227213.0436903","fee_pool":"57227816.6766542","base_fee_in_stroops":100,"base_reserve_in_stroops":5000000,"max_tx_set_size":100,"protocol_version":10,"header_xdr":"AAAACmBx8eUqa/N6uj90NwgVd+r+afeFk8Rl/FAoxGpHRt2jdIn+3X+/O3PFUUZ8Tgy4rfD1oNamR+9NMOCM2V6ndksAAAAAXKJAiQAAAAAAAAAAPyIIYU6Y37lve/MwZls1vmbgxgFdx93hdzOn6g8kHhQ1BS9aAKuXtApQoE3gKpjQ5ze0H9qUruyOUsbM776zXQAIjNMN4r8uJHCvJwACCHvk18POAAAAAwAAAAAAQZnVAAAAZABMS0AAAABkkiIcXkjaTtc9zTQBn0o72CUBe3u+2Mz7W6dgkvkYcJJle8JCNmXx5HcRlDSHJzzBShc8C3rQUIsIuJ93eoBMgHeYAzfholE8hjvrHrqoHq8jfPowxj1FGD6HaUPD1PHTcBXmf0U0cs2Ki0NBDDKNcwKC84nUPdumCkdAxSuEzn4AAAAA"}
Expand Down
4 changes: 4 additions & 0 deletions clients/horizonclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ type ClientInterface interface {
Root() (hProtocol.Root, error)
NextAssetsPage(hProtocol.AssetsPage) (hProtocol.AssetsPage, error)
PrevAssetsPage(hProtocol.AssetsPage) (hProtocol.AssetsPage, error)
NextLedgersPage(hProtocol.LedgersPage) (hProtocol.LedgersPage, error)
PrevLedgersPage(hProtocol.LedgersPage) (hProtocol.LedgersPage, error)
NextEffectsPage(effects.EffectsPage) (effects.EffectsPage, error)
PrevEffectsPage(effects.EffectsPage) (effects.EffectsPage, error)
}

// DefaultTestNetClient is a default client to connect to test network.
Expand Down
24 changes: 24 additions & 0 deletions clients/horizonclient/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,29 @@ func (m *MockClient) PrevAssetsPage(page hProtocol.AssetsPage) (hProtocol.Assets
return a.Get(0).(hProtocol.AssetsPage), a.Error(1)
}

// NextLedgersPage is a mocking method
Copy link
Member

Choose a reason for hiding this comment

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

What happens when there is no further Next/Prev to return? Can this be tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will look into this.

func (m *MockClient) NextLedgersPage(page hProtocol.LedgersPage) (hProtocol.LedgersPage, error) {
a := m.Called(page)
return a.Get(0).(hProtocol.LedgersPage), a.Error(1)
}

// PrevLedgersPage is a mocking method
func (m *MockClient) PrevLedgersPage(page hProtocol.LedgersPage) (hProtocol.LedgersPage, error) {
a := m.Called(page)
return a.Get(0).(hProtocol.LedgersPage), a.Error(1)
}

// NextEffectsPage is a mocking method
func (m *MockClient) NextEffectsPage(page effects.EffectsPage) (effects.EffectsPage, error) {
a := m.Called(page)
return a.Get(0).(effects.EffectsPage), a.Error(1)
}

// PrevEffectsPage is a mocking method
func (m *MockClient) PrevEffectsPage(page effects.EffectsPage) (effects.EffectsPage, error) {
a := m.Called(page)
return a.Get(0).(effects.EffectsPage), a.Error(1)
}

// ensure that the MockClient implements ClientInterface
var _ ClientInterface = &MockClient{}