(Engine) Variety of engine updates (#390)

* drop common uuid v4 func and imported package as needed

* removed common functions regarding json marshal and unmarshal and used the json package directly. WRT unmarshal it was calling reflect and converted to string which is also checked in the JSON package so it was doing a double up, this will be a tiny gain as it was directly used in the requester package for all our outbound requests.

* add in string

* explicitly throw away return error value

* atleast return the error that websocket initialise returns

* return error when not connected

* fix comment

* Adds comments

* move package declarations

* drop append whenever we call supported

* remove unused import

* Change incorrect spelling

* fix tests

* fix go import issue
This commit is contained in:
Ryan O'Hara-Reid
2019-12-03 10:06:08 +11:00
committed by Adrian Gallagher
parent c27b8657e2
commit 0c5d75b22c
70 changed files with 393 additions and 462 deletions

View File

@@ -10,7 +10,7 @@ func TestNewComm(t *testing.T) {
var cfg config.CommunicationsConfig
_, err := NewComm(&cfg)
if err == nil {
t.Error("NewComm should failed on no enabled communication mediums")
t.Error("NewComm should have failed on no enabled communication mediums")
}
cfg.TelegramConfig.Enabled = true

View File

@@ -82,7 +82,7 @@ func (s *Slack) PushEvent(event base.Event) error {
return s.WebsocketSend("message",
fmt.Sprintf("event: %s %s", event.Type, event.Message))
}
return nil
return errors.New("slack not connected")
}
// BuildURL returns an appended token string with the SlackURL
@@ -212,7 +212,7 @@ func (s *Slack) WebsocketReader() {
}
var data WebsocketResponse
err = common.JSONDecode(resp, &data)
err = json.Unmarshal(resp, &data)
if err != nil {
log.Errorln(log.CommunicationMgr, err)
continue
@@ -253,7 +253,7 @@ func (s *Slack) WebsocketReader() {
func (s *Slack) handlePresenceChange(resp []byte) error {
var pres PresenceChange
err := common.JSONDecode(resp, &pres)
err := json.Unmarshal(resp, &pres)
if err != nil {
return err
}
@@ -270,7 +270,7 @@ func (s *Slack) handleMessageResponse(resp []byte, data WebsocketResponse) error
return errors.New("reply to is != 0")
}
var msg Message
err := common.JSONDecode(resp, &msg)
err := json.Unmarshal(resp, &msg)
if err != nil {
return err
}
@@ -318,7 +318,7 @@ func (s *Slack) handleReconnectResponse(resp []byte) error {
URL string `json:"url"`
}
var recURL reconnectResponse
err := common.JSONDecode(resp, &recURL)
err := json.Unmarshal(resp, &recURL)
if err != nil {
return err
}

View File

@@ -1,9 +1,9 @@
package slack
import (
"encoding/json"
"testing"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/communications/base"
"github.com/thrasher-corp/gocryptotrader/config"
)
@@ -198,7 +198,7 @@ func TestHandlePresenceChange(t *testing.T) {
t.Error("slack handlePresenceChange(), unmarshalled malformed json")
}
data, _ := common.JSONEncode(pres)
data, _ := json.Marshal(pres)
err = s.handlePresenceChange(data)
if err != nil {
t.Errorf("slack handlePresenceChange() Error: %s", err)
@@ -225,7 +225,7 @@ func TestHandleMessageResponse(t *testing.T) {
var msg Message
msg.User = "1337"
msg.Text = "Hello World!"
resp, _ := common.JSONEncode(msg)
resp, _ := json.Marshal(msg)
err = s.handleMessageResponse(resp, data)
if err != nil {
@@ -233,7 +233,7 @@ func TestHandleMessageResponse(t *testing.T) {
}
msg.Text = "!notacommand"
resp, _ = common.JSONEncode(msg)
resp, _ = json.Marshal(msg)
err = s.handleMessageResponse(resp, data)
if err == nil {
@@ -270,7 +270,7 @@ func TestHandleReconnectResponse(t *testing.T) {
}
testURL.URL = "https://www.thrasher.io"
data, _ := common.JSONEncode(testURL)
data, _ := json.Marshal(testURL)
err = s.handleReconnectResponse(data)
if err != nil || s.ReconnectURL != "https://www.thrasher.io" {

View File

@@ -5,6 +5,7 @@ package telegram
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
@@ -219,7 +220,7 @@ func (t *Telegram) SendMessage(text string, chatID int64) error {
text,
}
json, err := common.JSONEncode(&messageToSend)
json, err := json.Marshal(&messageToSend)
if err != nil {
return err
}
@@ -241,14 +242,17 @@ func (t *Telegram) SendMessage(text string, chatID int64) error {
}
// SendHTTPRequest sends an authenticated HTTP request
func (t *Telegram) SendHTTPRequest(path string, json []byte, result interface{}) error {
func (t *Telegram) SendHTTPRequest(path string, data []byte, result interface{}) error {
headers := make(map[string]string)
headers["content-type"] = "application/json"
resp, err := common.SendHTTPRequest(http.MethodPost, path, headers, bytes.NewBuffer(json))
resp, err := common.SendHTTPRequest(http.MethodPost,
path,
headers,
bytes.NewBuffer(data))
if err != nil {
return err
}
return common.JSONDecode([]byte(resp), result)
return json.Unmarshal([]byte(resp), result)
}