-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuttercms.go
295 lines (243 loc) · 5.45 KB
/
buttercms.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package ButterCMS
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os"
"strings"
)
var authToken string
var preview bool
const (
VERSION = "2.3.0"
API_BASE_URL = "https://api.buttercms.com/"
V2_ENDPOINT = "/v2"
)
var ENV_BASE_URL = os.Getenv("API_BASE_URL")
func getRequest(path string, params map[string]string) ([]byte, error) {
if authToken == "" {
return nil, errors.New("no auth token set")
}
client := http.DefaultClient
baseUrl := API_BASE_URL
if ENV_BASE_URL != "" {
baseUrl = ENV_BASE_URL
}
url, err := url.JoinPath(baseUrl, V2_ENDPOINT, path)
if err != nil {
return nil, err
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Butter-Client", "Go/"+VERSION)
q := req.URL.Query()
q.Add("auth_token", authToken)
for k := range params {
q.Add(k, params[k])
}
if preview {
q.Add("preview", "1")
}
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if http.StatusOK != resp.StatusCode {
return nil, errors.New(http.StatusText(resp.StatusCode))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
bodyBytes := []byte(body)
return bodyBytes, err
}
func SetAuthToken(token string) {
authToken = token
}
func SetPreviewMode(inPreview bool) {
preview = inPreview
}
///////////
// Feed
///////////
func GetFeed(feedType string) (*FeedAPIResponse, error) {
body, err := getRequest("feeds/"+feedType+"/", nil)
if err != nil {
return nil, err
}
var resp = new(FeedAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
///////////
// Pages
///////////
func GetPages(pageType string, params map[string]string) (*PagesAPIResponse, error) {
body, err := getRequest("pages/"+pageType+"/", params)
if err != nil {
return nil, err
}
var resp = new(PagesAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
func GetPage(pageType string, slug string, params map[string]string) (*PageAPIResponse, error) {
body, err := getRequest("pages/"+pageType+"/"+slug+"/", params)
if err != nil {
return nil, err
}
var resp = new(PageAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
///////////
// Posts
///////////
func SearchPosts(query string, params map[string]string) (*PostsAPIResponse, error) {
params["query"] = query
body, err := getRequest("search/", params)
if err != nil {
return nil, err
}
var resp = new(PostsAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
func GetPosts(params map[string]string) (*PostsAPIResponse, error) {
body, err := getRequest("posts/", params)
if err != nil {
return nil, err
}
var resp = new(PostsAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
func GetPost(slug string) (*PostAPIResponse, error) {
body, err := getRequest("posts/"+slug+"/", nil)
if err != nil {
return nil, err
}
var resp = new(PostAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
///////////
// Authors
///////////
func GetAuthors(params map[string]string) (*AuthorsAPIResponse, error) {
body, err := getRequest("authors/", params)
if err != nil {
return nil, err
}
var resp = new(AuthorsAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
func GetAuthor(slug string, params map[string]string) (*AuthorAPIResponse, error) {
body, err := getRequest("authors/"+slug+"/", params)
if err != nil {
return nil, err
}
var resp = new(AuthorAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
///////////
// Categories
///////////
func GetCategories(params map[string]string) (*CategoriesAPIResponse, error) {
body, err := getRequest("categories/", params)
if err != nil {
return nil, err
}
var resp = new(CategoriesAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
func GetCategory(slug string, params map[string]string) (*CategoryAPIResponse, error) {
body, err := getRequest("categories/"+slug, params)
if err != nil {
return nil, err
}
var resp = new(CategoryAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
///////////
// Tags
///////////
func GetTags(params map[string]string) (*TagsAPIResponse, error) {
body, err := getRequest("tags/", params)
if err != nil {
return nil, err
}
var resp = new(TagsAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
func GetTag(slug string, params map[string]string) (*TagAPIResponse, error) {
body, err := getRequest("tags/"+slug+"/", params)
if err != nil {
return nil, err
}
var resp = new(TagAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}
///////////
// Content Fields
///////////
func GetContentFields(keys []string, params map[string]string) (*ContentFieldsAPIResponse, error) {
params["keys"] = strings.Join(keys, ",")
body, err := getRequest("content/", params)
if err != nil {
return nil, err
}
var resp = new(ContentFieldsAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}