-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clients/horizonclient: Add support for Accounts endpoint. (#2229)
* clients/horizonclient: Add support for Accounts endpoint. Extend the horizonclient to support accounts filter. It allows you to retrive all the accounts with a given signer or with a trustline to an asset. ```go client := horizonclient.DefaultPublicNetClient accountsRequest := horizonclient.AccountsRequest{Signer: "GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU"} account, err := client.Accounts(accountsRequest) if err != nil { fmt.Println(err) return } fmt.Print(account) ``` * Apply suggestions from code review Co-Authored-By: Eric Saunders <ire-and-curses@users.noreply.github.com> * Remove if. * Typos. Co-authored-by: Eric Saunders <ire-and-curses@users.noreply.github.com>
- Loading branch information
1 parent
9c56e99
commit f7d9abc
Showing
7 changed files
with
270 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package horizonclient | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/stellar/go/support/errors" | ||
) | ||
|
||
// BuildURL creates the endpoint to be queried based on the data in the AccountsRequest struct. | ||
// Either "Signer" or "Asset" fields should be set when retrieving Accounts. | ||
// At the moment, you can't use both filters at the same time. | ||
func (r AccountsRequest) BuildURL() (endpoint string, err error) { | ||
|
||
nParams := countParams(r.Signer, r.Asset) | ||
|
||
if nParams <= 0 { | ||
err = errors.New("invalid request: no parameters - Signer or Asset must be provided") | ||
} | ||
|
||
if nParams >= 2 { | ||
err = errors.New("invalid request: too many parameters - Signer and Asset provided, provide a single filter") | ||
} | ||
|
||
if err != nil { | ||
return endpoint, err | ||
} | ||
query := url.Values{} | ||
switch { | ||
case len(r.Signer) > 0: | ||
query.Add("signer", r.Signer) | ||
|
||
case len(r.Asset) > 0: | ||
query.Add("asset", r.Asset) | ||
} | ||
|
||
endpoint = fmt.Sprintf( | ||
"accounts?%s", | ||
query.Encode(), | ||
) | ||
|
||
if pageParams := addQueryParams(cursor(r.Cursor), limit(r.Limit), r.Order); len(pageParams) > 0 { | ||
endpoint = fmt.Sprintf( | ||
"%s&%s", | ||
endpoint, | ||
pageParams, | ||
) | ||
} | ||
|
||
_, err = url.Parse(endpoint) | ||
if err != nil { | ||
err = errors.Wrap(err, "failed to parse endpoint") | ||
} | ||
|
||
return endpoint, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters