-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
75 lines (60 loc) · 1.66 KB
/
http.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cli
import (
"encoding/json"
"fmt"
"github.com/mradile/rssfeeder/pkg/rest"
"github.com/pkg/errors"
"io"
"io/ioutil"
"net/http"
)
func (c *client) makeHTTPRequest(method, url string, data interface{}) (*http.Request, error) {
var reader io.Reader
if data != nil {
var err error
reader, err = toJSONReader(data)
if err != nil {
return nil, errors.Wrap(err, "could not marshal struct to json")
}
}
uri := fmt.Sprintf("%s%s", c.cfg.Hostname, url)
LogDebug(fmt.Sprintf("creating request for %s - %s", method, uri))
req, err := http.NewRequest(method, uri, reader)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}
func (c *client) getResponse(req *http.Request, response interface{}, httpCode int) error {
res, err := Client.http.Do(req)
if err != nil {
return err
}
//noinspection GoUnhandledErrorResult
defer res.Body.Close()
code := res.StatusCode
//if 204 is expected and response is 204 there is no need for reading the body
if httpCode == http.StatusNoContent && code == http.StatusNoContent {
return nil
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if code != httpCode {
var er rest.ErrorResponse
err := json.Unmarshal(data, &er)
if err == nil {
if c.debug {
return fmt.Errorf("http code was [%d], expected [%d]: %s", code, httpCode, er.Message)
}
return fmt.Errorf(er.Message)
}
return fmt.Errorf("http code was [%d], expected [%d]: %s", code, httpCode, string(data))
}
if err := json.Unmarshal(data, response); err != nil {
return fmt.Errorf("could not unmarshal response '%s': %v", string(data), err)
}
return nil
}