diff --git a/client.go b/client.go index 92e0a7b..a33903a 100644 --- a/client.go +++ b/client.go @@ -18,7 +18,7 @@ import ( ) const ( - ClientVersion = "2.1.1" + ClientVersion = "2.2.0" Endpoint = "https://rest.messagebird.com" ) @@ -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 +} diff --git a/lookup.go b/lookup.go new file mode 100644 index 0000000..4b1b393 --- /dev/null +++ b/lookup.go @@ -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 +} diff --git a/lookup_test.go b/lookup_test.go new file mode 100644 index 0000000..7c9dbaf --- /dev/null +++ b/lookup_test.go @@ -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) +}