-
Notifications
You must be signed in to change notification settings - Fork 7
/
spaces.go
138 lines (108 loc) · 2.96 KB
/
spaces.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
package appoptics
import (
"fmt"
)
// ListSpacesResponse represents the returned data payload from Spaces API's List command (/spaces)
type ListSpacesResponse struct {
Query map[string]int `json:"query"`
Spaces []*Space `json:"spaces"`
}
// RetrieveSpaceResponse represents the returned data payload from Spaces API's Retrieve command (/spaces/:id)
type RetrieveSpaceResponse struct {
Space
Charts []map[string]int `json:"charts,omitempty"`
}
// Space represents a single AppOptics Space
type Space struct {
// ID is the unique identifier of the Space
ID int `json:"id,omitempty"`
// Name is the name of the Space
Name string `json:"name,omitempty"`
}
// SpacesCommunicator defines the interface for the Spaces API
type SpacesCommunicator interface {
Create(string) (*Space, error)
List(*PaginationParameters) ([]*Space, error)
Retrieve(int) (*RetrieveSpaceResponse, error)
Update(int, string) error
Delete(int) error
}
type SpacesService struct {
client *Client
}
func NewSpacesService(c *Client) *SpacesService {
return &SpacesService{c}
}
// Create creates the Space with the given name
func (s *SpacesService) Create(name string) (*Space, error) {
bodyStruct := struct {
Name string `json:"name"`
}{name}
createdSpace := &Space{}
req, err := s.client.NewRequest("POST", "spaces", bodyStruct)
if err != nil {
return nil, err
}
_, err = s.client.Do(req, createdSpace)
if err != nil {
return nil, err
}
return createdSpace, nil
}
// List implements the Spaces API's List command
func (s *SpacesService) List(rp *PaginationParameters) ([]*Space, error) {
req, err := s.client.NewRequest("GET", "spaces", nil)
if err != nil {
return nil, err
}
if rp != nil {
rp.AddToRequest(req)
}
var spacesResponse ListSpacesResponse
_, err = s.client.Do(req, &spacesResponse)
if err != nil {
return nil, err
}
return spacesResponse.Spaces, nil
}
// Retrieve implements the Spaces API's Retrieve command
func (s *SpacesService) Retrieve(id int) (*RetrieveSpaceResponse, error) {
retrievedSpace := &RetrieveSpaceResponse{}
spacePath := fmt.Sprintf("spaces/%d", id)
req, err := s.client.NewRequest("GET", spacePath, nil)
if err != nil {
return nil, err
}
_, err = s.client.Do(req, retrievedSpace)
if err != nil {
return nil, err
}
return retrievedSpace, nil
}
// Update implements the Spaces API's Update command
func (s *SpacesService) Update(id int, name string) error {
requestedSpace := &Space{Name: name}
spacePath := fmt.Sprintf("spaces/%d", id)
req, err := s.client.NewRequest("PUT", spacePath, requestedSpace)
if err != nil {
return err
}
_, err = s.client.Do(req, nil)
if err != nil {
return err
}
return nil
}
// Delete implements the Spaces API's Delete command
func (s *SpacesService) Delete(id int) error {
spacePath := fmt.Sprintf("spaces/%d", id)
req, err := s.client.NewRequest("DELETE", spacePath, nil)
if err != nil {
return err
}
_, err = s.client.Do(req, nil)
if err != nil {
return err
}
return nil
}