-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomldb.go
67 lines (58 loc) · 1.74 KB
/
gomldb.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
package gomldb
import (
"net/http"
"bytes"
"strings"
"errors"
"net/url"
)
type Connection struct {
uri string
}
func (conn *Connection) Connect(host string) error{
if strings.HasPrefix(host,"http") {
conn.uri = strings.TrimSuffix(host,"/")
return nil
} else {
return errors.New("URIs must start with 'http'")
}
}
func (conn *Connection) Get(url string, json []byte) (*http.Response, error) {
req, err := http.NewRequest("GET", conn.uri+url, bytes.NewBuffer(json))
if err != nil {
return nil,err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
return client.Do(req)
}
func (conn *Connection) Post(url string, json []byte) (*http.Response, error) {
req, err := http.NewRequest("POST", conn.uri+url, bytes.NewBuffer(json))
if err != nil {
return nil,err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
return client.Do(req)
}
func (conn *Connection) Put(url string, json []byte) (*http.Response, error) {
req, err := http.NewRequest("PUT", conn.uri+url, bytes.NewBuffer(json))
if err != nil {
return nil,err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
return client.Do(req)
}
func (conn *Connection) Delete(url string) (*http.Response, error) {
req, err := http.NewRequest("DELETE", conn.uri+url, nil)
if err != nil {
return nil,err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
return client.Do(req)
}
func (conn *Connection) Query(sql string) (*http.Response, error) {
return http.Get(conn.uri+"/v1/query?"+url.Values{"q":{sql},"format":{"table"}}.Encode())
}