Skip to content

Commit bd439f8

Browse files
author
Marcel Corso
committed
add new lookup endpoints
1 parent cc0ae25 commit bd439f8

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

client.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package messagebird
99
import (
1010
"encoding/json"
1111
"errors"
12+
"fmt"
1213
"io/ioutil"
1314
"log"
1415
"net/http"
@@ -263,3 +264,68 @@ func (c *Client) OtpVerify(recipient string, token string, params *OtpParams) (*
263264

264265
return message, nil
265266
}
267+
268+
// Lookup performs a new lookup for the specified number
269+
func (c *Client) Lookup(phoneNumber int, countryCode string) (*Lookup, error) {
270+
urlParams := &url.Values{}
271+
if countryCode != "" {
272+
urlParams.Set("countryCode", countryCode)
273+
}
274+
275+
path := fmt.Sprintf("lookup/%d?%s", phoneNumber, urlParams.Encode())
276+
277+
lookup := &Lookup{}
278+
if err := c.request(lookup, path, nil); err != nil {
279+
if err == ErrResponse {
280+
return lookup, err
281+
}
282+
283+
return nil, err
284+
}
285+
286+
return lookup, nil
287+
}
288+
289+
// NewHLRLookup creates a new HRL lookup for the specified number
290+
func (c *Client) NewHLRLookup(phoneNumber int, countryCode string, reference string) (*HLR, error) {
291+
params := &url.Values{}
292+
if countryCode != "" {
293+
params.Set("countryCode", countryCode)
294+
}
295+
if reference != "" {
296+
params.Set("reference", reference)
297+
}
298+
299+
hlr := &HLR{}
300+
path := fmt.Sprintf("lookup/%d/hlr", phoneNumber)
301+
if err := c.request(hlr, path, params); err != nil {
302+
if err == ErrResponse {
303+
return hlr, err
304+
}
305+
306+
return nil, err
307+
}
308+
309+
return hlr, nil
310+
}
311+
312+
// HLRLookup performs a HLR lookup for the specified number
313+
func (c *Client) HLRLookup(phoneNumber int, countryCode string) (*HLR, error) {
314+
urlParams := &url.Values{}
315+
if countryCode != "" {
316+
urlParams.Set("countryCode", countryCode)
317+
}
318+
319+
path := fmt.Sprintf("lookup/%d/hlr?%s", phoneNumber, urlParams.Encode())
320+
321+
hlr := &HLR{}
322+
if err := c.request(hlr, path, nil); err != nil {
323+
if err == ErrResponse {
324+
return hlr, err
325+
}
326+
327+
return nil, err
328+
}
329+
330+
return hlr, nil
331+
}

lookup.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package messagebird
2+
3+
type Formats struct {
4+
E164 string
5+
International string
6+
National string
7+
Rfc3966 string
8+
}
9+
10+
type Lookup struct {
11+
Href string
12+
CountryCode string
13+
CountryPrefix int
14+
PhoneNumber int
15+
Type string
16+
Formats *Formats
17+
HLR *HLR
18+
}

lookup_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package messagebird
2+
3+
import "testing"
4+
5+
var lookupObject []byte = []byte(`{
6+
"href":"https://rest.messagebird.com/lookup/31624971134",
7+
"countryCode":"NL",
8+
"countryPrefix":31,
9+
"phoneNumber":31624971134,
10+
"type":"mobile",
11+
"formats":{
12+
"e164":"+31624971134",
13+
"international":"+31 6 24971134",
14+
"national":"06 24971134",
15+
"rfc3966":"tel:+31-6-24971134"
16+
},
17+
"hlr":{
18+
"id":"6118d3f06566fcd0cdc8962h65065907",
19+
"network":20416,
20+
"reference":"yoloswag2000",
21+
"status":"active",
22+
"createdDatetime":"2015-12-15T08:19:24+00:00",
23+
"statusDatetime":"2015-12-15T08:19:25+00:00"
24+
}
25+
}`)
26+
27+
var lookupHLRObject []byte = []byte(`{
28+
"id":"6118d3f06566fcd0cdc8962h65065907",
29+
"network":20416,
30+
"reference":"yoloswag2000",
31+
"status":"active",
32+
"createdDatetime":"2015-12-15T08:19:24+00:00",
33+
"statusDatetime":"2015-12-15T08:19:25+00:00"
34+
}`)
35+
36+
func TestLookup(t *testing.T) {
37+
SetServerResponse(200, lookupObject)
38+
39+
phoneNumber := 31624971134
40+
lookup, err := mbClient.Lookup(phoneNumber, "NL")
41+
if err != nil {
42+
t.Fatalf("Didn't expect error while doing the lookup: %s", err)
43+
}
44+
45+
if lookup.Href != "https://rest.messagebird.com/lookup/31624971134" {
46+
t.Errorf("Unexpected lookup href: %s", lookup.Href)
47+
}
48+
if lookup.PhoneNumber != phoneNumber {
49+
t.Errorf("Unexpected lookup phoneNumber: %s", lookup.PhoneNumber)
50+
}
51+
if lookup.Formats != nil {
52+
if lookup.Formats.International != "+31 6 24971134" {
53+
t.Errorf("Unexpected International format: %s", lookup.HLR.Reference)
54+
}
55+
} else {
56+
t.Errorf("Unexpected empty Formats object")
57+
}
58+
if lookup.HLR != nil {
59+
if lookup.HLR.Reference != "yoloswag2000" {
60+
t.Errorf("Unexpected hlr reference: %s", lookup.HLR.Reference)
61+
}
62+
} else {
63+
t.Errorf("Unexpected empty hlr")
64+
}
65+
}
66+
67+
func checkHLR(t *testing.T, hlr *HLR) {
68+
if hlr.Id != "6118d3f06566fcd0cdc8962h65065907" {
69+
t.Errorf("Unexpected hlr id: %s", hlr.Id)
70+
}
71+
if hlr.Network != 20416 {
72+
t.Errorf("Unexpected hlr network: %d", hlr.Network)
73+
}
74+
if hlr.Reference != "yoloswag2000" {
75+
t.Errorf("Unexpected hlr reference: %s", hlr.Reference)
76+
}
77+
if hlr.Status != "active" {
78+
t.Errorf("Unexpected hlr status: %s", hlr.Status)
79+
}
80+
}
81+
82+
func TestHLRLookup(t *testing.T) {
83+
SetServerResponse(200, lookupHLRObject)
84+
85+
hlr, err := mbClient.HLRLookup(31624971134, "NL")
86+
if err != nil {
87+
t.Fatalf("Didn't expect error while doing the lookup: %s", err)
88+
}
89+
checkHLR(t, hlr)
90+
}
91+
92+
func TestNewHLRLookup(t *testing.T) {
93+
SetServerResponse(201, lookupHLRObject)
94+
95+
hlr, err := mbClient.NewHLRLookup(31624971134, "NL", "yoloswag2000")
96+
if err != nil {
97+
t.Fatalf("Didn't expect error while doing the lookup: %s", err)
98+
}
99+
100+
checkHLR(t, hlr)
101+
}

0 commit comments

Comments
 (0)