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

support to use custom http client #25

Open
choleraehyq opened this issue Oct 18, 2020 · 2 comments
Open

support to use custom http client #25

choleraehyq opened this issue Oct 18, 2020 · 2 comments

Comments

@choleraehyq
Copy link

Sometimes we need to use custom Transport or set Timeout.

@eynzhang
Copy link
Contributor

@choleraehyq do you have more concret example?

@filinvadim
Copy link

For example instead of:

type MarketClient struct {
	publicUrlBuilder *requestbuilder.PublicUrlBuilder
}

// Initializer
func (p *MarketClient) Init(host string) *MarketClient {
	p.publicUrlBuilder = new(requestbuilder.PublicUrlBuilder).Init(host)
	return p
}

Use pattern 'Constructor' and pass HTTP client as interface argument:

type Requestor interface{
    Get(string) ([]byte, error)
    Post(string, map[string]string) ([]byte, error)
}
type MarketClient struct {
	publicUrlBuilder *requestbuilder.PublicUrlBuilder
        client Requestor
}


func NewMarket(host string, cli Requestor) *MarketClient {
	p.publicUrlBuilder = new(requestbuilder.PublicUrlBuilder).Init(host)
        p.client = cli
	return p
}

func (mc *MarketClient) GetLatestTrade(symbol string) (*market.TradeTick, error) {
	request := new(model.GetRequest).Init()
	request.AddParam("symbol", symbol)

	url := mc.publicUrlBuilder.Build("/market/trade", request)
	resp, err := mc.cli.Get(url)
	if err != nil {
		return nil, err
	}
        result := market.GetLatestTradeResponse{}
	err = json.Unmarshal(resp, &result)
	if err != nil {
		return nil, err
	}
	if result.Status == "ok" && result.Tick != nil {
		return result.Tick, nil
	}
	return nil, errors.New(resp)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants