Minor changes to Request() func, closes #73

This commit is contained in:
Bruce Marriner 2016-01-10 17:30:19 -06:00
parent 9583ef4e19
commit 3357c56626

View file

@ -30,9 +30,12 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
fmt.Println("API REQUEST PAYLOAD :: [" + fmt.Sprintf("%+v", data) + "]") fmt.Println("API REQUEST PAYLOAD :: [" + fmt.Sprintf("%+v", data) + "]")
} }
body, err := json.Marshal(data) var body []byte
if err != nil { if data != nil {
return body, err = json.Marshal(data)
if err != nil {
return
}
} }
req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(body)) req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(body))
@ -59,6 +62,7 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
client := &http.Client{Timeout: (20 * time.Second)} client := &http.Client{Timeout: (20 * time.Second)}
resp, err := client.Do(req) resp, err := client.Do(req)
defer resp.Body.Close()
if err != nil { if err != nil {
return return
} }
@ -67,7 +71,6 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
if err != nil { if err != nil {
return return
} }
resp.Body.Close()
if s.Debug { if s.Debug {
@ -88,8 +91,7 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
// TODO check for 429 response, rate-limit when we get one. // TODO check for 429 response, rate-limit when we get one.
default: // Error condition default: // Error condition
err = fmt.Errorf("HTTP %d", resp.StatusCode) err = fmt.Errorf("HTTP %s, %s", resp.Status, response)
return
} }
return return