|
4 | 4 | package forwardemail |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
7 | 8 | "encoding/json" |
8 | 9 | "fmt" |
9 | | - "io" |
10 | 10 | "net/url" |
11 | 11 | "strconv" |
12 | 12 | "strings" |
@@ -39,55 +39,89 @@ type AliasParameters struct { |
39 | 39 | } |
40 | 40 |
|
41 | 41 | // GetAliases retrieves all email aliases for the specified domain. |
42 | | -func (c *Client) GetAliases(domain string) ([]Alias, error) { |
43 | | - req, err := c.newRequest("GET", fmt.Sprintf("/v1/domains/%s/aliases", domain)) |
44 | | - if err != nil { |
| 42 | +func (c *Client) GetAliases(ctx context.Context, domain string) ([]Alias, error) { |
| 43 | + if ctx == nil { |
| 44 | + return nil, ErrNilContext |
| 45 | + } |
| 46 | + if err := ctx.Err(); err != nil { |
45 | 47 | return nil, err |
46 | 48 | } |
| 49 | + if strings.TrimSpace(domain) == "" { |
| 50 | + return nil, ErrEmptyDomain |
| 51 | + } |
| 52 | + |
| 53 | + encodedDomain := url.PathEscape(domain) |
| 54 | + |
| 55 | + req, err := c.newRequest(ctx, "GET", fmt.Sprintf("/v1/domains/%s/aliases", encodedDomain), nil) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("failed to create request for GetAliases: %w", err) |
| 58 | + } |
47 | 59 |
|
48 | 60 | res, err := c.doRequest(req) |
49 | 61 | if err != nil { |
50 | | - return nil, err |
| 62 | + return nil, fmt.Errorf("failed to fetch aliases: %w", err) |
51 | 63 | } |
52 | 64 |
|
53 | 65 | var items []Alias |
54 | | - |
55 | | - err = json.Unmarshal(res, &items) |
56 | | - if err != nil { |
57 | | - return nil, err |
| 66 | + if err := json.Unmarshal(res, &items); err != nil { |
| 67 | + return nil, fmt.Errorf("failed to parse aliases response: %w", err) |
58 | 68 | } |
59 | 69 |
|
60 | 70 | return items, nil |
61 | 71 | } |
62 | 72 |
|
63 | 73 | // GetAlias retrieves a specific email alias by name for the specified domain. |
64 | | -func (c *Client) GetAlias(domain, alias string) (*Alias, error) { |
65 | | - req, err := c.newRequest("GET", fmt.Sprintf("/v1/domains/%s/aliases/%s", domain, alias)) |
66 | | - if err != nil { |
| 74 | +func (c *Client) GetAlias(ctx context.Context, domain, alias string) (*Alias, error) { |
| 75 | + if ctx == nil { |
| 76 | + return nil, ErrNilContext |
| 77 | + } |
| 78 | + if err := ctx.Err(); err != nil { |
67 | 79 | return nil, err |
68 | 80 | } |
| 81 | + if strings.TrimSpace(domain) == "" { |
| 82 | + return nil, ErrEmptyDomain |
| 83 | + } |
| 84 | + if strings.TrimSpace(alias) == "" { |
| 85 | + return nil, ErrEmptyAlias |
| 86 | + } |
| 87 | + |
| 88 | + encodedDomain := url.PathEscape(domain) |
| 89 | + encodedAlias := url.PathEscape(alias) |
| 90 | + |
| 91 | + req, err := c.newRequest(ctx, "GET", fmt.Sprintf("/v1/domains/%s/aliases/%s", encodedDomain, encodedAlias), nil) |
| 92 | + if err != nil { |
| 93 | + return nil, fmt.Errorf("failed to create request for GetAlias: %w", err) |
| 94 | + } |
69 | 95 |
|
70 | 96 | res, err := c.doRequest(req) |
71 | 97 | if err != nil { |
72 | | - return nil, err |
| 98 | + return nil, fmt.Errorf("failed to fetch alias: %w", err) |
73 | 99 | } |
74 | 100 |
|
75 | 101 | var item Alias |
76 | | - |
77 | | - err = json.Unmarshal(res, &item) |
78 | | - if err != nil { |
79 | | - return nil, err |
| 102 | + if err := json.Unmarshal(res, &item); err != nil { |
| 103 | + return nil, fmt.Errorf("failed to parse alias response: %w", err) |
80 | 104 | } |
81 | 105 |
|
82 | 106 | return &item, nil |
83 | 107 | } |
84 | 108 |
|
85 | 109 | // CreateAlias creates a new email alias for the specified domain with the given parameters. |
86 | | -func (c *Client) CreateAlias(domain, alias string, parameters AliasParameters) (*Alias, error) { |
87 | | - req, err := c.newRequest("POST", fmt.Sprintf("/v1/domains/%s/aliases", domain)) |
88 | | - if err != nil { |
| 110 | +func (c *Client) CreateAlias(ctx context.Context, domain, alias string, parameters AliasParameters) (*Alias, error) { |
| 111 | + if ctx == nil { |
| 112 | + return nil, ErrNilContext |
| 113 | + } |
| 114 | + if err := ctx.Err(); err != nil { |
89 | 115 | return nil, err |
90 | 116 | } |
| 117 | + if strings.TrimSpace(domain) == "" { |
| 118 | + return nil, ErrEmptyDomain |
| 119 | + } |
| 120 | + if strings.TrimSpace(alias) == "" { |
| 121 | + return nil, ErrEmptyAlias |
| 122 | + } |
| 123 | + |
| 124 | + encodedDomain := url.PathEscape(domain) |
91 | 125 |
|
92 | 126 | params := url.Values{} |
93 | 127 | params.Add("name", alias) |
@@ -115,30 +149,43 @@ func (c *Client) CreateAlias(domain, alias string, parameters AliasParameters) ( |
115 | 149 | } |
116 | 150 | } |
117 | 151 |
|
118 | | - req.Body = io.NopCloser(strings.NewReader(params.Encode())) |
| 152 | + req, err := c.newRequest(ctx, "POST", fmt.Sprintf("/v1/domains/%s/aliases", encodedDomain), strings.NewReader(params.Encode())) |
| 153 | + if err != nil { |
| 154 | + return nil, fmt.Errorf("failed to create request for CreateAlias: %w", err) |
| 155 | + } |
| 156 | + |
119 | 157 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
120 | 158 |
|
121 | 159 | res, err := c.doRequest(req) |
122 | 160 | if err != nil { |
123 | | - return nil, err |
| 161 | + return nil, fmt.Errorf("failed to create alias: %w", err) |
124 | 162 | } |
125 | 163 |
|
126 | 164 | var item Alias |
127 | | - |
128 | | - err = json.Unmarshal(res, &item) |
129 | | - if err != nil { |
130 | | - return nil, err |
| 165 | + if err := json.Unmarshal(res, &item); err != nil { |
| 166 | + return nil, fmt.Errorf("failed to parse create alias response: %w", err) |
131 | 167 | } |
132 | 168 |
|
133 | 169 | return &item, nil |
134 | 170 | } |
135 | 171 |
|
136 | 172 | // UpdateAlias updates an existing email alias with new parameters for the specified domain. |
137 | | -func (c *Client) UpdateAlias(domain, alias string, parameters AliasParameters) (*Alias, error) { |
138 | | - req, err := c.newRequest("PUT", fmt.Sprintf("/v1/domains/%s/aliases/%s", domain, alias)) |
139 | | - if err != nil { |
| 173 | +func (c *Client) UpdateAlias(ctx context.Context, domain, alias string, parameters AliasParameters) (*Alias, error) { |
| 174 | + if ctx == nil { |
| 175 | + return nil, ErrNilContext |
| 176 | + } |
| 177 | + if err := ctx.Err(); err != nil { |
140 | 178 | return nil, err |
141 | 179 | } |
| 180 | + if strings.TrimSpace(domain) == "" { |
| 181 | + return nil, ErrEmptyDomain |
| 182 | + } |
| 183 | + if strings.TrimSpace(alias) == "" { |
| 184 | + return nil, ErrEmptyAlias |
| 185 | + } |
| 186 | + |
| 187 | + encodedDomain := url.PathEscape(domain) |
| 188 | + encodedAlias := url.PathEscape(alias) |
142 | 189 |
|
143 | 190 | params := url.Values{} |
144 | 191 | params.Add("name", alias) |
@@ -166,34 +213,52 @@ func (c *Client) UpdateAlias(domain, alias string, parameters AliasParameters) ( |
166 | 213 | } |
167 | 214 | } |
168 | 215 |
|
169 | | - req.Body = io.NopCloser(strings.NewReader(params.Encode())) |
| 216 | + req, err := c.newRequest(ctx, "PUT", fmt.Sprintf("/v1/domains/%s/aliases/%s", encodedDomain, encodedAlias), strings.NewReader(params.Encode())) |
| 217 | + if err != nil { |
| 218 | + return nil, fmt.Errorf("failed to create request for UpdateAlias: %w", err) |
| 219 | + } |
| 220 | + |
170 | 221 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
171 | 222 |
|
172 | 223 | res, err := c.doRequest(req) |
173 | 224 | if err != nil { |
174 | | - return nil, err |
| 225 | + return nil, fmt.Errorf("failed to update alias: %w", err) |
175 | 226 | } |
176 | 227 |
|
177 | 228 | var item Alias |
178 | | - |
179 | | - err = json.Unmarshal(res, &item) |
180 | | - if err != nil { |
181 | | - return nil, err |
| 229 | + if err := json.Unmarshal(res, &item); err != nil { |
| 230 | + return nil, fmt.Errorf("failed to parse update alias response: %w", err) |
182 | 231 | } |
183 | 232 |
|
184 | 233 | return &item, nil |
185 | 234 | } |
186 | 235 |
|
187 | 236 | // DeleteAlias removes an email alias from the specified domain. |
188 | | -func (c *Client) DeleteAlias(domain, alias string) error { |
189 | | - req, err := c.newRequest("DELETE", fmt.Sprintf("/v1/domains/%s/aliases/%s", domain, alias)) |
190 | | - if err != nil { |
| 237 | +func (c *Client) DeleteAlias(ctx context.Context, domain, alias string) error { |
| 238 | + if ctx == nil { |
| 239 | + return ErrNilContext |
| 240 | + } |
| 241 | + if err := ctx.Err(); err != nil { |
191 | 242 | return err |
192 | 243 | } |
| 244 | + if strings.TrimSpace(domain) == "" { |
| 245 | + return ErrEmptyDomain |
| 246 | + } |
| 247 | + if strings.TrimSpace(alias) == "" { |
| 248 | + return ErrEmptyAlias |
| 249 | + } |
| 250 | + |
| 251 | + encodedDomain := url.PathEscape(domain) |
| 252 | + encodedAlias := url.PathEscape(alias) |
| 253 | + |
| 254 | + req, err := c.newRequest(ctx, "DELETE", fmt.Sprintf("/v1/domains/%s/aliases/%s", encodedDomain, encodedAlias), nil) |
| 255 | + if err != nil { |
| 256 | + return fmt.Errorf("failed to create request for DeleteAlias: %w", err) |
| 257 | + } |
193 | 258 |
|
194 | 259 | _, err = c.doRequest(req) |
195 | 260 | if err != nil { |
196 | | - return err |
| 261 | + return fmt.Errorf("failed to delete alias: %w", err) |
197 | 262 | } |
198 | 263 |
|
199 | 264 | return nil |
|
0 commit comments