-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebflow.go
78 lines (63 loc) · 1.54 KB
/
webflow.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
package webflow
import (
"crypto/tls"
"errors"
"fmt"
"strconv"
"github.com/astaxie/beego/httplib"
)
// API for webflow structure
type API struct {
Token string
Version string
Debug bool
}
// Param for http get parameter
type Param struct {
Page int
PerPage int
}
var (
// ErrorMissingTokenOrVersion for missing config
ErrorMissingTokenOrVersion = errors.New("missing webflow token or version")
)
// New for web flow API object
func New(token, version string, debug bool) (*API, error) {
if token == "" || version == "" {
return nil, ErrorMissingTokenOrVersion
}
api := &API{
Token: token,
Version: version,
Debug: debug,
}
return api, nil
}
// GetAllItemsFromCollection Get All Items For a Collection
func (api *API) GetAllItemsFromCollection(id string, resp interface{}, params ...Param) error {
url := fmt.Sprintf(allItemsFromCollectionURL, id)
err := api.fetchData(url, resp, params...)
return err
}
func (api *API) fetchData(url string, res interface{}, params ...Param) error {
req := httplib.Get(url).Debug(api.Debug)
req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
// Add authorization header
req.Header("Authorization", "Bearer "+api.Token)
req.Header("Accept-Version", api.Version)
offset := 0
limit := 10
if len(params) > 0 {
param := params[0]
if param.PerPage > 0 {
limit = param.PerPage
}
if param.Page > 1 {
offset = (param.Page - 1) * limit
}
}
req.Param("offset", strconv.Itoa(offset))
req.Param("limit", strconv.Itoa(limit))
err := req.ToJSON(res)
return err
}