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

client/horizonclient: Extend offers to support new filters. #2230

Merged
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
14 changes: 14 additions & 0 deletions clients/horizonclient/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,20 @@ func ExampleClient_Offers() {
return
}
fmt.Print(offers)

offerRequest = horizonclient.OfferRequest{
Seller: "GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU",
abuiles marked this conversation as resolved.
Show resolved Hide resolved
Selling: "COP:GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU",
Buying: "EUR:GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU",
Order: horizonclient.OrderDesc,
}

offers, err = client.Offers(offerRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(offers)
}

func ExampleClient_OperationDetail() {
Expand Down
3 changes: 3 additions & 0 deletions clients/horizonclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ type feeStatsRequest struct {
// The query parameters (Order, Cursor and Limit) are optional. All or none can be set.
type OfferRequest struct {
ForAccount string
Selling string
Seller string
Buying string
Order Order
Cursor string
Limit uint
Expand Down
75 changes: 75 additions & 0 deletions clients/horizonclient/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,81 @@ func TestOfferRequest(t *testing.T) {
assert.Equal(t, record.Amount, "1999979.8700000")
assert.Equal(t, record.LastModifiedLedger, int32(103307))
}

hmock.On(
"GET",
"https://localhost/offers?seller=GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
).ReturnString(200, offersResponse)

offersRequest = OfferRequest{
Seller: "GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
}

offers, err = client.Offers(offersRequest)
if assert.NoError(t, err) {
assert.Len(t, offers.Embedded.Records, 1)
}

hmock.On(
"GET",
"https://localhost/offers?buying=COP%3AGDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
).ReturnString(200, offersResponse)

offersRequest = OfferRequest{
Buying: "COP:GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
}

offers, err = client.Offers(offersRequest)
if assert.NoError(t, err) {
assert.Len(t, offers.Embedded.Records, 1)
}

hmock.On(
"GET",
"https://localhost/offers?selling=EUR%3AGDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
).ReturnString(200, offersResponse)

offersRequest = OfferRequest{
Selling: "EUR:GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
}

offers, err = client.Offers(offersRequest)
if assert.NoError(t, err) {
assert.Len(t, offers.Embedded.Records, 1)
}

hmock.On(
"GET",
"https://localhost/offers?selling=EUR%3AGDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
).ReturnString(200, offersResponse)

offersRequest = OfferRequest{
Selling: "EUR:GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
}

offers, err = client.Offers(offersRequest)
if assert.NoError(t, err) {
assert.Len(t, offers.Embedded.Records, 1)
}

hmock.On(
"GET",
"https://localhost/offers?buying=EUR%3AGDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F&seller=GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F&selling=EUR%3AGDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F&cursor=30&limit=20&order=desc",
).ReturnString(200, offersResponse)

offersRequest = OfferRequest{
Seller: "GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
Buying: "EUR:GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
Selling: "EUR:GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
Order: "desc",
Limit: 20,
Cursor: "30",
}

offers, err = client.Offers(offersRequest)
if assert.NoError(t, err) {
assert.Len(t, offers.Embedded.Records, 1)
}
}

func TestOperationsRequest(t *testing.T) {
Expand Down
30 changes: 23 additions & 7 deletions clients/horizonclient/offer_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@ import (

// BuildURL creates the endpoint to be queried based on the data in the OfferRequest struct.
func (or OfferRequest) BuildURL() (endpoint string, err error) {
if or.ForAccount == "" {
return endpoint, errors.New(`parameter "ForAccount" required`)
}
endpoint = fmt.Sprintf("accounts/%s/offers", or.ForAccount)
// backwards compatibility support
if len(or.ForAccount) > 0 {
endpoint = fmt.Sprintf("accounts/%s/offers", or.ForAccount)
queryParams := addQueryParams(cursor(or.Cursor), limit(or.Limit), or.Order)
if queryParams != "" {
endpoint = fmt.Sprintf("%s?%s", endpoint, queryParams)
}
} else {
query := url.Values{}
if len(or.Seller) > 0 {
query.Add("seller", or.Seller)
}
if len(or.Selling) > 0 {
query.Add("selling", or.Selling)
}
if len(or.Buying) > 0 {
query.Add("buying", or.Buying)
}

queryParams := addQueryParams(cursor(or.Cursor), limit(or.Limit), or.Order)
if queryParams != "" {
endpoint = fmt.Sprintf("%s?%s", endpoint, queryParams)
endpoint = fmt.Sprintf("offers?%s", query.Encode())
pageParams := addQueryParams(cursor(or.Cursor), limit(or.Limit), or.Order)
if pageParams != "" {
endpoint = fmt.Sprintf("%s&%s", endpoint, pageParams)
}
}

_, err = url.Parse(endpoint)
Expand Down