Skip to content

Commit

Permalink
change requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagleft committed Dec 29, 2021
1 parent eb56a3d commit b5bbd0f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 58 deletions.
14 changes: 8 additions & 6 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uexchange
import (
"encoding/json"
"errors"
"net/url"
)

// Auth client
Expand All @@ -15,11 +16,12 @@ func (c *Client) Auth(cred Credentials) (*APIAuthResultContainer, error) {
}

c.APICredentials = cred
body, err := c.sendRequest(c.getAPIURL("user/login"), "POST", map[string]interface{}{
"PublicKey": cred.AccountPublicKey,
"password": cred.Password,
"2fa_pin": cred.TwoFACode,
})
reqFields := url.Values{}
reqFields.Add("PublicKey", cred.AccountPublicKey)
reqFields.Add("password", cred.Password)
reqFields.Add("2fa_pin", cred.TwoFACode)

body, err := c.sendRequest(c.getAPIURL("user/login"), "POST", reqFields)
if err != nil {
return nil, err
}
Expand All @@ -44,7 +46,7 @@ func (c *Client) Auth(cred Credentials) (*APIAuthResultContainer, error) {

// Logout - close auth session
func (c *Client) Logout() error {
body, err := c.sendRequest(c.getAPIURL("user/logout"), "POST", mapTable{})
body, err := c.sendRequest(c.getAPIURL("user/logout"), "POST", url.Values{})
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package uexchange
import (
"encoding/json"
"errors"
"net/url"
)

// GetBalance - get balance data for all coins
func (c *Client) GetBalance() ([]BalanceData, error) {
body, err := c.sendRequest(c.getAPIURL("user/balance"), "GET", mapTable{})
body, err := c.sendRequest(c.getAPIURL("user/balance"), "GET", url.Values{})
if err != nil {
return nil, err
}
Expand Down
56 changes: 38 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package uexchange

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

// NewClient - ..
Expand All @@ -17,37 +17,57 @@ func (c *Client) getAPIURL(endpoint string) string {
return apiHost + ":" + apiPort + "/" + endpoint
}

func (c *Client) sendRequest(url string, requestType string, data map[string]interface{}) ([]byte, error) {
// declare http client
httpClient := &http.Client{}

// encode data fields to json
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, errors.New("failed to encode request data to json: " + err.Error())
}

// declare HTTP Method and Url
func (c *Client) sendRequest(requestURL string, requestType string, params url.Values) ([]byte, error) {
switch requestType {
default:
return nil, errors.New("invalid request type given: " + requestType)
case "POST":
break
return c.sendPOSTRequest(requestURL, params)
case "GET":
break
return c.sendGETRequest(requestURL, params)
}
req, err := http.NewRequest(requestType, url, bytes.NewBuffer(dataBytes))
}

func (c *Client) sendGETRequest(requestURL string, params url.Values) ([]byte, error) {
// send request
resp, err := http.Get(requestURL + "?" + params.Encode())
if err != nil {
return nil, errors.New("failed to send " + requestType + " request: " + err.Error())
return nil, err
}

// read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("failed to read request response: " + err.Error())
}

defer resp.Body.Close()
return body, nil
}

func (c *Client) sendPOSTRequest(requestURL string, params url.Values) ([]byte, error) {
// declare http client
httpClient := &http.Client{}

// create request
req, err := http.NewRequest("POST", requestURL, strings.NewReader(params.Encode()))
if err != nil {
return nil, errors.New("failed to send POST request: " + err.Error())
}

// set cookie
if c.AuthToken != "" {
req.Header.Set("Cookie", "auth_token="+c.AuthToken)
req.AddCookie(&http.Cookie{
Name: "auth_token",
Value: c.AuthToken,
})
}

// send request
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}

// read response
body, err := ioutil.ReadAll(resp.Body)
Expand Down
18 changes: 9 additions & 9 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package uexchange
import (
"encoding/json"
"errors"
"net/url"
)

// GetTradeHistory - get trading history by pairs
func (c *Client) GetTradeHistory(pairSymbol string) (*TradeHistoryDataContainer, error) {
body, err := c.sendRequest(c.getAPIURL("history/trade"), "GET", mapTable{
"pair": pairSymbol,
})
reqFields := url.Values{}
reqFields.Add("pair", pairSymbol)
body, err := c.sendRequest(c.getAPIURL("history/trade"), "GET", reqFields)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -74,17 +75,16 @@ func (s *GetAccountHistoryService) SetCurrency(newCurrency string) *GetAccountHi

// Do request
func (s *GetAccountHistoryService) Do() (*OperationsHistoryDataContainer, error) {
requestFieldsMap := mapTable{
"type": s.RequestType,
}
requestFieldsMap := url.Values{}
requestFieldsMap.Add("type", s.RequestType)
if s.FromID != "" {
requestFieldsMap["from_id"] = s.FromID
requestFieldsMap.Add("from_id", s.FromID)
}
if s.RecordType != "" {
requestFieldsMap["record_type"] = s.RecordType
requestFieldsMap.Add("record_type", s.RecordType)
}
if s.Currency != "" {
requestFieldsMap["currency"] = s.Currency
requestFieldsMap.Add("currency", s.Currency)
}

body, err := s.ExchangeClient.sendRequest(
Expand Down
13 changes: 7 additions & 6 deletions orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uexchange
import (
"encoding/json"
"errors"
"net/url"
)

// GetOrdersService - get orders service with optional params
Expand Down Expand Up @@ -33,12 +34,12 @@ func (s *GetOrdersService) SetTaskType(taskType string) *GetOrdersService {

// Do request
func (s *GetOrdersService) Do() (*OrdersDataContainer, error) {
requestFieldsMap := mapTable{}
requestFieldsMap := url.Values{}
if s.OrderType != "" {
requestFieldsMap["status"] = s.OrderType
requestFieldsMap.Add("status", s.OrderType)
}
if s.TaskType != "" {
requestFieldsMap["task"] = s.TaskType
requestFieldsMap.Add("task", s.TaskType)
}

body, err := s.ExchangeClient.sendRequest(
Expand All @@ -65,9 +66,9 @@ func (s *GetOrdersService) Do() (*OrdersDataContainer, error) {

// GetOrderHistory - get orders history
func (c *Client) GetOrderHistory(orderID string) (*OrdersHistoryDataContainer, error) {
body, err := c.sendRequest(c.getAPIURL("orders/history"), "POST", mapTable{
"order_id": orderID,
})
reqFields := url.Values{}
reqFields.Add("order_id", orderID)
body, err := c.sendRequest(c.getAPIURL("orders/history"), "POST", reqFields)
if err != nil {
return nil, err
}
Expand Down
39 changes: 21 additions & 18 deletions trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package uexchange
import (
"encoding/json"
"errors"
"net/url"
"strconv"
)

func (c *Client) sendTradeTask(orderType string, pairSymbol string, amount, price float64) (int64, error) {
body, err := c.sendRequest(c.getAPIURL("market/"+orderType), "POST", mapTable{
"pair": pairSymbol,
"amount": amount,
"price": price,
})
reqFields := url.Values{}
reqFields.Add("pair", pairSymbol)
reqFields.Add("amount", strconv.FormatFloat(amount, 'f', 8, 64))
reqFields.Add("price", strconv.FormatFloat(price, 'f', 8, 64))

body, err := c.sendRequest(c.getAPIURL("market/"+orderType), "POST", reqFields)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -40,9 +43,9 @@ func (c *Client) Sell(pairSymbol string, amount, price float64) (int64, error) {

// Hold or Unhold order
func (c *Client) Hold(orderID int64) error {
body, err := c.sendRequest(c.getAPIURL("market/hold"), "POST", mapTable{
"order_id": orderID,
})
reqFields := url.Values{}
reqFields.Add("order_id", strconv.FormatInt(orderID, 10))
body, err := c.sendRequest(c.getAPIURL("market/hold"), "POST", reqFields)
if err != nil {
return err
}
Expand All @@ -62,9 +65,9 @@ func (c *Client) Hold(orderID int64) error {

// Cancel the specified order
func (c *Client) Cancel(orderID int64) error {
body, err := c.sendRequest(c.getAPIURL("market/cancel"), "POST", mapTable{
"order_id": orderID,
})
reqFields := url.Values{}
reqFields.Add("order_id", strconv.FormatInt(orderID, 10))
body, err := c.sendRequest(c.getAPIURL("market/cancel"), "POST", reqFields)
if err != nil {
return err
}
Expand All @@ -84,7 +87,7 @@ func (c *Client) Cancel(orderID int64) error {

// GetPairs - get trading pairs list
func (c *Client) GetPairs() ([]PairsDataContainer, error) {
body, err := c.sendRequest(c.getAPIURL("market/pairs"), "GET", mapTable{})
body, err := c.sendRequest(c.getAPIURL("market/pairs"), "GET", url.Values{})
if err != nil {
return nil, err
}
Expand All @@ -104,9 +107,9 @@ func (c *Client) GetPairs() ([]PairsDataContainer, error) {

// GetOrderBook by trade pair
func (c *Client) GetOrderBook(pairSymbol string) (*BookValueDataContainer, error) {
body, err := c.sendRequest(c.getAPIURL("market/panel"), "POST", mapTable{
"pair": pairSymbol,
})
reqFields := url.Values{}
reqFields.Add("pair", pairSymbol)
body, err := c.sendRequest(c.getAPIURL("market/panel"), "POST", reqFields)
if err != nil {
return nil, err
}
Expand All @@ -126,9 +129,9 @@ func (c *Client) GetOrderBook(pairSymbol string) (*BookValueDataContainer, error

// GetMarketCurrenciesList - get exchange currencies list
func (c *Client) GetMarketCurrenciesList(pairSymbol string) (*CurrenciesListData, error) {
body, err := c.sendRequest(c.getAPIURL("market/curlist"), "GET", mapTable{
"pair": pairSymbol,
})
reqFields := url.Values{}
reqFields.Add("pair", pairSymbol)
body, err := c.sendRequest(c.getAPIURL("market/curlist"), "GET", reqFields)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b5bbd0f

Please sign in to comment.