/
httprequester.go
72 lines (57 loc) · 1.53 KB
/
httprequester.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
package yoti
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
)
const (
// HTTPMethodPost Post HTTP method
HTTPMethodPost = "POST"
// HTTPMethodGet Get HTTP method
HTTPMethodGet = "GET"
// HTTPMethodPut Put HTTP method
HTTPMethodPut = "PUT"
// HTTPMethodPatch Patch HTTP method
HTTPMethodPatch = "PATCH"
)
type httpResponse struct {
Success bool
StatusCode int
Content string
}
type httpRequester func(uri string, headers map[string]string, httpRequestMethod string, contentBytes []byte) (result *httpResponse, err error)
func doRequest(uri string, headers map[string]string, httpRequestMethod string, contentBytes []byte) (result *httpResponse, err error) {
client := &http.Client{}
supportedHTTPMethods := map[string]bool{"GET": true, "POST": true, "PUT": true, "PATCH": true}
if !supportedHTTPMethods[httpRequestMethod] {
err = fmt.Errorf("HTTP Method: '%s' is unsupported", httpRequestMethod)
return
}
var req *http.Request
if req, err = http.NewRequest(
httpRequestMethod,
uri,
bytes.NewBuffer(contentBytes)); err != nil {
return
}
for key, value := range headers {
req.Header.Add(key, value)
}
var resp *http.Response
resp, err = client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
var responseBody []byte
if responseBody, err = ioutil.ReadAll(resp.Body); err != nil {
log.Printf("Unable to read the HTTP response, error: %s", err)
}
result = &httpResponse{
Success: resp.StatusCode < 300,
StatusCode: resp.StatusCode,
Content: string(responseBody)}
return
}