diff --git a/auth.go b/auth.go index 45aba5d..4bb90b8 100644 --- a/auth.go +++ b/auth.go @@ -3,6 +3,7 @@ package uexchange import ( "encoding/json" "errors" + "net/url" ) // Auth client @@ -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 } @@ -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 } diff --git a/balance.go b/balance.go index c568d98..1bbc254 100644 --- a/balance.go +++ b/balance.go @@ -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 } diff --git a/client.go b/client.go index 03424bc..a304d07 100644 --- a/client.go +++ b/client.go @@ -1,11 +1,11 @@ package uexchange import ( - "bytes" - "encoding/json" "errors" "io/ioutil" "net/http" + "net/url" + "strings" ) // NewClient - .. @@ -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) diff --git a/history.go b/history.go index cffde55..5de1926 100644 --- a/history.go +++ b/history.go @@ -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 } @@ -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( diff --git a/orders.go b/orders.go index fe1f130..bc122bf 100644 --- a/orders.go +++ b/orders.go @@ -3,6 +3,7 @@ package uexchange import ( "encoding/json" "errors" + "net/url" ) // GetOrdersService - get orders service with optional params @@ -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( @@ -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 } diff --git a/trade.go b/trade.go index 806ae73..e869a37 100644 --- a/trade.go +++ b/trade.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 }