Skip to content

Commit

Permalink
Create a Login method for api
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Mar 17, 2022
1 parent b648744 commit b89baa8
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 31 deletions.
22 changes: 21 additions & 1 deletion v4/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (a *Api) AuthorizationToken() (models.AuthoritionToken, error) {
return auth, err
}

if err := a.SetAuthorize(auth); err != nil {
if err := a.CacheSetAuthorize(auth); err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("SetAuthorize")
}
Expand All @@ -99,3 +99,23 @@ func (a *Api) AuthorizationToken() (models.AuthoritionToken, error) {

return auth, nil
}

func (a *Api) Login() (models.AuthoritionToken, models.ListAccountsResponse, error) {
auth, err := a.AuthorizationToken()
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("AuthorizationToken")
}
return models.AuthoritionToken{}, models.ListAccountsResponse{}, err
}

acc, err := a.GetAccounts()
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("GetAccounts")
}
return models.AuthoritionToken{}, models.ListAccountsResponse{}, err
}

return auth, acc, nil
}
35 changes: 33 additions & 2 deletions v4/api/api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/thiagozs/go-mbsdk/v4/config"
"github.com/thiagozs/go-mbsdk/v4/models"
"github.com/thiagozs/go-mbsdk/v4/pkg/caller"
"github.com/thiagozs/go-mbsdk/v4/pkg/replacer"
Expand All @@ -13,26 +14,41 @@ func (a *Api) GetBalances() (models.ListBalancesResponse, error) {
balances := models.ListBalancesResponse{}
c, err := caller.ClientWithToken(http.MethodGet, a.cache)
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("ClientWithToken")
}
return balances, err
}

endpoint, err := replacer.Endpoint(replacer.OptKey("BALANCE_LIST"),
replacer.OptCache(a.cache),
)
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("Replacer")
}
return balances, err
}

bts, err := c.Get(endpoint)
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("Get")
}
return balances, err
}

if err := json.Unmarshal(bts, &balances); err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("Json Unmarshal Balances")
}
return balances, err
}

if err := a.SetBalance(balances); err != nil {
if err := a.CacheSetBalance(balances); err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("CacheSetBalance")
}
return balances, err
}

Expand All @@ -45,26 +61,41 @@ func (a *Api) GetAccounts() (models.ListAccountsResponse, error) {

c, err := caller.ClientWithToken(http.MethodGet, a.cache)
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("ClientWithToken")
}
return acc, err
}

endpoint, err := replacer.Endpoint(replacer.OptKey("ACCOUNTS"),
replacer.OptCache(a.cache),
)
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("Replacer")
}
return acc, err
}

bts, err := c.Get(endpoint)
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("Get")
}
return acc, err
}

if err := json.Unmarshal(bts, &acc); err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("Json Unmarshal ListAccountsResponse")
}
return acc, err
}

if err := a.SetAccounts(acc); err != nil {
if err := a.CacheSetAccounts(acc); err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("SetAccounts")
}
return acc, err
}

Expand Down
22 changes: 9 additions & 13 deletions v4/api/api_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,40 @@ import (
"github.com/thiagozs/go-mbsdk/v4/models"
)

