Skip to content

Commit

Permalink
Merge pull request #3 from messagebird/lookup
Browse files Browse the repository at this point in the history
Adding the new Lookup API endpoints
  • Loading branch information
samwierema committed Dec 17, 2015
2 parents cc0ae25 + 199ddeb commit 213612b
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 1 deletion.
53 changes: 52 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
ClientVersion = "2.1.1"
ClientVersion = "2.2.0"
Endpoint = "https://rest.messagebird.com"
)

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

return message, nil
}

// Lookup performs a new lookup for the specified number.
func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, error) {
urlParams := paramsForLookup(params)
path := "lookup/" + phoneNumber + "?" + urlParams.Encode()

lookup := &Lookup{}
if err := c.request(lookup, path, nil); err != nil {
if err == ErrResponse {
return lookup, err
}

return nil, err
}

return lookup, nil
}

// NewLookupHLR creates a new HLR lookup for the specified number.
func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
urlParams := paramsForLookup(params)
path := "lookup/" + phoneNumber + "/hlr"

hlr := &HLR{}
if err := c.request(hlr, path, urlParams); err != nil {
if err == ErrResponse {
return hlr, err
}

return nil, err
}

return hlr, nil
}

// LookupHLR performs a HLR lookup for the specified number.
func (c *Client) LookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
urlParams := paramsForLookup(params)
path := "lookup/" + phoneNumber + "/hlr?" + urlParams.Encode()

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

return nil, err
}

return hlr, nil
}
42 changes: 42 additions & 0 deletions lookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package messagebird

import "net/url"

type Formats struct {
E164 string
International string
National string
Rfc3966 string
}

type Lookup struct {
Href string
CountryCode string
CountryPrefix int
PhoneNumber int64
Type string
Formats Formats
HLR *HLR
}

type LookupParams struct {
CountryCode string
Reference string
}

func paramsForLookup(params *LookupParams) *url.Values {
urlParams := &url.Values{}

if params == nil {
return urlParams
}

if params.CountryCode != "" {
urlParams.Set("countryCode", params.CountryCode)
}
if params.Reference != "" {
urlParams.Set("reference", params.Reference)
}

return urlParams
}
102 changes: 102 additions & 0 deletions lookup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package messagebird

import (
"strconv"
"testing"
)

var lookupObject []byte = []byte(`{
"href":"https://rest.messagebird.com/lookup/31624971134",
"countryCode":"NL",
"countryPrefix":31,
"phoneNumber":31624971134,
"type":"mobile",
"formats":{
"e164":"+31624971134",
"international":"+31 6 24971134",
"national":"06 24971134",
"rfc3966":"tel:+31-6-24971134"
},
"hlr":{
"id":"6118d3f06566fcd0cdc8962h65065907",
"network":20416,
"reference":"referece2000",
"status":"active",
"createdDatetime":"2015-12-15T08:19:24+00:00",
"statusDatetime":"2015-12-15T08:19:25+00:00"
}
}`)

var lookupHLRObject []byte = []byte(`{
"id":"6118d3f06566fcd0cdc8962h65065907",
"network":20416,
"reference":"referece2000",
"status":"active",
"createdDatetime":"2015-12-15T08:19:24+00:00",
"statusDatetime":"2015-12-15T08:19:25+00:00"
}`)

func TestLookup(t *testing.T) {
SetServerResponse(200, lookupObject)

phoneNumber := "31624971134"
lookup, err := mbClient.Lookup(phoneNumber, &LookupParams{CountryCode: "NL"})
if err != nil {
t.Fatalf("Didn't expect error while doing the lookup: %s", err)
}

if lookup.Href != "https://rest.messagebird.com/lookup/31624971134" {
t.Errorf("Unexpected lookup href: %s", lookup.Href)
}
if strconv.FormatInt(lookup.PhoneNumber, 10) != phoneNumber {
t.Errorf("Unexpected lookup phoneNumber: %s", lookup.PhoneNumber)
}

if lookup.Formats.International != "+31 6 24971134" {
t.Errorf("Unexpected International format: %s", lookup.HLR.Reference)
}

if lookup.HLR != nil {
if lookup.HLR.Reference != "referece2000" {
t.Errorf("Unexpected hlr reference: %s", lookup.HLR.Reference)
}
} else {
t.Errorf("Unexpected empty hlr")
}
}

func checkHLR(t *testing.T, hlr *HLR) {
if hlr.Id != "6118d3f06566fcd0cdc8962h65065907" {
t.Errorf("Unexpected hlr id: %s", hlr.Id)
}
if hlr.Network != 20416 {
t.Errorf("Unexpected hlr network: %d", hlr.Network)
}
if hlr.Reference != "referece2000" {
t.Errorf("Unexpected hlr reference: %s", hlr.Reference)
}
if hlr.Status != "active" {
t.Errorf("Unexpected hlr status: %s", hlr.Status)
}
}

func TestLookupHLR(t *testing.T) {
SetServerResponse(200, lookupHLRObject)

hlr, err := mbClient.LookupHLR("31624971134", &LookupParams{CountryCode: "NL"})
if err != nil {
t.Fatalf("Didn't expect error while doing the lookup: %s", err)
}
checkHLR(t, hlr)
}

func TestNewLookupHLR(t *testing.T) {
SetServerResponse(201, lookupHLRObject)

hlr, err := mbClient.NewLookupHLR("31624971134", &LookupParams{CountryCode: "NL", Reference: "reference2000"})
if err != nil {
t.Fatalf("Didn't expect error while doing the lookup: %s", err)
}

checkHLR(t, hlr)
}

0 comments on commit 213612b

Please sign in to comment.