-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.go
149 lines (124 loc) · 3.75 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package frappe
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
)
// HTTPClient represents an HTTP client.
type HTTPClient interface {
Do(method, rURL string, params url.Values, headers http.Header) (HTTPResponse, error)
DoRaw(method, rURL string, reqBody []byte, headers http.Header) (HTTPResponse, error)
DoJSON(method, rURL string, params url.Values, headers http.Header, obj interface{}) (HTTPResponse, error)
GetClient() *httpClient
}
// httpClient is the default implementation of HTTPClient.
type httpClient struct {
client *http.Client
hLog *log.Logger
debug bool
}
// HTTPResponse encompasses byte body + the response of an HTTP request.
type HTTPResponse struct {
Body []byte
Response *http.Response
}
// NewHTTPClient returns a self-contained HTTP request object
// with underlying keep-alive transport.
func NewHTTPClient(h *http.Client, hLog *log.Logger, debug bool) HTTPClient {
if hLog == nil {
hLog = log.New(os.Stdout, "base.HTTP: ", log.Ldate|log.Ltime|log.Lshortfile)
}
if h == nil {
h = &http.Client{
Timeout: time.Duration(5) * time.Second,
Transport: &http.Transport{
MaxIdleConnsPerHost: 10,
ResponseHeaderTimeout: time.Second * time.Duration(5),
},
}
}
return &httpClient{
hLog: hLog,
client: h,
debug: debug,
}
}
// Do executes an HTTP request and returns the response.
func (h *httpClient) DoRaw(method, rURL string, reqBody []byte, headers http.Header) (HTTPResponse, error) {
var (
resp = HTTPResponse{}
err error
postBody io.Reader
)
// Encode POST / PUT params.
if method == http.MethodPost || method == http.MethodPut {
postBody = bytes.NewReader(reqBody)
}
req, err := http.NewRequest(method, rURL, postBody)
if err != nil {
h.hLog.Printf("Request preparation failed: %v", err)
return resp, errors.New("request preparation failed")
}
if headers != nil {
req.Header = headers
}
req.Header.Add("Accept", "application/json")
// If a content-type isn't set, set the default one.
if req.Header.Get("Content-Type") == "" {
if method == http.MethodPost || method == http.MethodPut {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}
}
// If the request method is GET or DELETE, add the params as QueryString.
if method == http.MethodGet || method == http.MethodDelete {
req.URL.RawQuery = string(reqBody)
}
r, err := h.client.Do(req)
if err != nil {
h.hLog.Printf("Request failed: %v", err)
return resp, errors.New("request failed")
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
h.hLog.Printf("Unable to read response: %v", err)
return resp, errors.New("error reading response")
}
resp.Response = r
resp.Body = body
if h.debug {
h.hLog.Printf("%s %s -- %d %v", method, req.URL.RequestURI(), resp.Response.StatusCode, req.Header)
}
return resp, nil
}
// DoRaw executes an HTTP request and returns the response.
func (h *httpClient) Do(method, rURL string, params url.Values, headers http.Header) (HTTPResponse, error) {
if params == nil {
params = url.Values{}
}
return h.DoRaw(method, rURL, []byte(params.Encode()), headers)
}
// DoJSON makes an HTTP request and parses the JSON response.
func (h *httpClient) DoJSON(method, url string, params url.Values, headers http.Header, obj interface{}) (HTTPResponse, error) {
resp, err := h.Do(method, url, params, headers)
if err != nil {
return resp, err
}
// We now unmarshal the body.
if err := json.Unmarshal(resp.Body, &obj); err != nil {
h.hLog.Printf("Error parsing JSON response: %v | %s", err, resp.Body)
return resp, errors.New("error parsing response")
}
return resp, nil
}
// GetClient return's the underlying net/http client.
func (h *httpClient) GetClient() *httpClient {
return h
}