-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
73 lines (59 loc) · 1.44 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
67
68
69
70
71
72
73
// Copyright 2021 TFCloud Co.,Ltd. All rights reserved.
// This source code is licensed under Apache-2.0 license
// that can be found in the LICENSE file.
package messaging
import (
"bytes"
"encoding/json"
"errors"
"net/http"
)
type Messenger interface {
SendSMS(to []string, message string) error
SendEmail(to []string, message, subject string) error
}
type Options struct {
URL string
SMSReceiver string
EmailReceiver string
}
type client struct {
Options
}
func NewClient(opts Options) (Messenger, error) {
if opts.URL == "" {
err := errors.New("client url is required.")
return nil, err
}
if opts.SMSReceiver == "" {
opts.SMSReceiver = "sms"
}
if opts.EmailReceiver == "" {
opts.EmailReceiver = "email"
}
return &client{opts}, nil
}
func (c *client) send(t string, to []string, message, subject string) error {
url := c.URL + "/notify"
d := map[string]interface{}{
"to": to,
"message": message,
}
switch t {
case "sms":
d["receiver"] = c.SMSReceiver
case "email":
d["receiver"] = c.EmailReceiver
d["subj"] = subject
}
payload, _ := json.Marshal(d)
// TODO(william): check response data
_, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
return err
}
func (c *client) SendSMS(to []string, message string) error {
return c.send("sms", to, message, "")
}
func (c *client) SendEmail(to []string, message, subject string) error {
return c.send("email", to, message, subject)
}