forked from pothtonswer/discordmuffin
Minor changes to Request() func, closes #73
This commit is contained in:
parent
9583ef4e19
commit
3357c56626
1 changed files with 8 additions and 6 deletions
10
restapi.go
10
restapi.go
|
@ -30,10 +30,13 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
|
|||
fmt.Println("API REQUEST PAYLOAD :: [" + fmt.Sprintf("%+v", data) + "]")
|
||||
}
|
||||
|
||||
body, err := json.Marshal(data)
|
||||
var body []byte
|
||||
if data != nil {
|
||||
body, err = json.Marshal(data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
|
@ -59,6 +62,7 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
|
|||
client := &http.Client{Timeout: (20 * time.Second)}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -67,7 +71,6 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
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.
|
||||
|
||||
default: // Error condition
|
||||
err = fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
return
|
||||
err = fmt.Errorf("HTTP %s, %s", resp.Status, response)
|
||||
}
|
||||
|
||||
return
|
||||
|
|
Loading…
Reference in a new issue