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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
// Package http defines higher level helpers for the net/http package
package http
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/eduvpn/eduvpn-common/types"
)
// The URLParemeters as the name suggests is a type used for the parameters in the URL.
type URLParameters map[string]string
// OptionalParams is a structure that defines the optional parameters that are given when making a HTTP call.
type OptionalParams struct {
Headers http.Header
URLParameters URLParameters
Body url.Values
Timeout time.Duration
}
// ConstructURL creates a URL with the included parameters.
func ConstructURL(baseURL string, parameters URLParameters) (string, error) {
url, parseErr := url.Parse(baseURL)
if parseErr != nil {
return "", types.NewWrappedError(
fmt.Sprintf(
"failed to construct url: %s including parameters: %v",
url,
parameters,
),
parseErr,
)
}
q := url.Query()
for parameter, value := range parameters {
q.Set(parameter, value)
}
url.RawQuery = q.Encode()
return url.String(), nil
}
// Get creates a Get request and returns the headers, body and an error.
func Get(url string) (http.Header, []byte, error) {
return MethodWithOpts(http.MethodGet, url, nil)
}
// Post creates a Post request and returns the headers, body and an error.
func Post(url string, body url.Values) (http.Header, []byte, error) {
return MethodWithOpts(http.MethodGet, url, &OptionalParams{Body: body})
}
// GetWithOpts creates a Get request with optional parameters and returns the headers, body and an error.
func GetWithOpts(url string, opts *OptionalParams) (http.Header, []byte, error) {
return MethodWithOpts(http.MethodGet, url, opts)
}
// PostWithOpts creates a Post request with optional parameters and returns the headers, body and an error.
func PostWithOpts(url string, opts *OptionalParams) (http.Header, []byte, error) {
return MethodWithOpts(http.MethodPost, url, opts)
}
// optionalURL ensures that the URL contains the optional parameters
// it returns the url (with parameters if success) and an error indicating success.
func optionalURL(url string, opts *OptionalParams) (string, error) {
if opts != nil {
url, urlErr := ConstructURL(url, opts.URLParameters)
if urlErr != nil {
return url, types.NewWrappedError(
fmt.Sprintf("failed to create HTTP request with url: %s", url),
urlErr,
)
}
return url, nil
}
return url, nil
}
// optionalHeaders ensures that the HTTP request uses the optional headers if defined.
func optionalHeaders(req *http.Request, opts *OptionalParams) {
// Add headers
if opts != nil && req != nil && opts.Headers != nil {
for k, v := range opts.Headers {
req.Header.Add(k, v[0])
}
}
}
// optionalBodyReader returns a HTTP body reader if there is a body, otherwise nil.
func optionalBodyReader(opts *OptionalParams) io.Reader {
if opts != nil && opts.Body != nil {
return strings.NewReader(opts.Body.Encode())
}
return nil
}
// MethodWithOpts creates a HTTP request using a method (e.g. GET, POST), an url and optional parameters
// It returns the HTTP headers, the body and an error if there is one.
func MethodWithOpts(
method string,
url string,
opts *OptionalParams,
) (http.Header, []byte, error) {
// Make sure the url contains all the parameters
// This can return an error,
// it already has the right error so so we don't wrap it further
url, urlErr := optionalURL(url, opts)
if urlErr != nil {
// No further type wrapping is needed here
return nil, nil, urlErr
}
// Default timeout is 5 seconds
// If a different timeout is given, set it
var timeout time.Duration = 5
if opts != nil && opts.Timeout > 0 {
timeout = opts.Timeout
}
// Create a client
client := &http.Client{Timeout: timeout * time.Second}
errorMessage := fmt.Sprintf("failed HTTP request with method %s and url %s", method, url)
// Create request object with the body reader generated from the optional arguments
req, reqErr := http.NewRequest(method, url, optionalBodyReader(opts))
if reqErr != nil {
return nil, nil, types.NewWrappedError(errorMessage, reqErr)
}
// See https://stackoverflow.com/questions/17714494/golang-http-request-results-in-eof-errors-when-making-multiple-requests-successi
req.Close = true
// Make sure the headers contain all the parameters
optionalHeaders(req, opts)
// Do request
resp, respErr := client.Do(req)
if respErr != nil {
return nil, nil, types.NewWrappedError(errorMessage, respErr)
}
// Request successful, make sure body is closed at the end
defer resp.Body.Close()
// Return a string
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return resp.Header, nil, types.NewWrappedError(errorMessage, readErr)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
// We make this a custom error because we want to extract the status code later
statusErr := &StatusError{URL: url, Body: string(body), Status: resp.StatusCode}
return resp.Header, body, types.NewWrappedError(errorMessage, statusErr)
}
// Return the body in bytes and signal the status error if there was one
return resp.Header, body, nil
}
// StatusError indicates that we have received a HTTP status error.
type StatusError struct {
URL string
Body string
Status int
}
// Error returns the StatusError as an error string.
func (e *StatusError) Error() string {
return fmt.Sprintf(
"failed obtaining HTTP resource: %s as it gave an unsuccessful status code: %d. Body: %s",
e.URL,
e.Status,
e.Body,
)
}
// ParseJSONError indicates that the HTTP error is because of failed JSON parsing
// It has the URL and the Body as context.
// The underlying JSON parsing Err itself is also wrapped here.
type ParseJSONError struct {
URL string
Body string
Err error
}
// Error returns the ParseJSONError as an error string.
func (e *ParseJSONError) Error() string {
return fmt.Sprintf(
"failed parsing json %s for HTTP resource: %s with error: %v",
e.Body,
e.URL,
e.Err,
)
}
|