-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathforms.go
163 lines (134 loc) · 4.65 KB
/
forms.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
package mailerlite
import (
"context"
"fmt"
"net/http"
)
const formEndpoint = "/forms"
// FormService defines an interface for form-related operations.
type FormService interface {
List(ctx context.Context, options *ListFormOptions) (*RootForms, *Response, error)
Get(ctx context.Context, formID string) (*RootForm, *Response, error)
Update(ctx context.Context, formID, formName string) (*RootForm, *Response, error)
Delete(ctx context.Context, formID string) (*Response, error)
Subscribers(ctx context.Context, options *ListFormSubscriberOptions) (*RootSubscribers, *Response, error)
}
// formService implements FormsService.
type formService struct {
*service
}
type RootForm struct {
Data Form `json:"data"`
}
type RootForms struct {
Data []Form `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
type Form struct {
Id string `json:"id"`
Type string `json:"type"`
Slug string `json:"slug"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
ConversionsCount int `json:"conversions_count"`
ConversionsRate ConversionRate `json:"conversions_rate"`
OpensCount int `json:"opens_count"`
Settings map[string]interface{} `json:"settings"`
LastRegistrationAt interface{} `json:"last_registration_at"`
Active bool `json:"active"`
IsBroken bool `json:"is_broken"`
HasContent bool `json:"has_content"`
Can Can `json:"can"`
UsedInAutomations bool `json:"used_in_automations"`
Warnings []interface{} `json:"warnings"`
DoubleOptin interface{} `json:"double_optin"`
ScreenshotUrl interface{} `json:"screenshot_url"`
}
type ConversionRate struct {
Float int `json:"float"`
String string `json:"string"`
}
type Can struct {
Update bool `json:"update"`
}
// ListFormOptions - modifies the behavior of FormService.List method
type ListFormOptions struct {
Type string `url:"-"`
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
Sort string `url:"sort,omitempty"`
}
// ListFormSubscriberOptions - modifies the behavior of FormService.Subscribers method
type ListFormSubscriberOptions struct {
FormID string `url:"-"`
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
func (s *formService) List(ctx context.Context, options *ListFormOptions) (*RootForms, *Response, error) {
path := fmt.Sprintf("%s/%s", formEndpoint, options.Type)
req, err := s.client.newRequest(http.MethodGet, path, options)
if err != nil {
return nil, nil, err
}
root := new(RootForms)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *formService) Get(ctx context.Context, formID string) (*RootForm, *Response, error) {
path := fmt.Sprintf("%s/%s", formEndpoint, formID)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(RootForm)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *formService) Update(ctx context.Context, formID, formName string) (*RootForm, *Response, error) {
body := map[string]interface{}{"name": formName}
path := fmt.Sprintf("%s/%s", formEndpoint, formID)
req, err := s.client.newRequest(http.MethodPut, path, body)
if err != nil {
return nil, nil, err
}
root := new(RootForm)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *formService) Delete(ctx context.Context, formID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", formEndpoint, formID)
req, err := s.client.newRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
res, err := s.client.do(ctx, req, nil)
if err != nil {
return res, err
}
return res, nil
}
func (s *formService) Subscribers(ctx context.Context, options *ListFormSubscriberOptions) (*RootSubscribers, *Response, error) {
path := fmt.Sprintf("%s/%s/subscribers", formEndpoint, options.FormID)
req, err := s.client.newRequest(http.MethodGet, path, options)
if err != nil {
return nil, nil, err
}
root := new(RootSubscribers)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}