Skip to content

Commit 3ff3c48

Browse files
author
Dirk Hoekstra
committed
Merge branch 'master' into json-requests
2 parents 1458ef1 + 7fd2300 commit 3ff3c48

File tree

9 files changed

+757
-119
lines changed

9 files changed

+757
-119
lines changed

client.go

Lines changed: 113 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,32 @@ import (
1818
"net/http"
1919
"net/url"
2020
"runtime"
21+
"strings"
2122
)
2223

2324
const (
2425
// ClientVersion is used in User-Agent request header to provide server with API level.
25-
ClientVersion = "3.0.0"
26+
ClientVersion = "4.2.0"
2627

2728
// Endpoint points you to MessageBird REST API.
2829
Endpoint = "https://rest.messagebird.com"
2930
)
3031

32+
const (
33+
// HLRPath represents the path to the HLR resource.
34+
HLRPath = "hlr"
35+
// MessagePath represents the path to the Message resource.
36+
MessagePath = "messages"
37+
// MMSPath represents the path to the MMS resource.
38+
MMSPath = "mms"
39+
// VoiceMessagePath represents the path to the VoiceMessage resource.
40+
VoiceMessagePath = "voicemessages"
41+
// VerifyPath represents the path to the Verify resource.
42+
VerifyPath = "verify"
43+
// LookupPath represents the path to the Lookup resource.
44+
LookupPath = "lookup"
45+
)
46+
3147
var (
3248
// ErrResponse is returned when we were able to contact API but request was not successful and contains error details.
3349
ErrResponse = errors.New("The MessageBird API returned an error")
@@ -138,7 +154,7 @@ func (c *Client) Balance() (*Balance, error) {
138154
// created by the NewHLR function.
139155
func (c *Client) HLR(id string) (*HLR, error) {
140156
hlr := &HLR{}
141-
if err := c.request(hlr, "hlr/"+id, nil); err != nil {
157+
if err := c.request(hlr, HLRPath+"/"+id, nil); err != nil {
142158
if err == ErrResponse {
143159
return hlr, err
144160
}
@@ -149,6 +165,21 @@ func (c *Client) HLR(id string) (*HLR, error) {
149165
return hlr, nil
150166
}
151167

168+
// HLRs lists all HLR objects that were previously created by the NewHLR
169+
// function.
170+
func (c *Client) HLRs() (*HLRList, error) {
171+
hlrList := &HLRList{}
172+
if err := c.request(hlrList, HLRPath, nil); err != nil {
173+
if err == ErrResponse {
174+
return hlrList, err
175+
}
176+
177+
return nil, err
178+
}
179+
180+
return hlrList, nil
181+
}
182+
152183
// NewHLR retrieves the information of an existing HLR.
153184
func (c *Client) NewHLR(msisdn string, reference string) (*HLR, error) {
154185
requestData, err := requestDataForHLR(msisdn, reference)
@@ -157,7 +188,8 @@ func (c *Client) NewHLR(msisdn string, reference string) (*HLR, error) {
157188
}
158189

159190
hlr := &HLR{}
160-
if err := c.request(hlr, "hlr", requestData); err != nil {
191+
192+
if err := c.request(hlr, HLRPath, requestData); err != nil {
161193
if err == ErrResponse {
162194
return hlr, err
163195
}
@@ -171,7 +203,7 @@ func (c *Client) NewHLR(msisdn string, reference string) (*HLR, error) {
171203
// Message retrieves the information of an existing Message.
172204
func (c *Client) Message(id string) (*Message, error) {
173205
message := &Message{}
174-
if err := c.request(message, "messages/"+id, nil); err != nil {
206+
if err := c.request(message, MessagePath+"/"+id, nil); err != nil {
175207
if err == ErrResponse {
176208
return message, err
177209
}
@@ -182,6 +214,25 @@ func (c *Client) Message(id string) (*Message, error) {
182214
return message, nil
183215
}
184216

217+
// Messages retrieves all messages of the user represented as a MessageList object.
218+
func (c *Client) Messages(msgListParams *MessageListParams) (*MessageList, error) {
219+
messageList := &MessageList{}
220+
params, err := paramsForMessageList(msgListParams)
221+
if err != nil {
222+
return messageList, err
223+
}
224+
225+
if err := c.request(messageList, MessagePath+"?"+params.Encode(), nil); err != nil {
226+
if err == ErrResponse {
227+
return messageList, err
228+
}
229+
230+
return nil, err
231+
}
232+
233+
return messageList, nil
234+
}
235+
185236
// NewMessage creates a new message for one or more recipients.
186237
func (c *Client) NewMessage(originator string, recipients []string, body string, msgParams *MessageParams) (*Message, error) {
187238
requestData, err := requestDataForMessage(originator, recipients, body, msgParams)
@@ -190,7 +241,7 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
190241
}
191242

192243
message := &Message{}
193-
if err := c.request(message, "messages", requestData); err != nil {
244+
if err := c.request(message, MessagePath, requestData); err != nil {
194245
if err == ErrResponse {
195246
return message, err
196247
}
@@ -201,10 +252,46 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
201252
return message, nil
202253
}
203254

255+
// MMSMessage retrieves the information of an existing MmsMessage.
256+
func (c *Client) MMSMessage(id string) (*MMSMessage, error) {
257+
mmsMessage := &MMSMessage{}
258+
if err := c.request(mmsMessage, MMSPath+"/"+id, nil); err != nil {
259+
if err == ErrResponse {
260+
return mmsMessage, err
261+
}
262+
263+
return nil, err
264+
}
265+
266+
return mmsMessage, nil
267+
}
268+
269+
// NewMMSMessage creates a new MMS message for one or more recipients.
270+
func (c *Client) NewMMSMessage(originator string, recipients []string, msgParams *MMSMessageParams) (*MMSMessage, error) {
271+
params, err := paramsForMMSMessage(msgParams)
272+
if err != nil {
273+
return nil, err
274+
}
275+
276+
params.Set("originator", originator)
277+
params.Set("recipients", strings.Join(recipients, ","))
278+
279+
mmsMessage := &MMSMessage{}
280+
if err := c.request(mmsMessage, MMSPath, params); err != nil {
281+
if err == ErrResponse {
282+
return mmsMessage, err
283+
}
284+
285+
return nil, err
286+
}
287+
288+
return mmsMessage, nil
289+
}
290+
204291
// VoiceMessage retrieves the information of an existing VoiceMessage.
205292
func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
206293
message := &VoiceMessage{}
207-
if err := c.request(message, "voicemessages/"+id, nil); err != nil {
294+
if err := c.request(message, VoiceMessagePath+"/"+id, nil); err != nil {
208295
if err == ErrResponse {
209296
return message, err
210297
}
@@ -215,6 +302,20 @@ func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
215302
return message, nil
216303
}
217304

305+
// VoiceMessages retrieves all VoiceMessages of the user.
306+
func (c *Client) VoiceMessages() (*VoiceMessageList, error) {
307+
messageList := &VoiceMessageList{}
308+
if err := c.request(messageList, VoiceMessagePath, nil); err != nil {
309+
if err == ErrResponse {
310+
return messageList, err
311+
}
312+
313+
return nil, err
314+
}
315+
316+
return messageList, nil
317+
}
318+
218319
// NewVoiceMessage creates a new voice message for one or more recipients.
219320
func (c *Client) NewVoiceMessage(recipients []string, body string, params *VoiceMessageParams) (*VoiceMessage, error) {
220321
requestData, err := requestDataForVoiceMessage(recipients, body, params)
@@ -223,7 +324,7 @@ func (c *Client) NewVoiceMessage(recipients []string, body string, params *Voice
223324
}
224325

225326
message := &VoiceMessage{}
226-
if err := c.request(message, "voicemessages", requestData); err != nil {
327+
if err := c.request(message, VoiceMessagePath, requestData); err != nil {
227328
if err == ErrResponse {
228329
return message, err
229330
}
@@ -242,7 +343,7 @@ func (c *Client) NewVerify(recipient string, params *VerifyParams) (*Verify, err
242343
}
243344

244345
verify := &Verify{}
245-
if err := c.request(verify, "verify", requestData); err != nil {
346+
if err := c.request(verify, VerifyPath, requestData); err != nil {
246347
if err == ErrResponse {
247348
return verify, err
248349
}
@@ -258,7 +359,7 @@ func (c *Client) VerifyToken(id, token string) (*Verify, error) {
258359
params := &url.Values{}
259360
params.Set("token", token)
260361

261-
path := "verify/" + id + "?" + params.Encode()
362+
path := VerifyPath + "/" + id + "?" + params.Encode()
262363

263364
verify := &Verify{}
264365
if err := c.request(verify, path, nil); err != nil {
@@ -275,7 +376,7 @@ func (c *Client) VerifyToken(id, token string) (*Verify, error) {
275376
// Lookup performs a new lookup for the specified number.
276377
func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, error) {
277378
urlParams := paramsForLookup(params)
278-
path := "lookup/" + phoneNumber + "?" + urlParams.Encode()
379+
path := LookupPath + "/" + phoneNumber + "?" + urlParams.Encode()
279380

280381
lookup := &Lookup{}
281382
if err := c.request(lookup, path, nil); err != nil {
@@ -292,7 +393,7 @@ func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, erro
292393
// NewLookupHLR creates a new HLR lookup for the specified number.
293394
func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
294395
requestData := requestDataForLookup(params)
295-
path := "lookup/" + phoneNumber + "/hlr"
396+
path := LookupPath + "/" + phoneNumber + "/" + HLRPath
296397

297398
hlr := &HLR{}
298399
if err := c.request(hlr, path, requestData); err != nil {
@@ -309,7 +410,7 @@ func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, e
309410
// LookupHLR performs a HLR lookup for the specified number.
310411
func (c *Client) LookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
311412
urlParams := paramsForLookup(params)
312-
path := "lookup/" + phoneNumber + "/hlr?" + urlParams.Encode()
413+
path := LookupPath + "/" + phoneNumber + "/" + HLRPath + "?" + urlParams.Encode()
313414

314415
hlr := &HLR{}
315416
if err := c.request(hlr, path, nil); err != nil {

hlr.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ type HLR struct {
2020
Errors []Error
2121
}
2222

23+
// HLRList represents a list of HLR requests.
24+
type HLRList struct {
25+
Offset int
26+
Limit int
27+
Count int
28+
TotalCount int
29+
Links map[string]*string
30+
Items []HLR
31+
}
32+
2333
type hlrRequest struct {
2434
MSISDN string `json:"msisdn"`
2535
Reference string `json:"reference"`

0 commit comments

Comments
 (0)