Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aliereno committed Apr 19, 2020
1 parent 2db56e7 commit 58ad3a5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 41 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ func main() {

```

## Passing params
## Examples

[Endpoint Params](https://github.com/aliereno/go-btcturk/blob/master/btcturk/params.go)
```go
package main

Expand All @@ -59,9 +58,26 @@ import (

func main() {
api := btcturk.NewBTCTurkClient()
api.SetAuthKey("publicKey", "privateKey")

api.Quantity(0.001).
//PUBLIC ENDPOINTS

//TICKER
_, _ = api.PairSymbol(btcturk.BTCTRY).Ticker()

//ORDER BOOK
_, _ = api.PairSymbol(btcturk.BTCTRY).Limit(10).OrderBook() // limit optional

//TRADES
_, _ = api.PairSymbol(btcturk.BTCTRY).Trades()


//PRIVATE ENDPOINTS

_, _ = api.SetAuthKey("publicKey", "privateKey")

_, _ = api.Balance()

_, _ = api.Quantity(0.001).
Price(50000).
StopPrice(0).
OrderMethod("limit").
Expand Down
31 changes: 8 additions & 23 deletions btcturk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type Client struct {
}
type GeneralResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Code int8 `json:"code"`
Message *string `json:"message"`
Code int `json:"code"`
Data interface{} `json:"data"`
}

Expand Down Expand Up @@ -68,22 +68,6 @@ func (c *Client) newRequest(method, relURL string, body io.Reader) (*http.Reques
return req, nil
}

func (c *Client) newRequestCustomURL(method, relURL string, body io.Reader) (*http.Request, error) {
rel, err := url.Parse(relURL)
if err != nil {
return nil, err
}

req, err := http.NewRequest(method, c.baseURL.ResolveReference(rel).String(), body)
if err != nil {
return nil, err
}

req = req.WithContext(context.Background())

return req, nil
}

func (c *Client) do(r *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.client.Do(r)
if err != nil {
Expand All @@ -104,30 +88,30 @@ func (c *Client) do(r *http.Request, v interface{}) (*http.Response, error) {
return nil, err
}

if response.Success == true {
if response.Code == 0 {
v = response.Data
} else {
return nil, errors.New("response -> 'success = false'")
return nil, errors.New(*response.Message)
}

return resp, nil
}

func (c *Client) auth(req *http.Request) error {
if c.privateKey == "" {
return errors.New("Private Key is not set")
return errors.New("private key is not set")
}

if c.publicKey == "" {
return errors.New("Public Key is not set")
return errors.New("public key is not set")
}

key, err := base64.StdEncoding.DecodeString(c.privateKey)
if err != nil {
return err
}

stamp := fmt.Sprint(time.Now().Unix())
stamp := fmt.Sprint(time.Now().Unix() * 1000)
message := c.publicKey + stamp

h := hmac.New(sha256.New, key)
Expand All @@ -136,6 +120,7 @@ func (c *Client) auth(req *http.Request) error {
req.Header.Set("X-PCK", c.publicKey)
req.Header.Set("X-Stamp", stamp)
req.Header.Set("X-Signature", base64.StdEncoding.EncodeToString(h.Sum(nil)))
req.Header.Add("Content-Type", "application/json")
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions btcturk/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func (c *Client) PairSymbol(v string) *Client {
return c
}

// Custom Param
func (c *Client) AddCustomParam(k string, v string) *Client {
c.params.Add(k, v)
return c
}

func (c *Client) addParamInt(key string, value int) *Client {
c.params.Add(key, strconv.Itoa(value))
return c
Expand Down
13 changes: 9 additions & 4 deletions btcturk/trades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package btcturk
import "fmt"

type Trade struct {
TimeStamp float64 `json:"date"`
TID string `json:"tid"`
Price string `json:"price"`
Amount string `json:"amount"`
Pair string `json:"pair"`
PairNormalized string `json:"pairNormalized"`
Numerator string `json:"numerator"`
Denominator string `json:"denominator"`
TimeStamp float64 `json:"date"`
TID string `json:"tid"`
Price string `json:"price"`
Amount string `json:"amount"`
Side string `json:"side"`
}

func (c *Client) Trades() ([]Trade, error) {
Expand Down
2 changes: 1 addition & 1 deletion btcturk/user_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Balance struct {
}

func (c *Client) Balance() ([]Balance, error) {
req, err := c.newRequest("GET", "/api/v1/users/balances", c.body)
req, err := c.newRequest("GET", "/api/v1/users/balances", nil)
if err != nil {
return []Balance{}, err
}
Expand Down
23 changes: 14 additions & 9 deletions btcturk/user_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"
)

const (
Expand Down Expand Up @@ -81,7 +80,8 @@ type OrderType struct {
}

func (c *Client) OpenOrders() ([]OpenOrders, error) {
req, err := c.newRequest("GET", fmt.Sprintf("/api/v1/openOrders?%s", c.params.Encode()), nil)
jsonString, err := json.Marshal(c.params)
req, err := c.newRequest("GET", "/api/v1/openOrders", bytes.NewBuffer(jsonString))
if err != nil {
return []OpenOrders{}, err
}
Expand All @@ -98,9 +98,7 @@ func (c *Client) OpenOrders() ([]OpenOrders, error) {
}

func (c *Client) CancelOrder() (bool, error) {
c.body = strings.NewReader(c.params.Encode())

req, err := c.newRequest("POST", "/api/v1/order", c.body)
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/v1/order?%s", c.params.Encode()), c.body)
if err != nil {
return false, err
}
Expand All @@ -109,6 +107,11 @@ func (c *Client) CancelOrder() (bool, error) {
}

var response GeneralResponse

// TODO
// API returns `"code":""`
// my code expects `"code":0` an integer
// so it will return error
if _, err = c.do(req, &response); err != nil {
return false, err
}
Expand All @@ -119,6 +122,9 @@ func (c *Client) CancelOrder() (bool, error) {
func (c *Client) Buy() (OrderType, error) {
c.params.Add("orderType", "buy")
jsonString, err := json.Marshal(c.params)
if err != nil {
return OrderType{}, err
}

req, err := c.newRequest("POST", "/api/v1/order", bytes.NewBuffer(jsonString))
if err != nil {
Expand All @@ -128,8 +134,6 @@ func (c *Client) Buy() (OrderType, error) {
return OrderType{}, err
}

req.Header.Add("content-type", "application/json")

var response OrderType
if _, err = c.do(req, &response); err != nil {
return OrderType{}, err
Expand All @@ -141,6 +145,9 @@ func (c *Client) Buy() (OrderType, error) {
func (c *Client) Sell() (OrderType, error) {
c.params.Add("orderType", "sell")
jsonString, err := json.Marshal(c.params)
if err != nil {
return OrderType{}, err
}

req, err := c.newRequest("POST", "/api/v1/order", bytes.NewBuffer(jsonString))
if err != nil {
Expand All @@ -150,8 +157,6 @@ func (c *Client) Sell() (OrderType, error) {
return OrderType{}, err
}

req.Header.Add("content-type", "application/json")

var response OrderType
if _, err = c.do(req, &response); err != nil {
return OrderType{}, err
Expand Down

0 comments on commit 58ad3a5

Please sign in to comment.