-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
148 lines (126 loc) · 4.04 KB
/
httpclient.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
// Package httpclient contains an httpclient type for making REST API calls
package httpclient
import (
"log"
"net/http"
"io/ioutil"
"bytes"
"strings"
)
type HttpClient struct {
BaseUrl string
Headers map[string]string
PrintLogs bool
}
func NewHttpClient(baseUrl string, headers map[string]string, printLogs bool) *HttpClient {
httpClient := new(HttpClient)
httpClient.BaseUrl = baseUrl
httpClient.Headers = headers
httpClient.PrintLogs = printLogs
return httpClient
}
func (c *HttpClient) logSendRequest(url string, requestType string, body string) {
log.Println("Sending [", requestType, "] request to Server (", url, "):")
log.Println("Body:")
log.Println(body)
}
func (c *HttpClient) logResponse(statusCode string, response string) {
log.Println("Received response from Server (", c.BaseUrl, "):")
log.Println("Status: ", statusCode)
log.Println("Response:")
log.Println(response)
}
func genParamString(paramMap map[string]string) string {
if paramMap == nil || len(paramMap) == 0 {
return ""
}
output := "?"
for key, value := range paramMap {
output += key
output += "="
output += value
output += "&"
}
return output
}
func (c *HttpClient) httpRequest(url string, requestType string, headers map[string]string, body string) (string, error) {
var req *http.Request
var err error
if requestType == "GET" {
req, err = http.NewRequest(requestType, url, nil)
} else {
var bodyStr = []byte(body)
req, err = http.NewRequest(requestType, url, bytes.NewBuffer(bodyStr))
}
if err != nil {
return "", err
}
// Default Headers
for key, value := range c.Headers {
req.Header.Set(key, value)
}
// Custom Headers
for key, value := range headers {
req.Header.Set(key, value)
}
if c.PrintLogs {
c.logSendRequest(url, requestType, body)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
response, err := ioutil.ReadAll(resp.Body)
responseString := string(response)
if err != nil {
log.Println("ERROR reading response for URL: ", url)
return "", err
}
if c.PrintLogs {
c.logResponse(resp.Status, responseString)
}
return responseString, nil
}
func (c *HttpClient) Get(url string, queryParamsMap map[string]string, headers map[string]string) (string, error) {
urlString := url + genParamString(queryParamsMap)
resp, err := c.httpRequest(urlString, "GET", headers, "")
if err != nil {
log.Println("Http GET Error for URL: ", urlString)
return "", err
}
return resp, nil
}
func (c *HttpClient) Put(url string, queryParamsMap map[string]string, headers map[string]string, body string) (string, error) {
urlString := url + genParamString(queryParamsMap)
resp, err := c.httpRequest(urlString, "PUT", headers, body)
if err != nil {
log.Println("Http PUT Error for URL: ", urlString, )
return "", err
}
return resp, nil
}
func (c *HttpClient) Post(url string, queryParamsMap map[string]string, headers map[string]string, body string) (string, error) {
urlString := url + genParamString(queryParamsMap)
resp, err := c.httpRequest(urlString, "POST", headers, body)
if err != nil {
log.Println("Http POST Error for URL: ", urlString)
return "", err
}
return resp, nil
}
func (c *HttpClient) Delete(url string, queryParamsMap map[string]string, headers map[string]string, body string) (string, error) {
urlString := url + genParamString(queryParamsMap)
resp, err := c.httpRequest(urlString, "DELETE", headers, body)
if err != nil {
log.Println("Http DELETE Error for URL: ", urlString)
return "", err
}
return resp, nil
}
func (c *HttpClient) MakeUrl(path string, args ...string) string {
url := c.BaseUrl
r := strings.NewReplacer(args...)
return url + r.Replace(path)
}