-
Notifications
You must be signed in to change notification settings - Fork 40
/
request.go
49 lines (42 loc) · 1.11 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package pusher
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
const (
contentTypeHeaderKey = "Content-Type"
contentTypeHeaderValue = "application/json"
)
var headers = map[string]string{
"Content-Type": "application/json",
"X-Pusher-Library": fmt.Sprintf("%s %s", libraryName, libraryVersion),
}
// 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))
for key, val := range headers {
req.Header.Set(http.CanonicalHeaderKey(key), val)
}
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
}