Skip to content

Commit

Permalink
client/horizonclient: Extend offers to support new filters. (#2230)
Browse files Browse the repository at this point in the history
* Extends OffersRequest to support seller, buying and selling filters.

This will allow users to consume data from the new endpoint `/offers`.

* Add sample.
  • Loading branch information
abuiles committed Feb 25, 2020
1 parent 559bc53 commit f1d4c47
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 7 deletions.
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",
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 @@ -477,6 +477,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

0 comments on commit f1d4c47

Please sign in to comment.