-
Notifications
You must be signed in to change notification settings - Fork 1
/
group.go
70 lines (60 loc) · 1.49 KB
/
group.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
package rest
import (
"bytes"
"encoding/json"
"net/http"
)
// Group is a container for the minimal group summary
type Group struct {
ID string `json:"id"`
Name string `json:"name"`
}
// GroupCreateRequest is the container for the request to create a new group
type GroupCreateRequest struct {
Name string `json:"name"`
}
// GroupCreate requests for a new group to be created with the given
// name
func (host Host) GroupCreate(name string) error {
uri := host.uri + rootAPISubPath + groupSubPath
groupReq := &GroupCreateRequest{
Name: name,
}
body, err := json.Marshal(groupReq)
if err != nil {
return err
}
req, err := http.NewRequest("POST", uri, bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return DecodeOCError(resp)
}
// GroupAll fetches a list of all groups
func (host Host) GroupAll() ([]Group, error) {
var groups []Group
uri := host.uri + rootAPISubPath + groupSubPath
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return groups, err
}
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
return groups, err
}
defer resp.Body.Close()
if err := DecodeOCError(resp); err != nil {
return groups, err
}
err = json.NewDecoder(resp.Body).Decode(&groups)
return groups, err
}