forked from Virtomize/mailtrain-go-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
49 lines (39 loc) · 1.1 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 gomailtrain
import (
"fmt"
"io/ioutil"
"net/http"
)
// Request implementation
func (a *API) Request(req *http.Request) ([]byte, error) {
req.Header.Add("Accept", "application/json, */*")
a.Auth(req)
Debug("Request:")
Debug(req)
resp, err := a.client.Do(req)
if err != nil {
return nil, err
}
Debug(fmt.Sprintf("Status Code: %d", resp.StatusCode))
res, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Debug("Response Body:")
Debug(string(res))
switch resp.StatusCode {
case http.StatusOK, http.StatusCreated, http.StatusPartialContent:
return res, nil
case http.StatusNoContent, http.StatusResetContent:
return nil, nil
case http.StatusUnauthorized:
return nil, fmt.Errorf("authentication failed")
case http.StatusServiceUnavailable:
return nil, fmt.Errorf("service is not available: %s", resp.Status)
case http.StatusInternalServerError:
return nil, fmt.Errorf("internal server error: %s", resp.Status)
case http.StatusConflict:
return nil, fmt.Errorf("conflict: %s", resp.Status)
}
return nil, fmt.Errorf("unknown response status: %s", resp.Status)
}