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

Allow for custom return structs #55

Open
drakos74 opened this issue Aug 19, 2020 · 2 comments
Open

Allow for custom return structs #55

drakos74 opened this issue Aug 19, 2020 · 2 comments

Comments

@drakos74
Copy link

drakos74 commented Aug 19, 2020

As the API of kraken evolves (new crypto-currencies , response structure etc ... )
It would be helpful for users to be able to define their own response structs.
Currently these are "hidden".
For example in Balance call

// Balance returns all account asset balances
func (api *KrakenAPI) Balance() (*BalanceResponse, error) {
	resp, err := api.queryPrivate("Balance", url.Values{}, &BalanceResponse{})
	if err != nil {
		return nil, err
	}

	return resp.(*BalanceResponse), nil
}

Someone cannot get the balance for 'LINK' for example, because the BalanceResponse struct does not parse this field from the json response.

This method could be more generic, in order to accommodate current (i.e. for LINK) and future needs, without the need for the client to define ALL currencies upfront.

An possible adjustment could be the following :

// Balance returns all account asset balances
func (api *KrakenAPI) Balance(response interface{}) error {
	response, err := api.queryPrivate("Balance", url.Values{}, response)
	if err != nil {
		return err
	}
	return nil
}

I 've made some tests, and it seems to work nicely for the this example of client user code.

type BalanceResponse struct {
	ZEUR  float64 `json:"ZEUR,string"`
	LINK  float64 `json:"LINK,string"`
}

func (api *Remote) Balance() (*BalanceResponse, error) {
	resp := &BalanceResponse{}
	err := api.privateApi.Balance(resp)
	return resp, err
}

and its used as ...

balance, err := client.Api.Balance()

	if err == nil {
		println(fmt.Sprintf("balance = %v", balance.ZEUR))
		println(fmt.Sprintf("balance = %v", balance.LINK))
	}

Would this be an interesting enhancement ? I could look into refactoring corresponding methods in that direction.
And we could always leave the current struct for default use-cases.
What do you think ?

@Glavic
Copy link
Contributor

Glavic commented Oct 10, 2020

What about just updating BalanceResponse type and add missing assets?

@drakos74
Copy link
Author

thanks for the reply @Glavic . That would also be an option, for sure.
I personally think that the above is more generic, and there is no need for adjustments on every new currency.

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

2 participants