-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
267 lines (227 loc) · 7.38 KB
/
client.go
File metadata and controls
267 lines (227 loc) · 7.38 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// package mailpitclient provides a production-ready client for interacting with Mailpit API.
// Mailpit is a popular email testing tool that provides a REST API for managing emails.
package mailpitclient
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
// Client represents a Mailpit API client.
type Client interface {
// Message operations
ListMessages(ctx context.Context, opts *ListOptions) (*MessagesResponse, error)
GetMessage(ctx context.Context, id string) (*Message, error)
GetMessageSource(ctx context.Context, id string) (string, error)
GetMessageHeaders(ctx context.Context, id string) (map[string][]string, error)
GetMessageHTMLCheck(ctx context.Context, id string) (*HTMLCheckResponse, error)
GetMessageLinkCheck(ctx context.Context, id string) (*LinkCheckResponse, error)
GetMessageSpamAssassinCheck(ctx context.Context, id string) (*SpamAssassinCheckResponse, error)
GetMessagePart(ctx context.Context, messageID, partID string) ([]byte, error)
GetMessagePartThumbnail(ctx context.Context, messageID, partID string) ([]byte, error)
GetMessageAttachment(ctx context.Context, messageID, attachmentID string) ([]byte, error)
DeleteMessage(ctx context.Context, id string) error
DeleteAllMessages(ctx context.Context) error
MarkMessageRead(ctx context.Context, id string) error
MarkMessageUnread(ctx context.Context, id string) error
ReleaseMessage(ctx context.Context, id string, releaseData *ReleaseMessageRequest) error
SearchMessages(ctx context.Context, query string, opts *SearchOptions) (*MessagesResponse, error)
DeleteSearchResults(ctx context.Context, query string) error
// Send operations
SendMessage(ctx context.Context, message *SendMessageRequest) (*SendMessageResponse, error)
// Tags operations
GetTags(ctx context.Context) ([]string, error)
SetTags(ctx context.Context, tags []string) ([]string, error)
SetMessageTags(ctx context.Context, tag string, messageIDs []string) error
DeleteTag(ctx context.Context, tag string) error
// View operations
GetMessageHTML(ctx context.Context, id string) (string, error)
GetMessageText(ctx context.Context, id string) (string, error)
GetMessageRaw(ctx context.Context, id string) (string, error)
GetMessagePartHTML(ctx context.Context, messageID, partID string) (string, error)
GetMessagePartText(ctx context.Context, messageID, partID string) (string, error)
GetMessageEvents(ctx context.Context, id string) (*EventsResponse, error)
// Server operations
GetServerInfo(ctx context.Context) (*ServerInfo, error)
GetWebUIConfig(ctx context.Context) (*WebUIConfig, error)
HealthCheck(ctx context.Context) error
Ping(ctx context.Context) error
// Statistics
GetStats(ctx context.Context) (*Stats, error)
// Chaos testing operations
GetChaosConfig(ctx context.Context) (*ChaosResponse, error)
SetChaosConfig(ctx context.Context, config *ChaosTriggers) (*ChaosResponse, error)
// Utility methods
Close() error
}
// Config holds the configuration for the Mailpit client.
type Config struct {
HTTPClient *http.Client
BaseURL string
APIPath string
Username string
Password string
APIKey string
UserAgent string
Timeout time.Duration
MaxRetries int
RetryDelay time.Duration
}
// DefaultConfig returns a default configuration.
func DefaultConfig() *Config {
return &Config{
BaseURL: "http://localhost:8025",
APIPath: "/api/v1",
Timeout: 30 * time.Second,
UserAgent: "mailpit-go-client/1.0.0",
MaxRetries: 3,
RetryDelay: 1 * time.Second,
HTTPClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// client is the concrete implementation of the Client interface.
type client struct {
config *Config
baseURL *url.URL
apiURL string
userAgent string
}
// NewClient creates a new Mailpit client with the given configuration.
func NewClient(config *Config) (Client, error) {
if config == nil {
config = DefaultConfig()
}
// Validate configuration
if config.BaseURL == "" {
return nil, &Error{
Type: ErrorTypeConfig,
Message: "BaseURL is required",
}
}
baseURL, err := url.Parse(config.BaseURL)
if err != nil {
return nil, &Error{
Type: ErrorTypeConfig,
Message: fmt.Sprintf("invalid BaseURL: %v", err),
Cause: err,
}
}
if config.APIPath == "" {
config.APIPath = "/api/v1"
}
if config.HTTPClient == nil {
config.HTTPClient = &http.Client{
Timeout: config.Timeout,
}
}
if config.UserAgent == "" {
config.UserAgent = "mailpit-go-client/1.0.0"
}
apiURL := baseURL.String() + config.APIPath
return &client{
config: config,
baseURL: baseURL,
apiURL: apiURL,
userAgent: config.UserAgent,
}, nil
}
// Close closes the client and releases any resources.
func (c *client) Close() error {
// Close HTTP client if we own it
if transport, ok := c.config.HTTPClient.Transport.(*http.Transport); ok {
transport.CloseIdleConnections()
}
return nil
}
// makeRequest performs an HTTP request with proper error handling and retries.
//
//nolint:unparam
func (c *client) makeRequest(ctx context.Context, method, endpoint string, body io.Reader) (*http.Response, error) {
u := c.apiURL + endpoint
var lastErr error
for attempt := 0; attempt <= c.config.MaxRetries; attempt++ {
if attempt > 0 {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(c.config.RetryDelay):
}
}
req, err := http.NewRequestWithContext(ctx, method, u, body)
if err != nil {
return nil, &Error{
Type: ErrorTypeRequest,
Message: fmt.Sprintf("failed to create request: %v", err),
Cause: err,
}
}
// Set headers
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
// Add authentication if configured
if c.config.APIKey != "" {
req.Header.Set("Authorization", "Bearer "+c.config.APIKey)
} else if c.config.Username != "" && c.config.Password != "" {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
resp, err := c.config.HTTPClient.Do(req)
if err != nil {
lastErr = &Error{
Type: ErrorTypeNetwork,
Message: fmt.Sprintf("request failed: %v", err),
Cause: err,
}
continue
}
// Check for successful response
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
return resp, nil
}
// Handle HTTP errors
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
lastErr = &Error{
Type: ErrorTypeAPI,
Message: fmt.Sprintf("API request failed with status %d", resp.StatusCode),
StatusCode: resp.StatusCode,
Response: string(b),
}
// Don't retry on 4xx errors (except rate limiting)
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode < http.StatusInternalServerError && resp.StatusCode != http.StatusTooManyRequests {
break
}
}
return nil, lastErr
}
// parseResponse parses a JSON response into the given struct.
func (c *client) parseResponse(resp *http.Response, target any) error {
defer resp.Body.Close()
if target == nil {
return nil
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return &Error{
Type: ErrorTypeResponse,
Message: fmt.Sprintf("failed to read response body: %v", err),
Cause: err,
}
}
if len(body) == 0 {
return nil
}
if err = json.Unmarshal(body, target); err != nil {
return &Error{
Type: ErrorTypeResponse,
Message: fmt.Sprintf("failed to parse JSON response: %v", err),
Cause: err,
}
}
return nil
}