Skip to content

Commit 213612b

Browse files
committed
Merge pull request #3 from messagebird/lookup
Adding the new Lookup API endpoints
2 parents cc0ae25 + 199ddeb commit 213612b

File tree

3 files changed

+196
-1
lines changed

3 files changed

+196
-1
lines changed

client.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
const (
21-
ClientVersion = "2.1.1"
21+
ClientVersion = "2.2.0"
2222
Endpoint = "https://rest.messagebird.com"
2323
)
2424

@@ -263,3 +263,54 @@ func (c *Client) OtpVerify(recipient string, token string, params *OtpParams) (*
263263

264264
return message, nil
265265
}
266+
267+
// Lookup performs a new lookup for the specified number.
268+
func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, error) {
269+
urlParams := paramsForLookup(params)
270+
path := "lookup/" + phoneNumber + "?" + urlParams.Encode()
271+
272+
lookup := &Lookup{}
273+
if err := c.request(lookup, path, nil); err != nil {
274+
if err == ErrResponse {
275+
return lookup, err
276+
}
277+
278+
return nil, err
279+
}
280+
281+
return lookup, nil
282+
}
283+
284+
// NewLookupHLR creates a new HLR lookup for the specified number.
285+
func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
286+
urlParams := paramsForLookup(params)
287+
path := "lookup/" + phoneNumber + "/hlr"
288+
289+
hlr := &HLR{}
290+
if err := c.request(hlr, path, urlParams); err != nil {
291+
if err == ErrResponse {
292+
return hlr, err
293+
}
294+
295+
return nil, err
296+
}
297+
298+
return hlr, nil
299+
}
300+
301+
// LookupHLR performs a HLR lookup for the specified number.
302+
func (c *Client) LookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
303+
urlParams := paramsForLookup(params)
304+
path := "lookup/" + phoneNumber + "/hlr?" + urlParams.Encode()
305+
306+
hlr := &HLR{}
307+
if err := c.request(hlr, path, nil); err != nil {
308+
if err == ErrResponse {
309+
return hlr, err
310+
}
311+
312+
return nil, err
313+
}
314+
315+
return hlr, nil
316+
}

lookup.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package messagebird
2+
3+
import "net/url"
4+
5+
type Formats struct {
6+
E164 string
7+
International string
8+
National string
9+
Rfc3966 string
10+
}
11+
12+
type Lookup struct {
13+
Href string
14+
CountryCode string
15+
CountryPrefix int
16+
PhoneNumber int64
17+
Type string
18+
Formats Formats
19+
HLR *HLR
20+
}
21+
22+
type LookupParams struct {
23+
CountryCode string
24+
Reference string
25+
}
26+
27+
func paramsForLookup(params *LookupParams) *url.Values {
28+
urlParams := &url.Values{}
29+
30+
if params == nil {
31+
return urlParams
32+
}
33+
34+
if params.CountryCode != "" {
35+
urlParams.Set("countryCode", params.CountryCode)
36+
}
37+
if params.Reference != "" {
38+
urlParams.Set("reference", params.Reference)
39+
}
40+
41+
return urlParams
42+
}

lookup_test.go

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

0 commit comments

Comments
 (0)