-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviews.go
More file actions
165 lines (134 loc) · 3.9 KB
/
views.go
File metadata and controls
165 lines (134 loc) · 3.9 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
package mailpitclient
import (
"context"
"fmt"
"io"
"net/http"
)
// GetMessageHTML retrieves the HTML view of a specific message.
func (c *client) GetMessageHTML(ctx context.Context, id string) (string, error) {
if id == "" {
return "", NewValidationError("message ID cannot be empty")
}
endpoint := fmt.Sprintf("/view/%s.html", id)
resp, err := c.makeRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", &Error{
Type: ErrorTypeResponse,
Message: fmt.Sprintf("failed to read HTML content: %v", err),
Cause: err,
}
}
return string(body), nil
}
// GetMessageText retrieves the text view of a specific message.
func (c *client) GetMessageText(ctx context.Context, id string) (string, error) {
if id == "" {
return "", NewValidationError("message ID cannot be empty")
}
endpoint := fmt.Sprintf("/view/%s.txt", id)
resp, err := c.makeRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", &Error{
Type: ErrorTypeResponse,
Message: fmt.Sprintf("failed to read text content: %v", err),
Cause: err,
}
}
return string(body), nil
}
// GetMessageRaw retrieves the raw message source.
func (c *client) GetMessageRaw(ctx context.Context, id string) (string, error) {
if id == "" {
return "", NewValidationError("message ID cannot be empty")
}
endpoint := fmt.Sprintf("/view/%s.raw", id)
resp, err := c.makeRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", &Error{
Type: ErrorTypeResponse,
Message: fmt.Sprintf("failed to read raw content: %v", err),
Cause: err,
}
}
return string(body), nil
}
// GetMessagePartHTML retrieves the HTML version of a specific message part.
func (c *client) GetMessagePartHTML(ctx context.Context, messageID, partID string) (string, error) {
if messageID == "" {
return "", NewValidationError("message ID cannot be empty")
}
if partID == "" {
return "", NewValidationError("part ID cannot be empty")
}
endpoint := fmt.Sprintf("/view/%s/part/%s.html", messageID, partID)
resp, err := c.makeRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", &Error{
Type: ErrorTypeResponse,
Message: fmt.Sprintf("failed to read part HTML content: %v", err),
Cause: err,
}
}
return string(body), nil
}
// GetMessagePartText retrieves the text version of a specific message part.
func (c *client) GetMessagePartText(ctx context.Context, messageID, partID string) (string, error) {
if messageID == "" {
return "", NewValidationError("message ID cannot be empty")
}
if partID == "" {
return "", NewValidationError("part ID cannot be empty")
}
endpoint := fmt.Sprintf("/view/%s/part/%s.text", messageID, partID)
resp, err := c.makeRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", &Error{
Type: ErrorTypeResponse,
Message: fmt.Sprintf("failed to read part text content: %v", err),
Cause: err,
}
}
return string(body), nil
}
// GetMessageEvents retrieves events for a specific message.
func (c *client) GetMessageEvents(ctx context.Context, id string) (*EventsResponse, error) {
if id == "" {
return nil, NewValidationError("message ID cannot be empty")
}
endpoint := fmt.Sprintf("/message/%s/events", id)
resp, err := c.makeRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
var events EventsResponse
if err = c.parseResponse(resp, &events); err != nil {
return nil, err
}
return &events, nil
}