Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit d67463c

Browse files
committed
improve update functionality
* add UpdateAnnotation test * add PatchAnnotation method
1 parent 4fdbeb5 commit d67463c

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

annotation.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,36 +124,68 @@ func (c *Client) NewGraphiteAnnotation(gfa *GraphiteAnnotation) (int64, error) {
124124
return result.ID, err
125125
}
126126

127-
// UpdateAnnotation updates an existing annotation with the Annotation it is passed
128-
func (c *Client) UpdateAnnotation(a *Annotation) (int64, error) {
129-
path := fmt.Sprintf("/api/annotations/%d", a.ID)
127+
// UpdateAnnotation updates all properties an existing annotation with the Annotation it is passed.
128+
func (c *Client) UpdateAnnotation(id int64, a *Annotation) (string, error) {
129+
path := fmt.Sprintf("/api/annotations/%d", id)
130130
data, err := json.Marshal(a)
131131
if err != nil {
132-
return 0, err
132+
return "", err
133133
}
134134
req, err := c.newRequest("PUT", path, nil, bytes.NewBuffer(data))
135135
if err != nil {
136-
return 0, err
136+
return "", err
137137
}
138138

139139
resp, err := c.Do(req)
140140
if err != nil {
141-
return 0, err
141+
return "", err
142142
}
143143
if resp.StatusCode != 200 {
144-
return 0, errors.New(resp.Status)
144+
return "", errors.New(resp.Status)
145145
}
146146

147147
data, err = ioutil.ReadAll(resp.Body)
148148
if err != nil {
149-
return 0, err
149+
return "", err
150150
}
151151

152152
result := struct {
153-
ID int64 `json:"id"`
153+
Message string `json:"message"`
154154
}{}
155155
err = json.Unmarshal(data, &result)
156-
return result.ID, err
156+
return result.Message, err
157+
}
158+
159+
// PatchAnnotation updates one or more properties of an existing annotation that matches the specified ID.
160+
func (c *Client) PatchAnnotation(id int64, a *Annotation) (string, error) {
161+
path := fmt.Sprintf("/api/annotations/%d", id)
162+
data, err := json.Marshal(a)
163+
if err != nil {
164+
return "", err
165+
}
166+
req, err := c.newRequest("PATCH", path, nil, bytes.NewBuffer(data))
167+
if err != nil {
168+
return "", err
169+
}
170+
171+
resp, err := c.Do(req)
172+
if err != nil {
173+
return "", err
174+
}
175+
if resp.StatusCode != 200 {
176+
return "", errors.New(resp.Status)
177+
}
178+
179+
data, err = ioutil.ReadAll(resp.Body)
180+
if err != nil {
181+
return "", err
182+
}
183+
184+
result := struct {
185+
Message string `json:"message"`
186+
}{}
187+
err = json.Unmarshal(data, &result)
188+
return result.Message, err
157189
}
158190

159191
// DeleteAnnotation deletes the annotation of the ID it is passed

annotation_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ const (
4040
"id": 1
4141
}`
4242

43+
updateAnnotationJSON = `{"message":"Annotation updated"}`
44+
45+
patchAnnotationJSON = `{"message":"Annotation patched"}`
46+
4347
deleteAnnotationJSON = `{"message":"Annotation deleted"}`
4448
)
4549

@@ -89,6 +93,44 @@ func TestNewAnnotation(t *testing.T) {
8993
}
9094
}
9195

96+
func TestUpdateAnnotation(t *testing.T) {
97+
server, client := gapiTestTools(200, updateAnnotationJSON)
98+
defer server.Close()
99+
100+
a := Annotation{
101+
Text: "new text description",
102+
}
103+
res, err := client.UpdateAnnotation(1, &a)
104+
if err != nil {
105+
t.Error(err)
106+
}
107+
108+
t.Log(pretty.PrettyFormat(res))
109+
110+
if res != "Annotation updated" {
111+
t.Error("update annotation response should contain the correct response message")
112+
}
113+
}
114+
115+
func TestPatchAnnotation(t *testing.T) {
116+
server, client := gapiTestTools(200, patchAnnotationJSON)
117+
defer server.Close()
118+
119+
a := Annotation{
120+
Text: "new text description",
121+
}
122+
res, err := client.PatchAnnotation(1, &a)
123+
if err != nil {
124+
t.Error(err)
125+
}
126+
127+
t.Log(pretty.PrettyFormat(res))
128+
129+
if res != "Annotation patched" {
130+
t.Error("patch annotation response should contain the correct response message")
131+
}
132+
}
133+
92134
func TestNewGraphiteAnnotation(t *testing.T) {
93135
server, client := gapiTestTools(200, newGraphiteAnnotationJSON)
94136
defer server.Close()

0 commit comments

Comments
 (0)