This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fluidinfo.go
316 lines (286 loc) · 8.24 KB
/
fluidinfo.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package fluidinfo
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strconv"
"strings"
)
type readClose struct {
io.Reader
io.Closer
}
type badStringError struct {
what string
str string
}
func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }
// Given a string of the form "host", "host:port", or "[ipv6::address]:port",
// return true if the string includes a port.
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
func send(req *http.Request) (resp *http.Response, err error) {
addr := req.URL.Host
if !hasPort(addr) {
addr += ":http"
}
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
err = req.Write(conn)
if err != nil {
conn.Close()
return nil, err
}
reader := bufio.NewReader(conn)
resp, err = http.ReadResponse(reader, req)
if err != nil {
conn.Close()
return nil, err
}
r := io.Reader(reader)
if v := resp.Header["Content-Length"]; v != nil {
n, err := strconv.Atoi(v[0])
if err != nil {
return nil, &badStringError{"invalid Content-Length", v[0]}
}
v := int64(n)
r = io.LimitReader(r, v)
}
resp.Body = readClose{r, conn}
return
}
func encodedUsernameAndPassword(user, pwd string) string {
bb := &bytes.Buffer{}
encoder := base64.NewEncoder(base64.StdEncoding, bb)
encoder.Write([]byte(user + ":" + pwd))
encoder.Close()
return bb.String()
}
func authGet(url_, user, pwd string) (r *http.Response, err error) {
var req http.Request
req.Method = "GET"
req.Header = map[string][]string{"Authorization": {"Basic " +
encodedUsernameAndPassword(user, pwd)}}
if req.URL, err = url.Parse(url_); err != nil {
return
}
if r, err = send(&req); err != nil {
return
}
return
}
// Post issues a POST to the specified URL.
// Caller should close r.Body when done reading it.
func authPost(url_, user, pwd, client, clientURL, version, agent, bodyType string,
body io.Reader) (r *http.Response, err error) {
var req http.Request
req.Method = "POST"
req.Body = body.(io.ReadCloser)
req.Header = map[string][]string{
"Content-Type": {bodyType},
"Transfer-Encoding": {"chunked"},
"User-Agent": {agent},
"X-Fluidinfo-Client": {client},
"X-Fluidinfo-Client-URL": {clientURL},
"X-Fluidinfo-Version": {version},
"Authorization": {"Basic " + encodedUsernameAndPassword(user, pwd)},
}
req.URL, err = url.Parse(url_)
if err != nil {
return nil, err
}
return send(&req)
}
// Put issues a PUT to the specified URL.
// Caller should close r.Body when done reading it.
func authPut(url_, user, pwd, client, clientURL, version, agent, bodyType string,
body io.Reader) (r *http.Response, err error) {
var req http.Request
req.Method = "PUT"
req.Body = body.(io.ReadCloser)
if user != "" && pwd != "" {
req.Header = map[string][]string{
"Content-Type": {bodyType},
"Transfer-Encoding": {"chunked"},
"User-Agent": {agent},
"X-Fluidinfo-Client": {client},
"X-Fluidinfo-Client-URL": {clientURL},
"X-Fluidinfo-Version": {version},
"Authorization": {"Basic " + encodedUsernameAndPassword(user, pwd)},
}
} else {
req.Header = map[string][]string{
"Content-Type": {bodyType},
"Transfer-Encoding": {"chunked"},
"User-Agent": {agent},
"X-Fluidinfo-Client": {client},
"X-Fluidinfo-Client-URL": {clientURL},
"X-Fluidinfo-Version": {version},
}
}
req.URL, err = url.Parse(url_)
if err != nil {
return nil, err
}
return send(&req)
}
// Delete issues a DELETE to the specified URL.
func authDelete(url_, user, pwd string) (r *http.Response, err error) {
var req http.Request
req.Method = "DELETE"
if user != "" && pwd != "" {
req.Header = map[string][]string{"Authorization": {"Basic " +
encodedUsernameAndPassword(user, pwd)}}
}
if req.URL, err = url.Parse(url_); err != nil {
return
}
if r, err = send(&req); err != nil {
return
}
return
}
// Head issues a HEAD to the specified URL.
func authHead(url_, user, pwd string) (r *http.Response, err error) {
var req http.Request
req.Method = "HEAD"
if user != "" && pwd != "" {
req.Header = map[string][]string{"Authorization": {"Basic " +
encodedUsernameAndPassword(user, pwd)}}
}
if req.URL, err = url.Parse(url_); err != nil {
return
}
if r, err = send(&req); err != nil {
return
}
return
}
// Do an authenticated Get if we've called Authenticated, otherwise
// just Get it without authentication.
func httpGet(url_, user, pwd string) (*http.Response, string, error) {
var r *http.Response
var full string = ""
var err error
if user != "" && pwd != "" {
r, err = authGet(url_, user, pwd)
} else {
r, err = http.Get(url_)
}
return r, full, err
}
// Do an authenticated Post if we've called Authenticated, otherwise
// just Post it without authentication.
func httpPost(url_, user, pwd, client, clientURL, version, agent,
data string) (*http.Response, error) {
var r *http.Response
var err error
body := bytes.NewBufferString(data)
bodyType := "application/json"
if user != "" && pwd != "" {
r, err = authPost(url_, user, pwd, client, clientURL,
version, agent, bodyType, body)
} else {
r, err = http.Post(url_, bodyType, body)
}
return r, err
}
// Do an authenticated Put.
func httpPut(url_, user, pwd, client, clientURL, version, agent,
data string) (*http.Response, error) {
var r *http.Response
var err error
body := bytes.NewBufferString(data)
bodyType := "application/json"
r, err = authPut(url_, user, pwd, client, clientURL,
version, agent, bodyType, body)
return r, err
}
// Do an authenticated Delete.
func httpDelete(url_, user, pwd string) (*http.Response, error) {
var r *http.Response
var err error
r, err = authDelete(url_, user, pwd)
return r, err
}
// Do an authenticated Head.
func httpHead(url_, user, pwd string) (*http.Response, error) {
var r *http.Response
var err error
r, err = authHead(url_, user, pwd)
return r, err
}
const (
DEFAULT_CLIENT = "gofluidinfo"
DEFAULT_CLIENT_URL = "http://github.com/micrypt/gofluidinfo"
DEFAULT_CLIENT_VERSION = "0.1"
DEFAULT_USER_AGENT = "gofluidinfo"
ERROR = "gofluidinfo Error: "
WARNING = "gofluidinfo Warning: "
DEFAULT_PORT = 80
SECURE_PORT = 443
UNIX_CREDENTIALS_FILE = ".fluidinfocredentials"
WINDOWS_CREDENTIALS_FILE = "fluidinfocredentials.ini"
RETRY_TIMEOUT = 5e9
PRIMITIVE_CONTENT_TYPE = "application/vnd.fluidinfo.value+json"
HEADER_ERROR = "X-Fluidinfo-Error-Class"
HEADER_REQUEST_ID = "X-Fluidinfo-Request-Id"
FLUIDINFO_PATH = "http://fluiddb.fluidinfo.com"
SANDBOX_PATH = "http://sandbox.fluidinfo.com"
)
type Client struct {
Username string
Password string
URL string
Client string
ClientURL string
Version string
Agent string
}
func NewClient(username, password string) *Client {
return &Client{username, password, SANDBOX_PATH, DEFAULT_CLIENT, DEFAULT_CLIENT_URL, DEFAULT_CLIENT_VERSION, DEFAULT_USER_AGENT}
}
func (self *Client) SetActiveMode() {
self.URL = FLUIDINFO_PATH
}
func (self *Client) Get(url_ string) (*http.Response, error) {
url_ = self.URL + url_
var resp *http.Response
var err error
resp, _, err = httpGet(url_, self.Username, self.Password)
return resp, err
}
func (self *Client) Post(url_, data string) (*http.Response, error) {
url_ = self.URL + url_
var resp *http.Response
var err error
resp, err = httpPost(url_, self.Username, self.Password, self.Client, self.ClientURL, self.Version, self.Agent, data)
return resp, err
}
func (self *Client) Put(url_, data string) (*http.Response, error) {
url_ = self.URL + url_
var resp *http.Response
var err error
resp, err = httpPut(url_, self.Username, self.Password, self.Client, self.ClientURL, self.Version, self.Agent, data)
return resp, err
}
func (self *Client) Delete(url_ string) (*http.Response, error) {
url_ = self.URL + url_
var resp *http.Response
var err error
resp, err = httpDelete(url_, self.Username, self.Password)
return resp, err
}
func (self *Client) Head(url_ string) (*http.Response, error) {
url_ = self.URL + url_
var resp *http.Response
var err error
resp, err = httpHead(url_, self.Username, self.Password)
return resp, err
}