-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
190 lines (152 loc) · 4.53 KB
/
client.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
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
package confclient
import (
"bytes"
"encoding/json"
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"strings"
)
type Client struct {
url string
httpClient *http.Client
scopeVars map[string]string
TemplateDir string
ConfigDir string
}
func InitiateClient(url string) *Client {
client := &Client{
url: url,
httpClient: &http.Client{},
scopeVars: make(map[string]string),
}
// Get all environment variables beginning with CFG_
// And store them in the scopeVars map
allEnvVars := os.Environ()
for _, e := range allEnvVars {
index := strings.Index(e, "=")
varname := strings.ToLower(e[:index])
if strings.HasPrefix(varname, "cfg_") {
scopevar := strings.TrimPrefix(varname, "cfg_")
log.WithFields(log.Fields{scopevar: e[index+1:]}).Info("Using scope")
client.scopeVars[scopevar] = e[index+1:]
}
}
return client
}
func (c *Client) GetList(key string) (ListResponse, error) {
var resp ListResponse
body, err := c.GETRequestJSON("/list/" + key)
if err != nil {
return resp, err
}
if err = json.Unmarshal(body, &resp); err != nil {
return resp, err
}
return resp, err
}
func (c *Client) GetHash(key string) (HashResponse, error) {
var resp HashResponse
body, err := c.GETRequestJSON("/hash/" + key)
if err != nil {
return resp, err
}
if err = json.Unmarshal(body, &resp); err != nil {
return resp, err
}
return resp, err
}
func (c *Client) GetString(key string) (StringResponse, error) {
var resp StringResponse
body, err := c.GETRequestJSON("/string/" + key)
if err != nil {
return resp, err
}
if err = json.Unmarshal(body, &resp); err != nil {
return resp, err
}
return resp, err
}
func (c *Client) GETRequestTEXT(path string) ([]byte, error) {
return c.GETRequest(path, "text/plain")
}
func (c *Client) GETRequestJSON(path string) ([]byte, error) {
return c.GETRequest(path, "application/json")
}
func (c *Client) GETRequest(path string, accept string) ([]byte, error) {
req_url := strings.Join([]string{c.url, path}, "")
req, err := http.NewRequest("GET", req_url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", accept)
// Send scope variables as x-cfg-blah request headers
for scopeKey, scopeVal := range c.scopeVars {
req.Header.Add("x-cfg-"+scopeKey, scopeVal)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
l := log.WithFields(log.Fields{"url": req_url, "httpcode": resp.StatusCode, "method": "GET"})
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
l.Warn("HTTP error")
return nil, errors.New(fmt.Sprintf("HTTP Error %d", resp.StatusCode))
}
l.Debug("HTTP log")
return ioutil.ReadAll(resp.Body)
}
func (c *Client) PATCHRequestJSON(path string, data []byte) ([]byte, error) {
req_url := strings.Join([]string{c.url, path}, "")
req, err := http.NewRequest("PATCH", req_url, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
l := log.WithFields(log.Fields{"url": req_url, "httpcode": resp.StatusCode, "method": "POST"})
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
l.Warn("HTTP error")
return nil, errors.New(fmt.Sprintf("HTTP Error %d", resp.StatusCode))
}
l.Debug("HTTP log")
return ioutil.ReadAll(resp.Body)
}
func (c *Client) POSTRequestJSON(path string, data []byte) ([]byte, error) {
req_url := strings.Join([]string{c.url, path}, "")
req, err := http.NewRequest("POST", req_url, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
l := log.WithFields(log.Fields{"url": req_url, "httpcode": resp.StatusCode, "method": "POST"})
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
l.Warn("HTTP error")
return nil, errors.New(fmt.Sprintf("HTTP Error %d", resp.StatusCode))
}
l.Debug("HTTP log")
return ioutil.ReadAll(resp.Body)
}
func (c *Client) DELETERequest(path string) ([]byte, error) {
req_url := strings.Join([]string{c.url, path}, "")
req, err := http.NewRequest("DELETE", req_url, nil)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
l := log.WithFields(log.Fields{"url": req_url, "httpcode": resp.StatusCode, "method": "DELETE"})
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
l.Warn("HTTP error")
return nil, errors.New(fmt.Sprintf("HTTP Error %d", resp.StatusCode))
}
l.Debug("HTTP log")
return ioutil.ReadAll(resp.Body)
}