-
Notifications
You must be signed in to change notification settings - Fork 1
/
textlocal.go
167 lines (129 loc) · 3.84 KB
/
textlocal.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package textlocal
import (
"errors"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"net/url"
)
// DefaultBaseUrl is the default TextLocal API gateway.
const DefaultBaseUrl = "https://api.txtlocal.com"
// ErrEmptyBaseUrl empty base URL can not be set.
var ErrEmptyBaseUrl = errors.New("You must set a base url. Use textlocal.DefaultBaseUrl to use default.")
// ErrNoApiKeyProvided no API key has been provided.
var ErrNoApiKeyProvided = errors.New("You must provide an API key.")
// ErrUnknown when an unknown error is returned.
var ErrUnknown = errors.New("API error, unknown.");
// ErrCommandNotValid when a command is not found on API endpoint.
var ErrCommandNotValid = errors.New("API error, unknown command.");
// ErrInvalidApiKey when a response comes back with API key error.
var ErrInvalidApiKey = errors.New("API error, invalid api key provided.");
const (
_ = iota
noCommandSpecifiedErrorCode
unrecognisedCommandErrorCode
invalidLoginDetailsErrorCode
)
func getErrorFromResponse(b map[string]interface{}) error {
if e, ok := b["error"].([]map[string]interface{}); ok {
if len(e) > 0 {
te := e[0]
switch te["code"].(int) {
case noCommandSpecifiedErrorCode:
return ErrCommandNotValid
case unrecognisedCommandErrorCode:
return ErrCommandNotValid
case invalidLoginDetailsErrorCode:
return ErrInvalidApiKey
}
}
}
return ErrUnknown
}
// GetCreditsResponse contains how many credits remain.
type GetCreditsResponse struct {
RemainingSMS int
RemainingMMS int
}
// Connection for connecting to TextLocal
type Connection struct {
baseUrl string
apiKey string
client *http.Client
}
// New connection.
func New(baseUrl, apiKey string) (*Connection, error) {
return NewWithCustomHttpClient(baseUrl, apiKey, http.DefaultClient)
}
// New connection.
func NewWithCustomHttpClient(baseUrl, apiKey string, client *http.Client) (*Connection, error) {
if len(baseUrl) == 0 {
return &Connection{}, ErrEmptyBaseUrl
}
if len(apiKey) == 0 {
return &Connection{}, ErrNoApiKeyProvided
}
return &Connection{
baseUrl: strings.TrimRight(baseUrl, "/"),
apiKey: apiKey,
client: client,
}, nil
}
// GetBaseUrl returns the base url used to construct the connection.
func (c *Connection) GetBaseUrl() string {
return c.baseUrl
}
// GetCredits will tell you amount of SMS and MMS credits available on the account.
func (c *Connection) GetCredits() (GetCreditsResponse, error) {
res, err := c.client.Get(c.baseUrl + "/balance?apiKey=" + c.apiKey)
if err != nil {
return GetCreditsResponse{}, err
}
content, err := ioutil.ReadAll(res.Body)
if err != nil {
return GetCreditsResponse{}, err
}
var payload map[string]interface{}
if err = json.Unmarshal(content, &payload); err != nil {
return GetCreditsResponse{}, err
}
if payload["status"].(string) != "success" {
return GetCreditsResponse{}, getErrorFromResponse(payload)
}
rS, rM := 0, 0
if ba, ok := payload["balance"].(map[string]interface{}); ok {
rS = int(ba["sms"].(float64))
rM = int(ba["mms"].(float64))
}
return GetCreditsResponse{
RemainingSMS: rS,
RemainingMMS: rM,
}, nil
}
// SendSMS will send a message to a list of numbers.
func (c *Connection) SendSMS(n []string, m string, f string) (error) {
form := url.Values{}
form.Add("apiKey", c.apiKey)
form.Add("numbers", strings.Join(n, ","))
form.Add("message", m)
form.Add("sender", f)
req, err := http.NewRequest("POST", c.baseUrl + "/send", strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err := c.client.Do(req)
if err != nil {
return err
}
content, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
var payload map[string]interface{}
if err = json.Unmarshal(content, &payload); err != nil {
return err
}
if payload["status"].(string) != "success" {
return getErrorFromResponse(payload)
}
return nil
}