-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfields.go
115 lines (93 loc) · 2.72 KB
/
fields.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
package mailerlite
import (
"context"
"fmt"
"net/http"
)
const fieldEndpoint = "/fields"
// FieldService defines an interface for field-related operations.
type FieldService interface {
List(ctx context.Context, options *ListFieldOptions) (*RootFields, *Response, error)
Create(ctx context.Context, fieldName, fieldType string) (*RootField, *Response, error)
Update(ctx context.Context, fieldID, fieldName string) (*RootField, *Response, error)
Delete(ctx context.Context, fieldID string) (*Response, error)
}
// fieldService implements FieldService.
type fieldService struct {
*service
}
type RootField struct {
Data Field `json:"data"`
}
type RootFields struct {
Data []Field `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
type Field struct {
Id string `json:"id"`
Name string `json:"name"`
Key string `json:"key"`
Type string `json:"type"`
}
// ListFieldOptions - modifies the behavior of FieldService.List method
type ListFieldOptions struct {
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
Sort string `url:"sort,omitempty"`
}
func (s *fieldService) List(ctx context.Context, options *ListFieldOptions) (*RootFields, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, fieldEndpoint, options)
if err != nil {
return nil, nil, err
}
root := new(RootFields)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *fieldService) Create(ctx context.Context, fieldName, fieldType string) (*RootField, *Response, error) {
body := map[string]interface{}{
"name": fieldName,
"type": fieldType,
}
req, err := s.client.newRequest(http.MethodPost, fieldEndpoint, body)
if err != nil {
return nil, nil, err
}
root := new(RootField)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *fieldService) Update(ctx context.Context, fieldID, fieldName string) (*RootField, *Response, error) {
body := map[string]interface{}{"name": fieldName}
path := fmt.Sprintf("%s/%s", fieldEndpoint, fieldID)
req, err := s.client.newRequest(http.MethodPut, path, body)
if err != nil {
return nil, nil, err
}
root := new(RootField)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *fieldService) Delete(ctx context.Context, fieldID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", fieldEndpoint, fieldID)
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
}