Repository navigation
Expand file tree
/
Copy pathcomponents.go
More file actions
167 lines (138 loc) · 4.11 KB
/
Copy pathcomponents.go
File metadata and controls
167 lines (138 loc) · 4.11 KB
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
164
165
166
167
package loops
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Component is a reusable LMX snippet that can be included from other email
// messages. It is identified by ID and has an LMX body.
type Component struct {
ID string `json:"id"`
Name string `json:"name"`
LMX string `json:"lmx"`
}
// CreateComponentRequest is the request body for [Client.CreateComponent].
// Both fields are required.
type CreateComponentRequest struct {
Name string `json:"name"`
LMX string `json:"lmx"`
}
// UpdateComponentRequest is the request body for [Client.UpdateComponent].
// At least one field must be set.
type UpdateComponentRequest struct {
Name string `json:"name,omitempty"`
LMX string `json:"lmx,omitempty"`
}
// UpdateComponentResult is the result of [Client.UpdateComponent]. It embeds
// the updated [Component] and adds AffectedEmailCount, the number of emails
// using this component that were updated by the body change (0 when only the
// name changed).
type UpdateComponentResult struct {
Component
AffectedEmailCount int `json:"affectedEmailCount"`
}
// CreateComponent creates a new component.
func (c *Client) CreateComponent(req CreateComponentRequest) (*Component, error) {
b, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to encode request: %w", err)
}
httpReq, err := c.newRequest(http.MethodPost, "/components", bytes.NewReader(b))
if err != nil {
return nil, err
}
resp, err := c.do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return nil, errorFromResponse(resp)
}
var result Component
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result, nil
}
// UpdateComponent updates the component identified by id.
func (c *Client) UpdateComponent(id string, req UpdateComponentRequest) (*UpdateComponentResult, error) {
b, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to encode request: %w", err)
}
httpReq, err := c.newRequest(http.MethodPost, "/components/"+id, bytes.NewReader(b))
if err != nil {
return nil, err
}
resp, err := c.do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResponse(resp)
}
var result UpdateComponentResult
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result, nil
}
// GetComponent returns the component identified by id.
func (c *Client) GetComponent(id string) (*Component, error) {
req, err := c.newRequest(http.MethodGet, "/components/"+id, nil)
if err != nil {
return nil, err
}
resp, err := c.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResponse(resp)
}
var result Component
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result, nil
}
// ListComponents returns a single page of components along with pagination
// information. To iterate every page, use [Paginate].
func (c *Client) ListComponents(params PaginationParams) ([]Component, *Pagination, error) {
q := url.Values{}
if params.PerPage != "" {
q.Set("perPage", params.PerPage)
}
if params.Cursor != "" {
q.Set("cursor", params.Cursor)
}
path := "/components"
if len(q) > 0 {
path += "?" + q.Encode()
}
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
resp, err := c.do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, nil, errorFromResponse(resp)
}
var result struct {
Pagination Pagination `json:"pagination"`
Data []Component `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, nil, fmt.Errorf("failed to decode response: %w", err)
}
return result.Data, &result.Pagination, nil
}