-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivecampaign.go
118 lines (100 loc) · 2.28 KB
/
activecampaign.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
package activecampaign
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/sethgrid/pester"
)
var (
ErrNoURLProvided = errors.New("please provide your api url")
ErrNoAuthenticationProvided = errors.New("please provide an authentication method")
)
// ActiveCampaign will be the main
type ActiveCampaign struct {
Client *pester.Client
url string
apiKey string
output string
}
func New(url, apiKey string) (*ActiveCampaign, error) {
if url == "" {
return nil, ErrNoURLProvided
}
if strings.HasSuffix(url, "/") {
url = strings.TrimSuffix(url, "")
}
client := pester.New()
client.MaxRetries = 10
client.Backoff = pester.LinearBackoff
client.RetryOnHTTP429 = true
ac := ActiveCampaign{
Client: client,
output: "json",
url: url,
}
if apiKey != "" {
ac.apiKey = apiKey
} else {
return nil, ErrNoAuthenticationProvided
}
return &ac, nil
}
type POF struct {
Pagination *Pagination
Ordering []Ordering
Filtering []Filtering
}
type Pagination struct {
Limit int
Offset int
}
type Ordering struct {
Key string
Order string
}
type Filtering struct {
Key string
Value string
}
func (a *ActiveCampaign) send(ctx context.Context, method, api string, pof *POF, body io.Reader) (*http.Response, error) {
u, err := url.Parse(a.url + "/api/3/" + api)
if err != nil {
return nil, &Error{Op: "send", Err: err}
}
if pof != nil {
query := u.Query()
if pof.Pagination != nil {
query.Set("limit", strconv.Itoa(pof.Pagination.Limit))
query.Set("offset", strconv.Itoa(pof.Pagination.Offset))
}
for _, v := range pof.Ordering {
query.Add(fmt.Sprintf("orders[%s]", v.Key), v.Order)
}
for _, v := range pof.Filtering {
query.Add(fmt.Sprintf("filters[%s]", v.Key), v.Value)
}
u.RawQuery = query.Encode()
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), body)
if err != nil {
return nil, &Error{Op: "send", Err: err}
}
req.Header.Set("Api-Token", a.apiKey)
//b, _ := httputil.DumpRequest(req, true)
//fmt.Println(string(b))
res, err := a.Client.Do(req)
if err != nil {
return nil, &Error{Op: "send", Err: err}
}
// b, _ = httputil.DumpResponse(res, true)
// fmt.Println(string(b))
return res, nil
}
func (a *ActiveCampaign) CredentialsTest() bool {
return true
}