Skip to content

Commit

Permalink
Release: v1.0.25-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-the-h committed Oct 16, 2021
1 parent 96a59f7 commit bb2068b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 47 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ Changelog
=========
All notable changes to this project will be documented in this file.

v1.0.25-alpha
------------

### Changed

- Fixed login process

v1.0.24-alpha
------------

Expand Down
37 changes: 26 additions & 11 deletions api/ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/amir-the-h/okex"
"github.com/amir-the-h/okex/events"
"github.com/gorilla/websocket"
"log"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -107,14 +108,17 @@ func (c *ClientWs) Connect(p bool) error {
//
// https://www.okex.com/docs-v5/en/#websocket-api-login
func (c *ClientWs) Login() error {
c.mu[true].Lock()
if c.Authorized {
return nil
}
if c.AuthRequested != nil && time.Since(*c.AuthRequested) < 30 {
if c.AuthRequested != nil && time.Since(*c.AuthRequested).Seconds() < 30 {
return nil
}
log.Println("login")
now := time.Now()
c.AuthRequested = &now
c.mu[true].Unlock()
method := http.MethodGet
path := "/users/self/verify"
ts, sign := c.sign(method, path)
Expand Down Expand Up @@ -167,10 +171,20 @@ func (c *ClientWs) Unsubscribe(p bool, ch []okex.ChannelName, args map[string]st

// Send message through either connections
func (c *ClientWs) Send(p bool, op okex.Operation, args []map[string]string, extras ...map[string]string) error {
err := c.Connect(p)
if err != nil {
return err
if op != okex.LoginOperation {
err := c.Connect(p)
if err == nil {
if p {
err = c.WaitForAuthorization()
if err != nil {
return err
}
}
} else {
return err
}
}

data := map[string]interface{}{
"op": op,
"args": args,
Expand Down Expand Up @@ -199,11 +213,14 @@ func (c *ClientWs) SetChannels(errCh chan *events.Error, subCh chan *events.Subs

// WaitForAuthorization waits for the auth response and try to log in if it was needed
func (c *ClientWs) WaitForAuthorization() error {
ticker := time.NewTicker(time.Millisecond * 300)
defer ticker.Stop()
if c.Authorized {
return nil
}
if err := c.Login(); err != nil {
return err
}
ticker := time.NewTicker(time.Millisecond * 300)
defer ticker.Stop()
for range ticker.C {
if c.Authorized {
return nil
Expand All @@ -220,21 +237,20 @@ func (c *ClientWs) dial(p bool) error {
c.rmu[p].Unlock()
}()
conn, res, err := websocket.DefaultDialer.Dial(string(c.url[p]), nil)
defer res.Body.Close()
if err != nil {
return fmt.Errorf("error %d: %s", res.StatusCode, err)
return fmt.Errorf("error %d: %w", res.StatusCode, err)
}
go func() {
err := c.receiver(p)
if err != nil {
fmt.Printf("receiver error: %v\n", err)
c.Cancel()
}
}()
go func() {
err := c.sender(p)
if err != nil {
fmt.Printf("sender error: %v\n", err)
c.Cancel()
}
}()
c.conn[p] = conn
Expand Down Expand Up @@ -362,8 +378,7 @@ func (c *ClientWs) process(data []byte, e *events.Basic) bool {
}()
return true
case "login":
au := time.Now().Add(time.Second * -30)
if au.After(*c.AuthRequested) {
if time.Since(*c.AuthRequested).Seconds() > 30 {
c.AuthRequested = nil
_ = c.Login()
break
Expand Down
24 changes: 0 additions & 24 deletions api/ws/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ func (c *Private) Account(req requests.Account, ch ...chan *private.Account) err
if len(ch) > 0 {
c.aCh = ch[0]
}
err := c.WaitForAuthorization()
if err != nil {
return err
}
return c.Subscribe(true, []okex.ChannelName{"account"}, m)
}

Expand All @@ -60,10 +56,6 @@ func (c *Private) Position(req requests.Position, ch ...chan *private.Position)
if len(ch) > 0 {
c.pCh = ch[0]
}
err := c.WaitForAuthorization()
if err != nil {
return err
}
return c.Subscribe(true, []okex.ChannelName{"positions"}, m)
}

Expand All @@ -75,10 +67,6 @@ func (c *Private) UPosition(req requests.Position, rCh ...bool) error {
if len(rCh) > 0 && rCh[0] {
c.pCh = nil
}
err := c.WaitForAuthorization()
if err != nil {
return err
}
return c.Unsubscribe(true, []okex.ChannelName{"positions"}, m)
}

Expand All @@ -91,10 +79,6 @@ func (c *Private) BalanceAndPosition(ch ...chan *private.BalanceAndPosition) err
if len(ch) > 0 {
c.bnpCh = ch[0]
}
err := c.WaitForAuthorization()
if err != nil {
return err
}
return c.Subscribe(true, []okex.ChannelName{"balance_and_position"}, m)
}

Expand All @@ -118,10 +102,6 @@ func (c *Private) Order(req requests.Order, ch ...chan *private.Order) error {
if len(ch) > 0 {
c.oCh = ch[0]
}
err := c.WaitForAuthorization()
if err != nil {
return err
}
return c.Subscribe(true, []okex.ChannelName{"orders"}, m)
}

Expand All @@ -133,10 +113,6 @@ func (c *Private) UOrder(req requests.Order, rCh ...bool) error {
if len(rCh) > 0 && rCh[0] {
c.oCh = nil
}
err := c.WaitForAuthorization()
if err != nil {
return err
}
return c.Unsubscribe(true, []okex.ChannelName{"orders"}, m)
}

Expand Down
12 changes: 0 additions & 12 deletions api/ws/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ func (c *Trade) PlaceOrder(req ...requests.PlaceOrder) error {
for i, order := range req {
tmpArgs[i] = okex.S2M(order)
}
err := c.WaitForAuthorization()
if err != nil {
return err
}
return c.Send(true, op, tmpArgs, map[string]string{"id": req[0].ID})
}

Expand All @@ -58,10 +54,6 @@ func (c *Trade) CancelOrder(req ...requests.CancelOrder) error {
for i, order := range req {
tmpArgs[i] = okex.S2M(order)
}
err := c.WaitForAuthorization()
if err != nil {
return err
}
return c.Send(true, op, tmpArgs, map[string]string{"id": req[0].ID})
}

Expand All @@ -82,9 +74,5 @@ func (c *Trade) AmendOrder(req ...requests.AmendOrder) error {
for i, order := range req {
tmpArgs[i] = okex.S2M(order)
}
err := c.WaitForAuthorization()
if err != nil {
return err
}
return c.Send(true, op, tmpArgs, map[string]string{"id": req[0].ID})
}

0 comments on commit bb2068b

Please sign in to comment.