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: assets next and prev methods #1277

Merged
merged 1 commit into from
May 15, 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
59 changes: 59 additions & 0 deletions clients/horizonclient/asset_request_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package horizonclient

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -29,3 +30,61 @@ func TestAssetRequestBuildUrl(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "assets?asset_code=ABC&asset_issuer=GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU&order=desc", endpoint)
}

func ExampleClient_NextAssetsPage() {
client := DefaultPublicNetClient
// assets for asset issuer
assetRequest := AssetRequest{ForAssetIssuer: "GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU",
Limit: 20}
asset, err := client.Assets(assetRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(asset)

// all assets
assetRequest = AssetRequest{}
asset, err = client.Assets(assetRequest)
if err != nil {
fmt.Println(err)
return
}

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

func ExampleClient_PrevAssetsPage() {
client := DefaultPublicNetClient
// assets for asset issuer
assetRequest := AssetRequest{ForAssetIssuer: "GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU",
Limit: 20}
asset, err := client.Assets(assetRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(asset)

// all assets
assetRequest = AssetRequest{}
asset, err = client.Assets(assetRequest)
if err != nil {
fmt.Println(err)
return
}

// next page
prevPage, err := client.PrevAssetsPage(asset)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(prevPage)
}
12 changes: 12 additions & 0 deletions clients/horizonclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,5 +527,17 @@ func (c *Client) Version() string {
return version
}

// 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
func (c *Client) PrevAssetsPage(page hProtocol.AssetsPage) (assets hProtocol.AssetsPage, err error) {
err = c.sendRequestURL(page.Links.Prev.Href, "get", &assets)
return
}

// ensure that the horizon client implements ClientInterface
var _ ClientInterface = &Client{}
2 changes: 2 additions & 0 deletions clients/horizonclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ type ClientInterface interface {
StreamLedgers(ctx context.Context, request LedgerRequest, handler LedgerHandler) error
StreamOrderBooks(ctx context.Context, request OrderBookRequest, handler OrderBookHandler) error
Root() (hProtocol.Root, error)
NextAssetsPage(hProtocol.AssetsPage) (hProtocol.AssetsPage, error)
PrevAssetsPage(hProtocol.AssetsPage) (hProtocol.AssetsPage, error)
}

// DefaultTestNetClient is a default client to connect to test network.
Expand Down
12 changes: 12 additions & 0 deletions clients/horizonclient/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,17 @@ func (m *MockClient) Root() (hProtocol.Root, error) {
return a.Get(0).(hProtocol.Root), a.Error(1)
}

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

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

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