-
Notifications
You must be signed in to change notification settings - Fork 44
/
mailjet_client.go
50 lines (41 loc) · 1.2 KB
/
mailjet_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
package persistence
import (
"fmt"
"github.com/mailjet/mailjet-apiv3-go"
"github.com/singerdmx/BulletJournal/daemon/config"
"github.com/singerdmx/BulletJournal/daemon/logging"
"os"
)
type MailjetClient struct {
client *mailjet.Client
log logging.Logger
}
var mailjetClient MailjetClient
func (m *MailjetClient) SetLogger() {
m.log = *logging.GetLogger()
}
func (m *MailjetClient) SetMailjetClient(publicKey string, privateKey string) {
m.client = mailjet.NewMailjetClient(publicKey, privateKey)
if m.client == nil {
m.log.Errorf("failed to set mailjet client")
}
}
func (m *MailjetClient) NewClient() {
serviceConfig := *config.GetConfig()
mailjetClient.SetLogger()
mailjetClient.SetMailjetClient(os.Getenv(serviceConfig.ApiKeyPublic), os.Getenv(serviceConfig.ApiKeyPrivate))
}
func GetMailClient() (*mailjet.Client, error) {
if mailjetClient.client == nil {
mailjetClient.NewClient()
if mailjetClient.client == nil {
mailjetClient.log.Errorf("failed to get mailjet client")
return nil, &MailjetClientError{}
}
}
return mailjetClient.client, nil
}
type MailjetClientError struct {}
func (e *MailjetClientError) Error() string {
return fmt.Sprintf("mailjet client retrival error")
}