forked from ccuetoh/fossil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nests.go
194 lines (159 loc) · 4.35 KB
/
nests.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package fossil
import (
"encoding/json"
"fmt"
"time"
)
//***** Structures *****//
// Nest represents the information regarding a nest
type Nest struct {
ID int `json:"id"`
UUID string `json:"uuid"`
Author string `json:"author"`
Name string `json:"name"`
Description string `json:"description"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// jsonNestPage is a slate to correctly parse responses from the API into JSON
type jsonNestPage struct {
Data []struct {
Object string `json:"object"`
Nest *Nest `json:"attributes"`
} `json:"data"`
Meta Meta `json:"meta"`
}
// Egg represents the information regarding an egg
type Egg struct {
ID int `json:"id"`
UUID string `json:"uuid"`
Nest int `json:"nest"`
Author string `json:"author"`
Description string `json:"description"`
DockerImage string `json:"docker_image"`
Config EggConfig `json:"config"`
Startup string `json:"startup"`
Script EggScript `json:"script"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// EggStartup represents the startup settings of an egg
type EggStartup struct {
Done string `json:"done"`
UserInteraction []string `json:"userInteraction"`
}
// EggConfig represents the configurations of an egg
type EggConfig struct {
Startup EggStartup `json:"startup"`
Stop string `json:"stop"`
CustomConfig []string `json:"custom_config"`
Extends string `json:"extends"`
}
// EggScript represents the script configuration of an egg
type EggScript struct {
Privileged bool `json:"privileged"`
Install string `json:"install"`
Entry string `json:"entry"`
Container string `json:"container"`
Extends string `json:"extends"`
}
//***** Converters *****//
// asUserSlice parses a jsonUserPage into a []*User
func (np *jsonNestPage) asNestSlice() (nests []*Nest) {
for _, n := range np.Data {
nests = append(nests, n.Nest)
}
return nests
}
//***** Pagination *****//
// getAll fetches all the existing pages for a nest. The original page is kept as index 0
func (np *jsonNestPage) getAll(token string) (pages []*jsonNestPage, err error) {
pages = append(pages, np)
for pages[len(pages)-1].Meta.Pagination.Links.Next != "" {
url := np.Meta.Pagination.Links.Next
bytes, err := queryURL(url, token, "GET", nil)
if err != nil {
return nil, err
}
var page jsonNestPage
err = json.Unmarshal(bytes, &page)
if err != nil {
return nil, err
}
pages = append(pages, &page)
}
return pages, nil
}
//***** Requests *****//
// GetNests fetches all available nests
func (c *ApplicationCredentials) GetNests() (nests []*Nest, err error) {
bytes, err := c.query("nests", "GET", nil)
if err != nil {
return
}
// Get the initial page
var page jsonNestPage
err = json.Unmarshal(bytes, &page)
if err != nil {
return
}
// Search for the remaining pages if present
pages, err := page.getAll(c.Token)
if err != nil {
return
}
for _, page := range pages {
nests = append(nests, page.asNestSlice()...)
}
return
}
// GetNest fetches, if present, a specific nest.
func (c *ApplicationCredentials) GetNest(id int) (nest *Nest, err error) {
bytes, err := c.query(fmt.Sprintf("nests/%d", id), "GET", nil)
if err != nil {
return
}
var wrapper struct {
Nest Nest `json:"attributes"`
}
err = json.Unmarshal(bytes, &wrapper)
if err != nil {
return
}
return &wrapper.Nest, nil
}
// GetEggs fetches all eggs inside a nest
func (c *ApplicationCredentials) GetEggs(nestID int) (eggs []*Egg, err error) {
bytes, err := c.query(fmt.Sprintf("nests/%d/eggs", nestID), "GET", nil)
if err != nil {
return
}
var wrapper struct {
Data []struct {
Egg *Egg `json:"attributes"`
} `json:"data"`
}
err = json.Unmarshal(bytes, &wrapper)
if err != nil {
return
}
for _, d := range wrapper.Data {
eggs = append(eggs, d.Egg)
}
return
}
// GetEgg searches for a specific eggs inside a nest
func (c *ApplicationCredentials) GetEgg(nestID int, eggID int) (egg *Egg, err error) {
bytes, err := c.query(fmt.Sprintf("nests/%d/eggs/%d", nestID, eggID), "GET", nil)
if err != nil {
return
}
var wrapper struct {
Egg *Egg `json:"attributes"`
}
err = json.Unmarshal(bytes, &wrapper)
if err != nil {
return
}
return wrapper.Egg, nil
}