-
Notifications
You must be signed in to change notification settings - Fork 4
/
messages.go
87 lines (71 loc) · 2.05 KB
/
messages.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
package mailersend
import (
"context"
"fmt"
"net/http"
"time"
)
const messageBasePath = "/messages"
type MessageService service
// messageRoot format of message response
type messageRoot struct {
Data []message `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
type message struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type singleMessageRoot struct {
Data singleMessage `json:"data"`
}
type singleMessage struct {
ID string `json:"id"`
Emails []email `json:"emails"`
Domain Domain `json:"domain"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type email struct {
ID string `json:"id"`
From string `json:"from"`
Subject string `json:"subject,omitempty"`
Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
Tags []string `json:"tags,omitempty"`
Status string `json:"status,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// ListMessageOptions - modifies the behavior of MessageService.List Method
type ListMessageOptions struct {
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
func (s *MessageService) List(ctx context.Context, options *ListMessageOptions) (*messageRoot, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, messageBasePath, options)
if err != nil {
return nil, nil, err
}
root := new(messageRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *MessageService) Get(ctx context.Context, messageID string) (*singleMessageRoot, *Response, error) {
path := fmt.Sprintf("%s/%s", messageBasePath, messageID)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(singleMessageRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}