forked from mailjet/mailjet-apiv3-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailjet_client.go
169 lines (140 loc) · 4.8 KB
/
mailjet_client.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
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
// Package mailjet provides methods for interacting with the last version of the Mailjet API.
// The goal of this component is to simplify the usage of the MailJet API for GO developers.
//
// For more details, see the full API Documentation at http://dev.mailjet.com/
package mailjet
import (
"net/http"
"strings"
"io"
"os"
"log"
)
// NewMailjetClient returns a new MailjetClient using an public apikey
// and an secret apikey to be used when authenticating to API.
func NewMailjetClient(apiKeyPublic, apiKeyPrivate string, baseUrl ...string) *Client {
mj := &httpClient{
apiKeyPublic: apiKeyPublic,
apiKeyPrivate: apiKeyPrivate,
client: http.DefaultClient,
}
if len(baseUrl) > 0 {
return &Client{client: mj, apiBase:baseUrl[0]}
}
return &Client{client: mj, apiBase:apiBase}
}
// APIKeyPublic returns the public key.
func (c *Client) APIKeyPublic() string {
return c.client.APIKeyPublic()
}
// APIKeyPrivate returns the secret key.
func (c *Client) APIKeyPrivate() string {
return c.client.APIKeyPrivate()
}
func (c *Client) Client() *http.Client {
return c.client.Client()
}
// SetClient allows to customize http client.
func (c *Client) SetClient(client *http.Client) {
c.client.SetClient(client)
}
// Filter applies a filter with the defined key and value.
func Filter(key, value string) RequestOptions {
return func(req *http.Request) {
q := req.URL.Query()
q.Add(key, value)
req.URL.RawQuery = strings.Replace(q.Encode(), "%2B", "+", 1)
}
}
// SortOrder defines the order of the result.
type SortOrder int
// These are the two possible order.
const (
SortDesc = SortOrder(iota)
SortAsc
)
var debugOut io.Writer = os.Stderr
// SetDebugOutput sets the output destination for the debug.
func SetDebugOutput(w io.Writer) {
debugOut = w
log.SetOutput(w)
}
// Sort applies the Sort filter to the request.
func Sort(value string, order SortOrder) RequestOptions {
if order == SortDesc {
value = value + "+DESC"
}
return Filter("Sort", value)
}
// List issues a GET to list the specified resource
// and stores the result in the value pointed to by res.
// Filters can be add via functional options.
func (c *Client) List(resource string, resp interface{}, options ...RequestOptions) (count, total int, err error) {
url := buildURL(c.apiBase, &Request{Resource: resource})
req, err := createRequest("GET", url, nil, nil, options...)
if err != nil {
return count, total, err
}
return c.client.Send(req).Read(resp).Call()
}
// Get issues a GET to view a resource specifying an id
// and stores the result in the value pointed to by res.
// Filters can be add via functional options.
// Without an specified ID in MailjetRequest, it is the same as List.
func (mj *Client) Get(mr *Request, resp interface{}, options ...RequestOptions) (err error) {
url := buildURL(mj.apiBase, mr)
req, err := createRequest("GET", url, nil, nil, options...)
if err != nil {
return err
}
_, _, err = mj.client.Send(req).Read(resp).Call()
return err
}
// Post issues a POST to create a new resource
// and stores the result in the value pointed to by res.
// Filters can be add via functional options.
func (mj *Client) Post(fmr *FullRequest, resp interface{}, options ...RequestOptions) (err error) {
url := buildURL(mj.apiBase, fmr.Info)
req, err := createRequest("POST", url, fmr.Payload, nil, options...)
if err != nil {
return err
}
headers := map[string]string{"Content-Type": "application/json"}
_,_, err = mj.client.Send(req).With(headers).Read(resp).Call()
return err
}
// Put is used to update a resource.
// Fields to be updated must be specified by the string array onlyFields.
// If onlyFields is nil, all fields except these with the tag read_only, are updated.
// Filters can be add via functional options.
func (mj *Client) Put(fmr *FullRequest, onlyFields []string, options ...RequestOptions) (err error) {
url := buildURL(mj.apiBase, fmr.Info)
req, err := createRequest("PUT", url, fmr.Payload, onlyFields, options...)
if err != nil {
return err
}
headers := map[string]string{"Content-Type": "application/json"}
_, _, err = mj.client.Send(req).With(headers).Call()
return err
}
// Delete is used to delete a resource.
func (mj *Client) Delete(mr *Request) (err error) {
url := buildURL(mj.apiBase, mr)
req, err := createRequest("DELETE", url, nil, nil)
if err != nil {
return err
}
_, _, err = mj.client.Send(req).Call()
return err
}
// SendMail send mail via API.
func (mj *Client) SendMail(data *InfoSendMail) (res *SentResult, err error) {
url := mj.apiBase + "/send/message"
req, err := createRequest("POST", url, data, nil)
if err != nil {
return res, err
}
headers := map[string]string{"Content-Type": "application/json"}
_, _, err = mj.client.Send(req).With(headers).Read(&res).Call()
return res, err
}