-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
99 lines (92 loc) · 2.17 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
package annotate
import (
"bytes"
"encoding/json"
"net/http"
"time"
)
type Client struct {
apiRoot string
client *http.Client
}
func NewClient(apiRoot string) Client {
return Client{
apiRoot: apiRoot,
client: &http.Client{},
}
}
// SendAnnotation sends a annotation to an annotations server
// apiRoot should be "http://foo/api" where the annotation routes
// would be available at http://foo/api/annotation...
func (c *Client) SendAnnotation(a Annotation) (Annotation, error) {
b, err := json.Marshal(a)
if err != nil {
return a, err
}
res, err := c.client.Post(c.apiRoot+"/annotation", "application/json", bytes.NewReader(b))
if err != nil {
return a, err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&a)
return a, err
}
// GetAnnotation gets an annotation by ID, and will return
// nil without an error if the annotation does not exist
func (c *Client) GetAnnotation(id string) (*Annotation, error) {
a := &Annotation{}
res, err := c.client.Get(c.apiRoot + "/annotation/" + id)
if res != nil && res.StatusCode == http.StatusNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&a)
return a, err
}
func (c *Client) GetAnnotations(start, end *time.Time, source, host, creationUser, owner, category, url, message string) (Annotations, error) {
a := Annotations{}
req, err := http.NewRequest("GET", c.apiRoot+"/annotation/query", nil)
if err != nil {
return a, nil
}
q := req.URL.Query()
if start != nil {
q.Add(StartDate, start.Format(time.RFC3339))
}
if end != nil {
q.Add(EndDate, end.Format(time.RFC3339))
}
if source != "" {
q.Add(Source, source)
}
if host != "" {
q.Add(Host, host)
}
if creationUser != "" {
q.Add(CreationUser, creationUser)
}
if owner != "" {
q.Add(Owner, owner)
}
if category != "" {
q.Add(Category, category)
}
if url != "" {
q.Add(Url, url)
}
if message != "" {
q.Add(Message, message)
}
req.URL.RawQuery = q.Encode()
req.Header.Add("Accept", "application/json")
res, err := c.client.Do(req)
if err != nil {
return a, err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&a)
return a, err
}