-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
74 lines (66 loc) · 1.67 KB
/
Copy pathclient.go
File metadata and controls
74 lines (66 loc) · 1.67 KB
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
74
package wavecell
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
const (
//SingleMessagePath for sending a single message
SingleMessagePath = "sms/v1/"
)
// HTTPInterface helps wavecell tests
type HTTPInterface interface {
Do(req *http.Request) (*http.Response, error)
}
// Client manages requests to wavecell
type Client struct {
BaseURL string
AuthKey string
ClientID string
HTTPClient HTTPInterface
}
func ClientWithAuthKey(key, clientID string) *Client {
return &Client{
BaseURL: "https://api.wavecell.com/",
ClientID: clientID,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
AuthKey: key,
}
}
// SingleMessage sends one message to one recipient
func (c Client) SingleMessage(m Message) (r Response, err error) {
if err = m.Validate(); err != nil {
return
}
b, err := json.Marshal(m)
if err != nil {
return
}
r, err = c.defaultRequest(b, SingleMessagePath+c.ClientID+"/single")
return
}
func (c Client) defaultRequest(b []byte, path string) (r Response, err error) {
req, err := http.NewRequest(http.MethodPost, c.BaseURL+path, bytes.NewBuffer(b))
if err != nil {
return
}
if c.AuthKey != "" {
req.Header.Add("Authorization", "Bearer "+c.AuthKey)
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
err = json.NewDecoder(resp.Body).Decode(&r)
return
}
bEr, _ := ioutil.ReadAll(resp.Body)
err = fmt.Errorf("received status code: %d, error: %s,for more information on this error refer to this link:https://developer.wavecell.com/v1/sms-api/api-send-sms", resp.StatusCode, bEr)
return
}