forked from devopsbq/corbel-go
/
http.go
151 lines (127 loc) · 3.96 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
150
151
package corbel
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"github.com/google/go-querystring/query"
)
// NewRequestContentType creates an API request using 'application/json', most common api query.
// method is the HTTP method to use
// endpoint is the endpoint of SR to speak with
// urlStr is the url to query. it must be preceded by a slash.
// headerContentType is the header['Content-Type'] of the request.
// headerAccept is the header['Accept'] of the request.
// body is, if specified, the value JSON encoded to be used as request body.
func (c *Client) NewRequestContentType(method, endpoint, urlStr, headerContentType, headerAccept string, body interface{}) (*http.Request, error) {
u, _ := url.Parse(c.URLFor(endpoint, urlStr))
buf := new(bytes.Buffer)
if body != nil {
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", headerContentType)
req.Header.Add("Accept", headerAccept)
req.Header.Add("User-Agent", c.UserAgent)
if c.CurrentToken != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token()))
}
// fmt.Println(u.String()) // for debug
// fmt.Println(buf.String()) // for debug
// fmt.Println("CurrentToken", c.CurrentToken) // for debug
return req, nil
}
// NewRequest creates an API request using 'application/json', most common api query.
// method is the HTTP method to use
// endpoint is the endpoint of SR to speak with
// url is the url to query. it must be preceded by a slash.
// body is, if specified, the value JSON encoded to be used as request body.
func (c *Client) NewRequest(method, endpoint, urlStr string, body interface{}) (*http.Request, error) {
return c.NewRequestContentType(method, endpoint, urlStr, "application/json", "application/json", body)
}
func returnErrorHTTPInterface(client *Client, req *http.Request, err error, object interface{}, desiredStatusCode int) (string, error) {
var (
res *http.Response
objectByte []byte
)
if err != nil {
return "", err
}
res, err = client.httpClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
objectByte, err = ioutil.ReadAll(res.Body)
if err != nil {
return "", errResponseError
}
err = json.Unmarshal(objectByte, &object)
if err != nil {
return "", errJSONUnmarshalError
}
// fmt.Println(string(objectByte)) // for debug
return returnErrorByHTTPStatusCode(res, desiredStatusCode)
}
func returnErrorHTTPSimple(client *Client, req *http.Request, err error, desiredStatusCode int) (string, error) {
var (
res *http.Response
)
if err != nil {
return "", err
}
res, err = client.httpClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
// objectByte, _ := ioutil.ReadAll(res.Body) // for debug
// fmt.Println(string(objectByte)) // for debug
return returnErrorByHTTPStatusCode(res, desiredStatusCode)
}
// returnErrorByHTTPStatusCode returns the http error code or nil if it returns the
// desired error
func returnErrorByHTTPStatusCode(res *http.Response, desiredStatusCode int) (string, error) {
var (
location *url.URL
locationString string
)
location, _ = res.Location()
if location == nil {
locationString = ""
} else {
locationString = location.String()
}
if res.StatusCode == desiredStatusCode {
return locationString, nil
}
if http.StatusText(res.StatusCode) == "" {
return "", fmt.Errorf("HTTP Error %d", res.StatusCode)
}
return locationString, fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode))
}
func addOptions(s string, opt interface{}) (string, error) {
v := reflect.ValueOf(opt)
if v.Kind() == reflect.Ptr && v.IsNil() {
return s, nil
}
u, err := url.Parse(s)
if err != nil {
return s, err
}
qv, err := query.Values(opt)
if err != nil {
return s, err
}
u.RawQuery = qv.Encode()
return u.String(), nil
}