forked from riobard/go-mailgun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
66 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
/*
Mailgun client in Go.
*/
package mailgun
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type Client struct {
httpClient *http.Client
key string
apiVersion int
apiEndpoint string
}
func New(key string) *Client {
return &Client{httpClient: &http.Client{}, key: key, apiVersion: 2, apiEndpoint: "api.mailgun.net"}
}
func (c *Client) SetEndpoint(str string) {
c.apiEndpoint = str
}
// make an api request
func (c *Client) api(method string, path string, fields url.Values) (body []byte, err error) {
var req *http.Request
url := fmt.Sprintf("https://%s/v%d%s", c.apiEndpoint, c.apiVersion, path)
if method == "POST" && fields != nil {
req, err = http.NewRequest(method, url, strings.NewReader(fields.Encode()))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
} else {
if fields != nil {
url += "?" + fields.Encode()
}
req, err = http.NewRequest(method, url, nil)
}
if err != nil {
return
}
req.SetBasicAuth("api", c.key)
rsp, err := c.httpClient.Do(req)
if err != nil {
return
}
defer rsp.Body.Close()
body, err = ioutil.ReadAll(rsp.Body)
if err != nil {
return
}
if rsp.StatusCode < 200 || rsp.StatusCode >= 300 {
err = fmt.Errorf("mailgun error: %d %s", rsp.StatusCode, body)
}
return
}