In the common package added JSONDecode error check. Added verbosity in SendHTTPGetRequest. Updated Nonce package function. Fixed issues in ItBit package and expanded test coverage.

This commit is contained in:
Ryan O'Hara-Reid
2017-09-12 08:37:18 +10:00
parent 939ce3ab21
commit e8c7bf9af4
24 changed files with 601 additions and 241 deletions

View File

@@ -20,6 +20,7 @@ import (
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"strconv"
"strings"
@@ -294,16 +295,24 @@ func SendHTTPRequest(method, path string, headers map[string]string, body io.Rea
// SendHTTPGetRequest sends a simple get request using a url string & JSON
// decodes the response into a struct pointer you have supplied. Returns an error
// on failure.
func SendHTTPGetRequest(url string, jsonDecode bool, result interface{}) error {
func SendHTTPGetRequest(url string, jsonDecode, isVerbose bool, result interface{}) error {
if isVerbose {
log.Println("Raw URL: ", url)
}
res, err := http.Get(url)
if err != nil {
return err
}
if res.StatusCode != 200 {
<<<<<<< dae90a2eaa109648bdb85f8298d805e00ad4e974
log.Printf("HTTP status code: %d\n", res.StatusCode)
log.Printf("URL: %s\n", url)
return errors.New("status code was not 200")
=======
return fmt.Errorf("common.SendHTTPGetRequest() error: HTTP status code %d", res.StatusCode)
>>>>>>> In the common package added JSONDecode error check. Added verbosity in SendHTTPGetRequest. Updated Nonce package function. Fixed issues in ItBit package and expanded test coverage.
}
contents, err := ioutil.ReadAll(res.Body)
@@ -311,16 +320,18 @@ func SendHTTPGetRequest(url string, jsonDecode bool, result interface{}) error {
return err
}
if isVerbose {
log.Println("Raw Resp: ", string(contents[:]))
}
defer res.Body.Close()
if jsonDecode {
err := JSONDecode(contents, &result)
err := JSONDecode(contents, result)
if err != nil {
log.Println(string(contents[:]))
return err
}
} else {
result = &contents
}
return nil
@@ -333,6 +344,9 @@ func JSONEncode(v interface{}) ([]byte, error) {
// JSONDecode decodes JSON data into a structure
func JSONDecode(data []byte, to interface{}) error {
if !StringContains(reflect.ValueOf(to).Type().String(), "*") {
return errors.New("json decode error - memory address not supplied")
}
return json.Unmarshal(data, to)
}

View File

@@ -488,15 +488,15 @@ func TestSendHTTPGetRequest(t *testing.T) {
url := `https://etherchain.org/api/account/multiple/0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe`
result := test{}
err := SendHTTPGetRequest(url, true, &result)
err := SendHTTPGetRequest(url, true, false, &result)
if err != nil {
t.Errorf("Test failed - common SendHTTPGetRequest error: %s", err)
}
err = SendHTTPGetRequest("DINGDONG", true, &result)
err = SendHTTPGetRequest("DINGDONG", true, false, &result)
if err == nil {
t.Error("Test failed - common SendHTTPGetRequest error")
}
err = SendHTTPGetRequest(url, false, &result)
err = SendHTTPGetRequest(url, false, false, &result)
if err != nil {
t.Error("Test failed - common SendHTTPGetRequest error")
}