-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathservices.go
113 lines (88 loc) · 2.3 KB
/
services.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
package appoptics
import "fmt"
type Service struct {
ID int `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Settings map[string]string `json:"settings,omitempty"`
Title string `json:"title,omitempty"`
}
type ServicesCommunicator interface {
List() (*ListServicesResponse, error)
Retrieve(int) (*Service, error)
Create(*Service) (*Service, error)
Update(*Service) error
Delete(int) error
}
type ListServicesResponse struct {
Query QueryInfo `json:"query,omitempty"`
Services []*Service `json:"services,omitempty"`
}
type ServicesService struct {
client *Client
}
func NewServiceService(c *Client) *ServicesService {
return &ServicesService{c}
}
// List retrieves all Services
func (ss *ServicesService) List() (*ListServicesResponse, error) {
req, err := ss.client.NewRequest("GET", "services", nil)
if err != nil {
return nil, err
}
servicesResponse := &ListServicesResponse{}
_, err = ss.client.Do(req, &servicesResponse)
if err != nil {
return nil, err
}
return servicesResponse, nil
}
// Retrieve returns the Service identified by the parameter
func (ss *ServicesService) Retrieve(id int) (*Service, error) {
service := &Service{}
path := fmt.Sprintf("services/%d", id)
req, err := ss.client.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
_, err = ss.client.Do(req, service)
if err != nil {
return nil, err
}
return service, nil
}
// Create creates the Service
func (ss *ServicesService) Create(s *Service) (*Service, error) {
req, err := ss.client.NewRequest("POST", "services", s)
if err != nil {
return nil, err
}
createdService := &Service{}
_, err = ss.client.Do(req, createdService)
if err != nil {
return nil, err
}
return createdService, nil
}
// Update updates the Service
func (ss *ServicesService) Update(s *Service) error {
path := fmt.Sprintf("services/%d", s.ID)
req, err := ss.client.NewRequest("PUT", path, s)
if err != nil {
return err
}
_, err = ss.client.Do(req, nil)
if err != nil {
return err
}
return nil
}
// Delete deletes the Service
func (ss *ServicesService) Delete(id int) error {
path := fmt.Sprintf("services/%d", id)
req, err := ss.client.NewRequest("DELETE", path, nil)
if err != nil {
return err
}
_, err = ss.client.Do(req, nil)
return err
}