forked from sendgrid/sendgrid-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendgrid.go
54 lines (46 loc) · 1.48 KB
/
sendgrid.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
// Package sendgrid provides a simple interface to interact with the SendGrid API
package sendgrid
import (
"github.com/sendgrid/rest" // depends on version 2.2.0
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
// Version is this client library's current version
const Version = "3.1.0"
// Client contains rest.Request to an a API call
type Client struct {
rest.Request
}
// GetRequest returns a default request object.
func GetRequest(key string, endpoint string, host string) rest.Request {
if host == "" {
host = "https://api.sendgrid.com"
}
baseURL := host + endpoint
requestHeaders := map[string]string{
"Authorization": "Bearer " + key,
"User-Agent": "sendgrid/" + Version + ";go",
"Accept": "application/json",
}
request := rest.Request{
BaseURL: baseURL,
Headers: requestHeaders,
}
return request
}
// Send gets API rest.Response to the give request body of type SGMailV3
func (cl *Client) Send(email *mail.SGMailV3) (*rest.Response, error) {
cl.Body = mail.GetRequestBody(email)
return API(cl.Request)
}
// NewSendClient create a new Client for a given key
func NewSendClient(key string) *Client {
request := GetRequest(key, "/v3/mail/send", "")
request.Method = "POST"
return &Client{request}
}
// DefaultClient is used if no custom HTTP client is defined
var DefaultClient = rest.DefaultClient
// API sets up the request to the SendGrid API, this is main interface.
func API(request rest.Request) (*rest.Response, error) {
return DefaultClient.API(request)
}