func (a *Api) SetBalance(balance models.ListBalancesResponse) error {
func (a *Api) CacheSetBalance(balance models.ListBalancesResponse) error {
return a.cache.SetKeyValAsJSON(config.BALANCE.String(), balance)
}

func (a *Api) SetAccounts(acc models.ListAccountsResponse) error {
func (a *Api) CacheSetAccounts(acc models.ListAccountsResponse) error {
return a.cache.SetKeyValAsJSON(config.ACCOUNTS.String(), acc)
}

func (a *Api) SetAuthorize(auth models.AuthoritionToken) error {
func (a *Api) CacheSetAuthorize(auth models.AuthoritionToken) error {
return a.cache.SetKeyValAsJSON(config.AUTHORIZE.String(), auth)
}

func (a *Api) SetOrder(orderIn []models.OrdersIndex) error {

func (a *Api) CacheSetOrder(orderIn []models.OrdersIndex) error {
orders := []models.OrdersIndex{}

val, err := a.cache.GetKeyVal(config.ORDERS_INDEX.String())
if err != nil && err.Error() != "not found" {
return err
}

if len(val) > 0 {
if err := json.Unmarshal([]byte(val), &orders); err != nil {
return err
}
}

orders = append(orders, orderIn...)

return a.cache.SetKeyValAsJSON(config.ORDERS_INDEX.String(), orders)
}

// func (a *Api) GetOrder(key string) (string, error) {
// return a.cache.GetKeyVal(key)
// }
func (a *Api) CacheGetOrder() (string, error) {
return a.cache.GetKeyVal(config.ORDERS_INDEX.String())
}

func (a *Api) DeleteOrder(key string) (string, error) {
return a.cache.DeleteKey(key)
func (a *Api) CacheDeleteOrder() (string, error) {
return a.cache.DeleteKey(config.ORDERS_INDEX.String())
}

func (a *Api) GetKeyVal(key string) (string, error) {
Expand Down
13 changes: 13 additions & 0 deletions v4/api/api_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/google/go-querystring/query"
"github.com/thiagozs/go-mbsdk/v4/config"
"github.com/thiagozs/go-mbsdk/v4/models"
"github.com/thiagozs/go-mbsdk/v4/pkg/caller"
"github.com/thiagozs/go-mbsdk/v4/pkg/replacer"
Expand All @@ -16,6 +17,9 @@ func (a *Api) Tickers(symbol string) (models.TickersResponse, error) {

c, err := caller.ClientPublic(http.MethodGet, a.cache)
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("ClientPublic")
}
return tickers, err
}

Expand All @@ -26,15 +30,24 @@ func (a *Api) Tickers(symbol string) (models.TickersResponse, error) {
replacer.OptCache(a.cache),
)
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("Replacer")
}
return tickers, err
}

bts, err := c.Get(fmt.Sprintf("%s?%s", endpoint, v.Encode()))
if err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("Get")
}
return tickers, err
}

if err := json.Unmarshal(bts, &tickers); err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("Json Unmarshal Tickers")
}
return tickers, err
}

Expand Down
4 changes: 2 additions & 2 deletions v4/api/api_trading.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (a *Api) PlaceOrder(opts ...PlaceOrdersParams) models.CustomPlaceOrderInfo
return orderInfo
}

if err := a.SetOrder([]models.OrdersIndex{
if err := a.CacheSetOrder([]models.OrdersIndex{
{
Symbol: params.Symbol,
ID: respOrder.OrderID,
Expand Down Expand Up @@ -335,7 +335,7 @@ func (a *Api) CancelAllOrders(symbol string) error {
}
}

if err := a.SetOrder(ordersIndex); err != nil {
if err := a.CacheSetOrder(ordersIndex); err != nil {
if config.Config.Debug {
a.log.Error().Stack().Err(err).Msg("SetOrder")
}
Expand Down
18 changes: 5 additions & 13 deletions v4/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,21 @@ func main() {
fmt.Println(err)
}

// step 1 - get all authorization (mandatory)
if auth, err := a.AuthorizationToken(); err != nil {
auth, acc, err := a.Login()
if err != nil {
fmt.Println(err)
} else {
fmt.Println(auth)
return
}

// step 2 - run the account function to get all info (mandatory)
if acc, err := a.GetAccounts(); err != nil {
fmt.Println(err)
} else {
fmt.Println(acc)
}
fmt.Printf("%+v\n", auth)
fmt.Printf("%+v\n", acc)

// step 3 - run others methods, before that, you need
// run the function account to get all information
if balances, err := a.GetBalances(); err != nil {
fmt.Println(err)
} else {
fmt.Println(balances)
}

// step 4 - publics endpoints
if ticker, err := a.Tickers("BTC-BRL"); err != nil {
fmt.Println(err)
} else {
Expand Down

0 comments on commit b89baa8

Please sign in to comment.