@@ -17,8 +17,8 @@ import (
17
17
)
18
18
19
19
const (
20
- CLIENT_VERSION = "1.0.0 "
21
- ENDPOINT = "https://rest.messagebird.com"
20
+ ClientVersion = "1.0.1 "
21
+ Endpoint = "https://rest.messagebird.com"
22
22
)
23
23
24
24
type Recipient struct {
@@ -97,20 +97,24 @@ type Client struct {
97
97
AccessKey string
98
98
}
99
99
100
+ // Create a new Client.
101
+ func New (AccessKey string ) * Client {
102
+ return & Client {AccessKey }
103
+ }
104
+
100
105
// This function performs a call to MessageBird's HTTP API and expects JSON in
101
106
// return. It then tries to unmarshal the JSON body of the response to the
102
107
// specified struct.
103
108
func (c Client ) request (v interface {}, path string , params * url.Values ) error {
104
109
var request * http.Request
105
110
106
111
// Construct the URI of the request.
107
- uri , err := url .Parse (ENDPOINT + "/" + path )
112
+ uri , err := url .Parse (Endpoint + "/" + path )
108
113
if err != nil {
109
114
return err
110
115
}
111
116
112
- // Create an http.Client and construct a new request.
113
- client := & http.Client {}
117
+ // Construct a new request.
114
118
if params == nil {
115
119
request , err = http .NewRequest ("GET" , uri .String (), nil )
116
120
} else {
@@ -123,14 +127,15 @@ func (c Client) request(v interface{}, path string, params *url.Values) error {
123
127
// Add a basic set of headers to the request.
124
128
request .Header .Add ("Accept" , "application/json" )
125
129
request .Header .Add ("Authorization" , "AccessKey " + c .AccessKey )
126
- request .Header .Add ("User-Agent" , "MessageBird/ApiClient/" + CLIENT_VERSION + " Go/" + runtime .Version ())
130
+ request .Header .Add ("User-Agent" , "MessageBird/ApiClient/" + ClientVersion + " Go/" + runtime .Version ())
127
131
128
132
// Add the Content-Type header if this is a POST request.
129
133
if params != nil {
130
134
request .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
131
135
}
132
136
133
- // Execute the HTTP request and wait for a response.
137
+ // Create an http.Client, execute the HTTP request and wait for a response.
138
+ client := & http.Client {}
134
139
response , err := client .Do (request )
135
140
if err != nil {
136
141
return err
@@ -146,8 +151,7 @@ func (c Client) request(v interface{}, path string, params *url.Values) error {
146
151
}
147
152
148
153
// Convert the JSON body to the specified struct.
149
- err = json .Unmarshal (body , v )
150
- if err != nil {
154
+ if err = json .Unmarshal (body , v ); err != nil {
151
155
return err
152
156
}
153
157
@@ -158,8 +162,7 @@ func (c Client) request(v interface{}, path string, params *url.Values) error {
158
162
func (c Client ) Balance () (* Balance , error ) {
159
163
balance := & Balance {}
160
164
161
- err := c .request (balance , "balance" , nil )
162
- if err != nil {
165
+ if err := c .request (balance , "balance" , nil ); err != nil {
163
166
return nil , err
164
167
}
165
168
@@ -170,8 +173,7 @@ func (c Client) Balance() (*Balance, error) {
170
173
func (c Client ) HLR (id string ) (* HLR , error ) {
171
174
hlr := & HLR {}
172
175
173
- err := c .request (hlr , "hlr/" + id , nil )
174
- if err != nil {
176
+ if err := c .request (hlr , "hlr/" + id , nil ); err != nil {
175
177
return nil , err
176
178
}
177
179
@@ -186,8 +188,7 @@ func (c Client) CreateHLR(msisdn string, reference string) (*HLR, error) {
186
188
187
189
hlr := & HLR {}
188
190
189
- err := c .request (hlr , "hlr" , params )
190
- if err != nil {
191
+ if err := c .request (hlr , "hlr" , params ); err != nil {
191
192
return nil , err
192
193
}
193
194
@@ -198,16 +199,14 @@ func (c Client) CreateHLR(msisdn string, reference string) (*HLR, error) {
198
199
func (c Client ) Message (id string ) (* Message , error ) {
199
200
msg := & Message {}
200
201
201
- err := c .request (msg , "messages/" + id , nil )
202
- if err != nil {
202
+ if err := c .request (msg , "messages/" + id , nil ); err != nil {
203
203
return nil , err
204
204
}
205
205
206
206
return msg , nil
207
207
}
208
208
209
- // This function creates a new message, which is sent to one or more
210
- // recipients.
209
+ // This function creates a new message, which is sent to one or more recipients.
211
210
func (c Client ) CreateMessage (originator string , recipients []string , body string , params * url.Values ) (* Message , error ) {
212
211
recips := strings .Join (recipients , "," )
213
212
@@ -224,25 +223,25 @@ func (c Client) CreateMessage(originator string, recipients []string, body strin
224
223
225
224
msg := & Message {}
226
225
227
- err := c .request (msg , "messages" , params )
228
- if err != nil {
226
+ if err := c .request (msg , "messages" , params ); err != nil {
229
227
return nil , err
230
228
}
231
229
232
230
return msg , nil
233
231
}
234
232
233
+ // This function retrieves the information of a specific voice message.
235
234
func (c Client ) VoiceMessage (id string ) (* VoiceMessage , error ) {
236
235
vmsg := & VoiceMessage {}
237
236
238
- err := c .request (vmsg , "voicemessages/" + id , nil )
239
- if err != nil {
237
+ if err := c .request (vmsg , "voicemessages/" + id , nil ); err != nil {
240
238
return nil , err
241
239
}
242
240
243
241
return vmsg , nil
244
242
}
245
243
244
+ // This function creates a new voice message
246
245
func (c Client ) CreateVoiceMessage (recipients []string , body string , params * url.Values ) (* VoiceMessage , error ) {
247
246
recips := strings .Join (recipients , "," )
248
247
@@ -257,8 +256,7 @@ func (c Client) CreateVoiceMessage(recipients []string, body string, params *url
257
256
258
257
vmsg := & VoiceMessage {}
259
258
260
- err := c .request (vmsg , "voicemessages" , params )
261
- if err != nil {
259
+ if err := c .request (vmsg , "voicemessages" , params ); err != nil {
262
260
return nil , err
263
261
}
264
262
0 commit comments