-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsites.go
144 lines (129 loc) · 3.34 KB
/
sites.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
139
140
141
142
143
144
package kel
import (
"fmt"
"net/http"
"github.com/brosner/go-json-spec-handler"
)
const (
sitesResourceType string = "sites"
sitesAPIPath string = "/sites"
)
// SiteService represents the link between the Site and the Client.
type SiteService struct {
client *Client
}
func (srv *SiteService) getPath(resourceGroup *ResourceGroup) string {
return fmt.Sprintf("%s%s", resourceGroup.getDetailPath(), sitesAPIPath)
}
// Create sends an HTTP request to create the Kel site.
func (srv *SiteService) Create(site *Site) CreateRequest {
return &createRequest{
client: srv.client,
path: srv.getPath(site.ResourceGroup),
buildDoc: buildSiteDoc(site),
handler: func(document *jsh.Document) error {
obj := document.Data[0]
if objErr := obj.Unmarshal(sitesResourceType, site); objErr != nil {
return objErr
}
site.srv = srv
return nil
},
}
}
// List returns all sites reachable by the API
func (srv *SiteService) List(resourceGroup *ResourceGroup, sites *[]*Site) ListRequest {
return &listRequest{
client: srv.client,
path: srv.getPath(resourceGroup),
handler: func(document *jsh.Document) error {
for i := range document.Data {
obj := document.Data[i]
site := &Site{srv: srv}
if objErr := obj.Unmarshal(site.GetResourceType(), site); objErr != nil {
return objErr
}
*sites = append(*sites, site)
}
return nil
},
}
}
// Get returns the site with the given name reachable by the API
func (srv *SiteService) Get(name string, site *Site) GetRequest {
site.Name = name
return &getRequest{
client: srv.client,
path: site.getDetailPath(),
handler: func(document *jsh.Document) error {
obj := document.Data[0]
if objErr := obj.Unmarshal(sitesResourceType, site); objErr != nil {
return objErr
}
site.srv = srv
return nil
},
}
}
func (site *Site) getDetailPath() string {
return fmt.Sprintf(
"%s%s/%s",
site.ResourceGroup.getDetailPath(),
sitesAPIPath,
site.GetID(),
)
}
// GetResourceType ...
func (site *Site) GetResourceType() string {
return sitesResourceType
}
// GetID ...
func (site *Site) GetID() string {
return site.Name
}
// Reload will get an updated site and point to it as its own.
func (site *Site) Reload() error {
reloaded := &Site{}
if err := site.srv.Get(site.GetID(), reloaded).Do(); err != nil {
return err
}
*site = *reloaded
return nil
}
// Save will persistent local data with the API.
func (site *Site) Save() error {
req := &updateRequest{
client: site.srv.client,
path: site.getDetailPath(),
buildDoc: buildSiteDoc(site),
handler: func(document *jsh.Document) error {
obj := document.Data[0]
if objErr := obj.Unmarshal(sitesResourceType, site); objErr != nil {
return objErr
}
return nil
},
}
return req.Do()
}
// Delete will destroy the site.
func (site *Site) Delete() error {
req := &deleteRequest{
client: site.srv.client,
path: site.getDetailPath(),
}
return req.Do()
}
func buildSiteDoc(site *Site) func(outreq *http.Request) (*jsh.Document, error) {
return func(outreq *http.Request) (*jsh.Document, error) {
obj, objErr := jsh.NewObject(site.GetID(), sitesResourceType, site)
if objErr != nil {
return nil, objErr
}
if err := obj.Validate(outreq, false); err != nil {
return nil, fmt.Errorf("error preparing object: %s", err.Error())
}
doc := jsh.Build(obj)
return doc, nil
}
}