-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
70 lines (57 loc) · 1.32 KB
/
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
package mmailer
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
type Client struct {
url string
httpClient *http.Client
}
func NewClient(url string, key string) *Client {
return &Client{
url: url + "/send?key=" + key,
httpClient: http.DefaultClient,
}
}
func (c *Client) SetHttpClient(client *http.Client) {
c.httpClient = client
}
func (c *Client) Send(ctx context.Context, e Email) (resps []Response, err error) {
return c.SendWith(ctx, e, "")
}
func (c *Client) SendWith(ctx context.Context, e Email, service string) (resps []Response, err error) {
payload, err := json.Marshal(e)
if err != nil {
return nil, err
}
if c.httpClient == nil {
return nil, errors.New("no http client i available")
}
req, err := http.NewRequest("POST", c.url, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("content-type", "application/json")
if len(service) > 0 {
req.Header.Set("X-Service", service)
}
res, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("did not get 200 from mmailer, %s", string(body))
}
err = json.Unmarshal(body, &resps)
return resps, err
}