forked from pusher/pusher-http-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
35 lines (32 loc) · 849 Bytes
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package pusher
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
// change timeout to time.Duration
func request(client *http.Client, method, url string, body []byte) ([]byte, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return processResponse(resp)
}
func processResponse(response *http.Response) ([]byte, error) {
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
if response.StatusCode >= 200 && response.StatusCode < 300 {
return responseBody, nil
}
message := fmt.Sprintf("Status Code: %s - %s", strconv.Itoa(response.StatusCode), string(responseBody))
err = errors.New(message)
return nil, err
}