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 fix merge issues #1241

Merged
merged 15 commits into from
May 3, 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
24 changes: 15 additions & 9 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
If you're making a doc PR or something tiny where the below is irrelevant, just delete this template and use a short description.
If you're making a doc PR or something tiny where the below is irrelevant, just delete this
template and use a short description.

### PR Structure

* [ ] This PR has reasonably narrow scope (if not, break it down into smaller PRs)
* [ ] This PR avoids mixing refactoring changes with feature changes (split into two PRs otherwise)
* [ ] This PR's title starts with name of package that is most changed in the PR, ex. `services/friendbot`

* [ ] This PR has reasonably narrow scope (if not, break it down into smaller PRs).
* [ ] This PR avoids mixing refactoring changes with feature changes (split into two PRs
otherwise).
* [ ] This PR's title starts with name of package that is most changed in the PR, ex.
`services/friendbot`

### Thoroughness

* [ ] This PR adds tests for the most critical parts of the new functionality or fixes.
* [ ] I've updated any docs ([developer docs](https://www.stellar.org/developers/reference/), `.md` files, etc...) affected by this change
* [ ] I've updated any docs ([developer docs](https://www.stellar.org/developers/reference/), `.md`
files, etc... affected by this change). Take a look in the `docs` folder for a given service,
like [this one](https://github.com/stellar/go/tree/master/services/horizon/internal/docs).

### Release planning

* [ ] I've updated the relevant CHANGELOG ([here](services/horizon/CHANGELOG.md) for Horizon) if needed with deprecations, added features, breaking changes, and DB schema changes
* [ ] I've decided if this PR requires a new major/minor version according to [semver](https://semver.org/), or if it's monly a patch change. The PR is targeted at the next release branch if it's not a patch change.

* [ ] I've updated the relevant CHANGELOG ([here](services/horizon/CHANGELOG.md) for Horizon) if
needed with deprecations, added features, breaking changes, and DB schema changes.
* [ ] I've decided if this PR requires a new major/minor version according to
[semver](https://semver.org/), or if it's mainly a patch change. The PR is targeted at the next
release branch if it's not a patch change.

## Summary

Expand Down
49 changes: 21 additions & 28 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
# Changelog

All notable changes to this project will be documented in this
file. This project adheres to [Semantic Versioning](http://semver.org/).

As this project is pre 1.0, breaking changes may happen for minor version
bumps. A breaking change will get clearly notified in this log.

NOTE: this changelog represents the changes that are associated with the library code in this repo (rather than the tools or services in this repo).

## [Unreleased]

### Added

- xdr: added support for new signer types
- build: `Signer` learned support for new signer types
- strkey: added support for new signer types
- network: Added the `HashTransaction` helper func to get the hash of a transaction targeted to a specific stellar network.
- trades: Added Server-Sent Events endpoint to support streaming of trades
- trades: add `base_offer_id` and `counter_offer_id` to trade resources.
- trade aggregation: Added an optional `offset` parameter that lets you offset the bucket timestamps in hour-long increments. Can only be used if the `resolution` parameter is greater than 1 hour. `offset` must also be in whole-hours and less than 24 hours.


### Changed:

- build: _BREAKING CHANGE_: A transaction built and signed using the `build` package no longer default to the test network.
- trades for offer endpoint will query for trades that match the given offer on either side of trades, rather than just the "sell" offer.

[Unreleased]: https://github.com/stellar/go/commits/master
This repository adheres to [Semantic Versioning](http://semver.org/).

This monorepo contains a number of projects, individually versioned and released. Please consult the relevant changelog:

* `horizon server` ([changelog](./services/horizon/CHANGELOG.md))
* `horizonclient` ([changelog](./clients/horizonclient/CHANGELOG.md))
* `txnbuild` ([changelog](./txnbuild/CHANGELOG.md))
* `bridge` ([changelog](./services/bridge/CHANGELOG.md))
* `compliance` ([changelog](./services/compliance/CHANGELOG.md))
* `federation` ([changelog](./services/federation/CHANGELOG.md))
* `bifrost` ([changelog](./services/bifrost/CHANGELOG.md))
* `ticker` (experimental) ([changelog](./exp/ticker/CHANGELOG.md))
* `stellar-vanity-gen` ([changelog](./tools/stellar-vanity-gen/CHANGELOG.md))
* `stellar-sign` ([changelog](./tools/stellar-sign/CHANGELOG.md))
* `stellar-archivist` ([changelog](./tools/stellar-archivist/CHANGELOG.md))
* `stellar-hd-wallet` ([changelog](./tools/stellar-hd-wallet/CHANGELOG.md))

If a project is pre-v1.0, breaking changes may happen for minor version
bumps. A breaking change will be clearly notified in the corresponding changelog.

Official project releases may be found here: https://github.com/stellar/go/releases
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ This repo is the home for all of the public go code produced by SDF. In additio
## Package Index

* [Horizon Server](services/horizon): Full-featured API server for Stellar network
* [Go Clients (Horizon SDK)](clients): Go SDK for making requests to Horizon Server
* [Go Horizon SDK - horizonclient](clients/horizonclient): Client for Horizon server (queries and transaction submission)
* [Go Horizon SDK - txnbuild](txnbuild): Construct Stellar transactions and operations
* [Bifrost](services/bifrost): Bitcoin/Ethereum -> Stellar bridge
* Servers for Anchors & Financial Institutions
* [Bridge Server](services/bridge): send payments and take action when payments are received
Expand Down
13 changes: 9 additions & 4 deletions clients/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Clients package

Packages contained by this package provide client libraries for accessing the ecosystem of stellar services. At present, it only contains a simple horizon client library, but in the future it will contain clients to interact with stellar-core, federation, the bridge server and more.
Packages here provide client libraries for accessing the ecosystem of Stellar services.

See [godoc](https://godoc.org/github.com/stellar/go/clients) for details about each package.
* `horizonclient` - programmatic client access to Horizon (use in conjunction with [txnbuild](../txnbuild))
* `stellartoml` - parse Stellar.toml files from the internet
* `federation` - resolve federation addresses into stellar account IDs, suitable for use within a transaction
* `horizon` (DEPRECATED) - the original Horizon client, now superceded by `horizonclient`

## Adding new client packages
See [GoDoc](https://godoc.org/github.com/stellar/go/clients) for more details.

## For developers: Adding new client packages

Ideally, each one of our client packages will have commonalities in their API to ease the cost of learning each. It's recommended that we follow a pattern similar to the `net/http` package's client shape:

A type, `Client`, is the central type of any client package, and its methods should provide the bulk of the functionality for the package. A `DefaultClient` var is provided for consumers that don't need client-level customization of behavior. Each method on the `Client` type should have a corresponding func at the package level that proxies a call through to the default client. For example, `http.Get()` is the equivalent of `http.DefaultClient.Get()`.
A type, `Client`, is the central type of any client package, and its methods should provide the bulk of the functionality for the package. A `DefaultClient` var is provided for consumers that don't need client-level customization of behavior. Each method on the `Client` type should have a corresponding func at the package level that proxies a call through to the default client. For example, `http.Get()` is the equivalent of `http.DefaultClient.Get()`.
7 changes: 2 additions & 5 deletions clients/horizon/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Package horizon provides client access to a horizon server, allowing an
// application to post transactions and lookup ledger information.
// Package horizon is DEPRECATED in favour of clients/horizonclient! It used to provide client access to a horizon
// server, allowing an application to post transactions and lookup ledger information.
//
// Create an instance of `Client` to customize the server used, or alternatively
// use `DefaultTestNetClient` or `DefaultPublicNetClient` to access the SDF run
// horizon servers.
// Deprecated: clients/horizon package with all its exported methods and variables will no longer be
// maintained. It will be removed in future versions of the SDK.
// Use clients/horizonclient (https://godoc.org/github.com/stellar/go/clients/horizonclient) instead.
Expand Down
17 changes: 15 additions & 2 deletions clients/horizonclient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ All notable changes to this project will be documented in this
file. This project adheres to [Semantic Versioning](http://semver.org/).


## [v1.0.1] - (draft release)
## [v1.1.0](https://github.com/stellar/go/releases/tag/horizonclient-v1.1.0) - 2019-02-02

### Added
- `Client.Root()` method for querying the root endpoint of a horizon server.

- `Client.Root()` method for querying the root endpoint of a horizon server.
- Support for returning concrete effect types[#1217](https://github.com/stellar/go/pull/1217)
- Fix when no HTTP client is provided

### Changes

- `Client.Fund()` now returns `TransactionSuccess` instead of a http response pointer.

- Querying the effects endpoint now supports returning the concrete effect type for each effect. This is also supported in streaming mode. See the [docs](https://godoc.org/github.com/stellar/go/clients/horizonclient#Client.Effects) for examples.

## [v1.0.0](https://github.com/stellar/go/releases/tag/horizonclient-v1.0) - 2019-04-26

* Initial release
14 changes: 8 additions & 6 deletions clients/horizonclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,30 @@ This library is aimed at developers building Go applications that interact with
import hClient "github.com/stellar/go/clients/horizonclient"
...

// use the default pubnet client
// Use the default pubnet client
client := hClient.DefaultPublicNetClient
// create an account request

// Create an account request
accountRequest := hClient.AccountRequest{AccountID: "GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU"}

// load the account detail from the network
// Load the account detail from the network
account, err := client.AccountDetail(accountRequest)
if err != nil {
fmt.Println(err)
return
}
// account contains information about the stellar account
// Account contains information about the stellar account
fmt.Print(account)

```
For more examples, refer to the [documentation](https://godoc.org/github.com/stellar/go/clients/horizonclient).

## Running the tests
Run the unit tests from the package directory: `go test`

## Contributing
Please read [CONTRIBUTING](../../CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
Please read [Code of Conduct](https://github.com/stellar/.github/blob/master/CODE_OF_CONDUCT.md) to understand this project's communication rules.

To submit improvements and fixes to this library, please see [CONTRIBUTING](../CONTRIBUTING.md).

## License
This project is licensed under the Apache License - see the [LICENSE](../../LICENSE-APACHE.txt) file for details.
37 changes: 22 additions & 15 deletions clients/horizonclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

"github.com/manucorporat/sse"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/protocols/horizon/effects"
"github.com/stellar/go/protocols/horizon/operations"
"github.com/stellar/go/support/app"
"github.com/stellar/go/support/errors"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func (c *Client) sendRequestURL(requestURL string, method string, a interface{})
return errors.Wrap(err, "error creating HTTP request")
}
c.setClientAppHeaders(req)

c.setDefaultClient()
if c.horizonTimeOut == 0 {
c.horizonTimeOut = HorizonTimeOut
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func (c *Client) stream(
return errors.Wrap(err, "error creating HTTP request")
}
req.Header.Set("Accept", "text/event-stream")
// to do: confirm name and version
c.setDefaultClient()
c.setClientAppHeaders(req)

// We can use c.HTTP here because we set Timeout per request not on the client. See sendRequest()
Expand Down Expand Up @@ -198,11 +198,18 @@ func (c *Client) stream(

func (c *Client) setClientAppHeaders(req *http.Request) {
req.Header.Set("X-Client-Name", "go-stellar-sdk")
req.Header.Set("X-Client-Version", app.Version())
req.Header.Set("X-Client-Version", c.Version())
req.Header.Set("X-App-Name", c.AppName)
req.Header.Set("X-App-Version", c.AppVersion)
}

// setDefaultClient sets the default HTTP client when none is provided.
func (c *Client) setDefaultClient() {
if c.HTTP == nil {
c.HTTP = http.DefaultClient
}
}

// fixHorizonURL strips all slashes(/) at the end of HorizonURL if any, then adds a single slash
func (c *Client) fixHorizonURL() string {
return strings.TrimRight(c.HorizonURL, "/") + "/"
Expand Down Expand Up @@ -251,7 +258,7 @@ func (c *Client) AccountData(request AccountRequest) (accountData hProtocol.Acco

// Effects returns effects(https://www.stellar.org/developers/horizon/reference/resources/effect.html)
// It can be used to return effects for an account, a ledger, an operation, a transaction and all effects on the network.
func (c *Client) Effects(request EffectRequest) (effects hProtocol.EffectsPage, err error) {
func (c *Client) Effects(request EffectRequest) (effects effects.EffectsPage, err error) {
err = c.sendRequest(request, &effects)
return
}
Expand All @@ -263,13 +270,6 @@ func (c *Client) Assets(request AssetRequest) (assets hProtocol.AssetsPage, err
return
}

// Stream is for endpoints that support streaming
func (c *Client) Stream(ctx context.Context, request StreamRequest, handler func(interface{})) (err error) {

err = request.Stream(ctx, c, handler)
return
}

// Ledgers returns information about all ledgers.
// See https://www.stellar.org/developers/horizon/reference/endpoints/ledgers-all.html
func (c *Client) Ledgers(request LedgerRequest) (ledgers hProtocol.LedgersPage, err error) {
Expand Down Expand Up @@ -422,11 +422,13 @@ func (c *Client) Trades(request TradeRequest) (tds hProtocol.TradesPage, err err

// Fund creates a new account funded from friendbot. It only works on test networks. See
// https://www.stellar.org/developers/guides/get-started/create-account.html for more information.
func (c *Client) Fund(addr string) (*http.Response, error) {
func (c *Client) Fund(addr string) (txSuccess hProtocol.TransactionSuccess, err error) {
if !c.isTestNet {
return nil, errors.New("Can't fund account from friendbot on production network")
return txSuccess, errors.New("can't fund account from friendbot on production network")
}
return http.Get(c.HorizonURL + "friendbot?addr=" + addr)
friendbotURL := fmt.Sprintf("%sfriendbot?addr=%s", c.fixHorizonURL(), addr)
err = c.sendRequestURL(friendbotURL, "get", &txSuccess)
return
}

// StreamTrades streams executed trades. It can be used to stream all trades, trades for an account and
Expand Down Expand Up @@ -520,5 +522,10 @@ func (c *Client) Root() (root hProtocol.Root, err error) {
return
}

// Version returns the current version.
func (c *Client) Version() string {
return version
}

// ensure that the horizon client implements ClientInterface
var _ ClientInterface = &Client{}
58 changes: 20 additions & 38 deletions clients/horizonclient/effect_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,38 @@ import (
)

// EffectHandler is a function that is called when a new effect is received
type EffectHandler func(effects.Base)
type EffectHandler func(effects.Effect)

// BuildURL creates the endpoint to be queried based on the data in the EffectRequest struct.
// If no data is set, it defaults to the build the URL for all effects
func (er EffectRequest) BuildURL() (endpoint string, err error) {

nParams := countParams(er.ForAccount, er.ForLedger, er.ForOperation, er.ForTransaction)

if nParams > 1 {
err = errors.New("invalid request: too many parameters")
}

if err != nil {
return endpoint, err
return endpoint, errors.New("invalid request: too many parameters")
}

endpoint = "effects"

if er.ForAccount != "" {
endpoint = fmt.Sprintf(
"accounts/%s/effects",
er.ForAccount,
)
endpoint = fmt.Sprintf("accounts/%s/effects", er.ForAccount)
}

if er.ForLedger != "" {
endpoint = fmt.Sprintf(
"ledgers/%s/effects",
er.ForLedger,
)
endpoint = fmt.Sprintf("ledgers/%s/effects", er.ForLedger)
}

if er.ForOperation != "" {
endpoint = fmt.Sprintf(
"operations/%s/effects",
er.ForOperation,
)
endpoint = fmt.Sprintf("operations/%s/effects", er.ForOperation)
}

if er.ForTransaction != "" {
endpoint = fmt.Sprintf(
"transactions/%s/effects",
er.ForTransaction,
)
endpoint = fmt.Sprintf("transactions/%s/effects", er.ForTransaction)
}

queryParams := addQueryParams(cursor(er.Cursor), limit(er.Limit), er.Order)
if queryParams != "" {
endpoint = fmt.Sprintf(
"%s?%s",
endpoint,
queryParams,
)
endpoint = fmt.Sprintf("%s?%s", endpoint, queryParams)
}

_, err = url.Parse(endpoint)
Expand All @@ -77,24 +56,27 @@ func (er EffectRequest) BuildURL() (endpoint string, err error) {
// StreamEffects streams horizon effects. It can be used to stream all effects or account specific effects.
// Use context.WithCancel to stop streaming or context.Background() if you want to stream indefinitely.
// EffectHandler is a user-supplied function that is executed for each streamed effect received.
func (er EffectRequest) StreamEffects(
ctx context.Context,
client *Client,
handler EffectHandler,
) (err error) {
func (er EffectRequest) StreamEffects(ctx context.Context, client *Client, handler EffectHandler) error {
endpoint, err := er.BuildURL()
if err != nil {
return errors.Wrap(err, "unable to build endpoint")
return errors.Wrap(err, "unable to build endpoint for effects request")
}

url := fmt.Sprintf("%s%s", client.fixHorizonURL(), endpoint)
return client.stream(ctx, url, func(data []byte) error {
var effect effects.Base
err = json.Unmarshal(data, &effect)
var baseEffect effects.Base
// unmarshal into the base effect type
if err = json.Unmarshal(data, &baseEffect); err != nil {
return errors.Wrap(err, "error unmarshaling data for effects request")
}

// unmarshal into the concrete effect type
effs, err := effects.UnmarshalEffect(baseEffect.GetType(), data)
if err != nil {
return errors.Wrap(err, "error unmarshaling data")
return errors.Wrap(err, "unmarshaling to the correct effect type")
}
handler(effect)

handler(effs)
return nil
})
}
Loading