-
Notifications
You must be signed in to change notification settings - Fork 4
/
locations.go
170 lines (136 loc) · 3.54 KB
/
locations.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
package fossil
import (
"encoding/json"
"fmt"
"time"
)
//***** Structures *****//
// Location represents a server location
type Location struct {
ID int `json:"id"`
ShortName string `json:"short"`
LongName string `json:"long"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
}
// jsonLocationPage is a slate to correctly parse responses from the API into JSON
type jsonLocationPage struct {
Data []struct {
Location *Location `json:"attributes"`
} `json:"data"`
Meta Meta `json:"meta"`
}
//***** Converters *****//
// asUserSlice parses a jsonUserPage into a []*User
func (lp *jsonLocationPage) asLocationSlice() (locations []*Location) {
for _, l := range lp.Data {
locations = append(locations, l.Location)
}
return locations
}
//***** Pagination *****//
// getAll fetches all the existing pages for a location. The original page is kept as index 0
func (lp *jsonLocationPage) getAll(token string) (pages []*jsonLocationPage, err error) {
pages = append(pages, lp)
for pages[len(pages)-1].Meta.Pagination.Links.Next != "" {
url := lp.Meta.Pagination.Links.Next
bytes, err := queryURL(url, token, "GET", nil)
if err != nil {
return nil, err
}
var page jsonLocationPage
err = json.Unmarshal(bytes, &page)
if err != nil {
return nil, err
}
pages = append(pages, &page)
}
return pages, nil
}
//***** Requests *****//
// GetLocations fetches all available locations
func (c *ApplicationCredentials) GetLocations() (locations []*Location, err error) {
bytes, err := c.query("locations", "GET", nil)
if err != nil {
return
}
// Get the initial page
var page jsonLocationPage
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 {
locations = append(locations, page.asLocationSlice()...)
}
return
}
// GetLocation fetches the location with the given ID
func (c *ApplicationCredentials) GetLocation(id int) (loc *Location, err error) {
bytes, err := c.query(fmt.Sprintf("locations/%d", id), "GET", nil)
if err != nil {
return
}
var wrapper struct {
Location *Location `json:"attributes"`
}
err = json.Unmarshal(bytes, &wrapper)
if err != nil {
return
}
return wrapper.Location, nil
}
// CreateLocation makes a new location with the provided names
func (c *ApplicationCredentials) CreateLocation(shortName, longName string) (loc *Location, err error) {
type names struct {
Short string `json:"short"`
Long string `json:"long"`
}
b := names{
Short: shortName,
Long: longName,
}
nms, err := json.Marshal(b)
if err != nil {
return
}
bytes, err := c.query("locations", "POST", nms)
if err != nil {
return
}
var fWrapper struct {
Location *Location `json:"attributes"`
}
err = json.Unmarshal(bytes, &fWrapper)
if err != nil {
return
}
return fWrapper.Location, nil
}
// UpdateLocationName modifies the short and long names of a location
func (c *ApplicationCredentials) UpdateLocationName(loc *Location) (err error) {
type names struct {
Short string `json:"short"`
Long string `json:"long"`
}
b := names{
Short: loc.ShortName,
Long: loc.LongName,
}
nms, err := json.Marshal(b)
if err != nil {
return
}
_, err = c.query(fmt.Sprintf("locations/%d", loc.ID), "PATCH", nms)
return
}
// DeleteLocation marks a server as suspended
func (c *ApplicationCredentials) DeleteLocation(lid int) (err error) {
_, err = c.query(fmt.Sprintf("locations/%d", lid), "DELETE", nil)
return
}