You've already forked email-api-client
65 lines
1.4 KiB
Go
Executable File
65 lines
1.4 KiB
Go
Executable File
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type EmailRequest struct {
|
|
Source string `json:"Source"`
|
|
Destination struct {
|
|
ToAddresses []string `json:"ToAddresses"`
|
|
CcAddresses []string `json:"CcAddresses"`
|
|
BccAddresses []string `json:"BccAddresses"`
|
|
} `json:"Destination"`
|
|
Message struct {
|
|
Body struct {
|
|
Html struct {
|
|
Data string `json:"Data"`
|
|
} `json:"Html,omitempty"`
|
|
Text struct {
|
|
Data string `json:"Data"`
|
|
} `json:"Text,omitempty"`
|
|
} `json:"Body"`
|
|
Subject struct {
|
|
Data string `json:"Data"`
|
|
} `json:"Subject"`
|
|
} `json:"Message"`
|
|
ScheduledTime time.Time `json:"ScheduledTime,omitempty"`
|
|
Catch bool `json:"Catch,omitempty"`
|
|
}
|
|
|
|
type EmailCreateResponse struct {
|
|
Id string `json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (client *Client) SendEmail(request *EmailRequest) (*EmailCreateResponse, error) {
|
|
jsonData, _ := json.Marshal(request)
|
|
|
|
httpClient := &http.Client{}
|
|
req, _ := http.NewRequest(
|
|
"POST",
|
|
client.config.Endpoint+"/api/email",
|
|
bytes.NewReader(jsonData),
|
|
)
|
|
req.Header.Set("Authorization", client.token.TokenType+" "+client.token.AccessToken)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := Response[EmailCreateResponse]{}
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &response.Payload, nil
|
|
}
|