Skip to content

Commit 1973e2c

Browse files
author
Marcel Corso
committed
new lookup endpoints: some changes after feedback
1 parent bd439f8 commit 1973e2c

File tree

3 files changed

+55
-44
lines changed

3 files changed

+55
-44
lines changed

client.go

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package messagebird
99
import (
1010
"encoding/json"
1111
"errors"
12-
"fmt"
1312
"io/ioutil"
1413
"log"
1514
"net/http"
@@ -265,14 +264,10 @@ func (c *Client) OtpVerify(recipient string, token string, params *OtpParams) (*
265264
return message, nil
266265
}
267266

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())
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()
276271

277272
lookup := &Lookup{}
278273
if err := c.request(lookup, path, nil); err != nil {
@@ -286,18 +281,12 @@ func (c *Client) Lookup(phoneNumber int, countryCode string) (*Lookup, error) {
286281
return lookup, nil
287282
}
288283

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-
}
284+
// NewHLRLookup creates a new HLR lookup for the specified number.
285+
func (c *Client) NewHLRLookup(phoneNumber string, lookupParams *LookupParams) (*HLR, error) {
286+
params := paramsForLookup(lookupParams)
298287

299288
hlr := &HLR{}
300-
path := fmt.Sprintf("lookup/%d/hlr", phoneNumber)
289+
path := "lookup/" + phoneNumber + "/hlr"
301290
if err := c.request(hlr, path, params); err != nil {
302291
if err == ErrResponse {
303292
return hlr, err
@@ -309,14 +298,11 @@ func (c *Client) NewHLRLookup(phoneNumber int, countryCode string, reference str
309298
return hlr, nil
310299
}
311300

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-
}
301+
// HLRLookup performs a HLR lookup for the specified number.
302+
func (c *Client) HLRLookup(phoneNumber string, params *LookupParams) (*HLR, error) {
303+
urlParams := paramsForLookup(params)
318304

319-
path := fmt.Sprintf("lookup/%d/hlr?%s", phoneNumber, urlParams.Encode())
305+
path := "lookup/" + phoneNumber + "/hlr?" + urlParams.Encode()
320306

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

lookup.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package messagebird
22

3+
import "net/url"
4+
35
type Formats struct {
46
E164 string
57
International string
@@ -11,8 +13,30 @@ type Lookup struct {
1113
Href string
1214
CountryCode string
1315
CountryPrefix int
14-
PhoneNumber int
16+
PhoneNumber int64
1517
Type string
16-
Formats *Formats
18+
Formats Formats
1719
HLR *HLR
1820
}
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: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package messagebird
22

3-
import "testing"
3+
import (
4+
"strconv"
5+
"testing"
6+
)
47

58
var lookupObject []byte = []byte(`{
69
"href":"https://rest.messagebird.com/lookup/31624971134",
@@ -17,7 +20,7 @@ var lookupObject []byte = []byte(`{
1720
"hlr":{
1821
"id":"6118d3f06566fcd0cdc8962h65065907",
1922
"network":20416,
20-
"reference":"yoloswag2000",
23+
"reference":"referece2000",
2124
"status":"active",
2225
"createdDatetime":"2015-12-15T08:19:24+00:00",
2326
"statusDatetime":"2015-12-15T08:19:25+00:00"
@@ -27,7 +30,7 @@ var lookupObject []byte = []byte(`{
2730
var lookupHLRObject []byte = []byte(`{
2831
"id":"6118d3f06566fcd0cdc8962h65065907",
2932
"network":20416,
30-
"reference":"yoloswag2000",
33+
"reference":"referece2000",
3134
"status":"active",
3235
"createdDatetime":"2015-12-15T08:19:24+00:00",
3336
"statusDatetime":"2015-12-15T08:19:25+00:00"
@@ -36,27 +39,25 @@ var lookupHLRObject []byte = []byte(`{
3639
func TestLookup(t *testing.T) {
3740
SetServerResponse(200, lookupObject)
3841

39-
phoneNumber := 31624971134
40-
lookup, err := mbClient.Lookup(phoneNumber, "NL")
42+
phoneNumber := "31624971134"
43+
lookup, err := mbClient.Lookup(phoneNumber, &LookupParams{CountryCode: "NL"})
4144
if err != nil {
4245
t.Fatalf("Didn't expect error while doing the lookup: %s", err)
4346
}
4447

4548
if lookup.Href != "https://rest.messagebird.com/lookup/31624971134" {
4649
t.Errorf("Unexpected lookup href: %s", lookup.Href)
4750
}
48-
if lookup.PhoneNumber != phoneNumber {
51+
if strconv.FormatInt(lookup.PhoneNumber, 10) != phoneNumber {
4952
t.Errorf("Unexpected lookup phoneNumber: %s", lookup.PhoneNumber)
5053
}
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")
54+
55+
if lookup.Formats.International != "+31 6 24971134" {
56+
t.Errorf("Unexpected International format: %s", lookup.HLR.Reference)
5757
}
58+
5859
if lookup.HLR != nil {
59-
if lookup.HLR.Reference != "yoloswag2000" {
60+
if lookup.HLR.Reference != "referece2000" {
6061
t.Errorf("Unexpected hlr reference: %s", lookup.HLR.Reference)
6162
}
6263
} else {
@@ -71,7 +72,7 @@ func checkHLR(t *testing.T, hlr *HLR) {
7172
if hlr.Network != 20416 {
7273
t.Errorf("Unexpected hlr network: %d", hlr.Network)
7374
}
74-
if hlr.Reference != "yoloswag2000" {
75+
if hlr.Reference != "referece2000" {
7576
t.Errorf("Unexpected hlr reference: %s", hlr.Reference)
7677
}
7778
if hlr.Status != "active" {
@@ -82,7 +83,7 @@ func checkHLR(t *testing.T, hlr *HLR) {
8283
func TestHLRLookup(t *testing.T) {
8384
SetServerResponse(200, lookupHLRObject)
8485

85-
hlr, err := mbClient.HLRLookup(31624971134, "NL")
86+
hlr, err := mbClient.HLRLookup("31624971134", &LookupParams{CountryCode: "NL"})
8687
if err != nil {
8788
t.Fatalf("Didn't expect error while doing the lookup: %s", err)
8889
}
@@ -92,7 +93,7 @@ func TestHLRLookup(t *testing.T) {
9293
func TestNewHLRLookup(t *testing.T) {
9394
SetServerResponse(201, lookupHLRObject)
9495

95-
hlr, err := mbClient.NewHLRLookup(31624971134, "NL", "yoloswag2000")
96+
hlr, err := mbClient.NewHLRLookup("31624971134", &LookupParams{CountryCode: "NL", Reference: "reference2000"})
9697
if err != nil {
9798
t.Fatalf("Didn't expect error while doing the lookup: %s", err)
9899
}

0 commit comments

Comments
 (0)