forked from hanzoai/gochimp3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegments.go
121 lines (92 loc) · 2.95 KB
/
segments.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
package gochimp3
import "fmt"
const (
segments_path = "/lists/%s/segments"
single_segment_path = segments_path + "/%s"
)
type ListOfSegments struct {
baseList
Segments []Segment `json:"segments"`
ListID string `json:"list_id"`
}
type SegmentRequest struct {
Name string `json:"name"`
StaticSegment []string `json:"static_segments"`
Options SegmentOptions `json:"options"`
}
type Segment struct {
SegmentRequest
ID string `json:"id"`
MemberCount int `json:"member_count"`
Type string `json:"type"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ListID string `json:"list_id"`
withLinks
}
type SegmentOptions struct {
Match string `json:"match"`
Conditions []SegmentConditional `json:"conditions"`
}
// SegmentConditional represents parameters to filter by
type SegmentConditional struct {
Field string `json:"field"`
OP string `json:"op"`
Value float64 `json:"value"`
}
type SegmentQueryParams struct {
ExtendedQueryParams
Type string
SinceCreatedAt string
BeforeCreatedAt string
SinceUpdatedAt string
BeforeUpdatedAt string
}
func (q SegmentQueryParams) Params() map[string]string {
m := q.ExtendedQueryParams.Params()
m["type"] = q.Type
m["since_created_at"] = q.SinceCreatedAt
m["since_updated_at"] = q.SinceUpdatedAt
m["before_created_at"] = q.BeforeCreatedAt
m["before_updated_at"] = q.BeforeUpdatedAt
return m
}
func (list ListResponse) GetSegments(params *SegmentQueryParams) (*ListOfSegments, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(segments_path, list.ID)
response := new(ListOfSegments)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
func (list ListResponse) GetSegment(id string, params *BasicQueryParams) (*Segment, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(single_segment_path, list.ID, id)
response := new(Segment)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
func (list ListResponse) CreateSegment(body *SegmentRequest) (*Segment, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(segments_path, list.ID)
response := new(Segment)
return response, list.api.Request("POST", endpoint, nil, &body, response)
}
func (list ListResponse) UpdateSegment(id string, body *SegmentRequest) (*Segment, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(single_segment_path, list.ID, id)
response := new(Segment)
return response, list.api.Request("PATCH", endpoint, nil, &body, response)
}
func (list ListResponse) DeleteSegment(id string) (bool, error) {
if err := list.CanMakeRequest(); err != nil {
return false, err
}
endpoint := fmt.Sprintf(single_segment_path, list.ID, id)
return list.api.RequestOk("DELETE", endpoint)
}