|
| 1 | +package emailgateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type priority string |
| 12 | + |
| 13 | +const ( |
| 14 | + PriorityLow priority = "LOW" |
| 15 | + PriorityHigh priority = "HIGH" |
| 16 | + |
| 17 | + baseURL = "https://api.cm.com/email/gateway/v1" |
| 18 | + defaultHttpTimeout = 30 * time.Second |
| 19 | + defaultTransactionalPriority = PriorityHigh |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + ErrUnableToSendEmail = errors.New("unable to send email") |
| 24 | +) |
| 25 | + |
| 26 | +type Client struct { |
| 27 | + baseClient *http.Client |
| 28 | + productToken string |
| 29 | + transactionalPriority priority |
| 30 | +} |
| 31 | + |
| 32 | +type Config struct { |
| 33 | + ProductToken string |
| 34 | + CustomHttpClient *http.Client |
| 35 | + DefaultTransactionalPriority priority |
| 36 | +} |
| 37 | + |
| 38 | +func NewClient(config Config) (*Client, error) { |
| 39 | + var client Client |
| 40 | + |
| 41 | + if config.ProductToken == "" { |
| 42 | + return nil, errors.New("config.ProductToken must be set") |
| 43 | + } |
| 44 | + client.productToken = config.ProductToken |
| 45 | + |
| 46 | + client.baseClient = &http.Client{Timeout: defaultHttpTimeout} |
| 47 | + if config.CustomHttpClient != nil { |
| 48 | + client.baseClient = config.CustomHttpClient |
| 49 | + } |
| 50 | + |
| 51 | + client.transactionalPriority = defaultTransactionalPriority |
| 52 | + if config.DefaultTransactionalPriority != "" { |
| 53 | + client.transactionalPriority = config.DefaultTransactionalPriority |
| 54 | + } |
| 55 | + |
| 56 | + return &client, nil |
| 57 | +} |
| 58 | + |
| 59 | +type SendEmailResponse struct { |
| 60 | + Status int `json:"status"` |
| 61 | + Message string `json:"message"` |
| 62 | + Success bool `json:"success"` |
| 63 | + MessageID string `json:"messageId"` |
| 64 | +} |
| 65 | + |
| 66 | +func (c *Client) SendTransactionalEmail(email Email) (*SendEmailResponse, error) { |
| 67 | + emailPriority := c.transactionalPriority |
| 68 | + if email.Priority != "" { |
| 69 | + emailPriority = email.Priority |
| 70 | + } |
| 71 | + |
| 72 | + mailBody, err := json.Marshal(email) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + req, err := http.NewRequest(http.MethodPost, baseURL+"/transactional?priority="+string(emailPriority), bytes.NewBuffer(mailBody)) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + req.Header.Set("Content-Type", "application/json") |
| 83 | + req.Header.Set("X-CM-PRODUCTTOKEN", c.productToken) |
| 84 | + |
| 85 | + resp, err := c.baseClient.Do(req) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + defer resp.Body.Close() |
| 90 | + |
| 91 | + var response SendEmailResponse |
| 92 | + _ = json.NewDecoder(resp.Body).Decode(&response) |
| 93 | + |
| 94 | + if resp.StatusCode != http.StatusAccepted { |
| 95 | + return &response, ErrUnableToSendEmail |
| 96 | + } |
| 97 | + |
| 98 | + return &response, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (c *Client) SendMarketingEmail(email Email) (*SendEmailResponse, error) { |
| 102 | + mailBody, err := json.Marshal(email) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + req, err := http.NewRequest(http.MethodPost, baseURL+"/transactional", bytes.NewBuffer(mailBody)) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + req.Header.Set("Content-Type", "application/json") |
| 113 | + req.Header.Set("X-CM-PRODUCTTOKEN", c.productToken) |
| 114 | + |
| 115 | + resp, err := c.baseClient.Do(req) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + defer resp.Body.Close() |
| 120 | + |
| 121 | + var response SendEmailResponse |
| 122 | + _ = json.NewDecoder(resp.Body).Decode(&response) |
| 123 | + |
| 124 | + if resp.StatusCode != http.StatusAccepted { |
| 125 | + return &response, ErrUnableToSendEmail |
| 126 | + } |
| 127 | + |
| 128 | + return &response, nil |
| 129 | +} |
0 commit comments