Skip to content

Commit 9e08281

Browse files
author
Sam Wierema
committed
Add support for the Lookup API
1 parent 0affba6 commit 9e08281

File tree

6 files changed

+196
-1
lines changed

6 files changed

+196
-1
lines changed

examples/lookup.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
# ACCESS_KEY = ''
9+
# PHONE_NUMBER = ''
10+
11+
try:
12+
ACCESS_KEY
13+
except NameError:
14+
print('You need to set an ACCESS_KEY constant in this file')
15+
sys.exit(1)
16+
17+
try:
18+
PHONE_NUMBER
19+
except NameError:
20+
print('You need to set an PHONE_NUMBER constant in this file')
21+
sys.exit(1)
22+
23+
try:
24+
# Create a MessageBird client with the specified ACCESS_KEY.
25+
client = messagebird.Client(ACCESS_KEY)
26+
27+
# Fetch the Lookup object for the specified PHONE_NUMBER.
28+
lookup = client.lookup(PHONE_NUMBER)
29+
30+
# Print the object information.
31+
print('\nThe following information was returned as a Lookup object:\n')
32+
print(' href : %s' % lookup.href)
33+
print(' phoneNumber : %s' % lookup.phoneNumber)
34+
print(' countryCode : %s' % lookup.countryCode)
35+
print(' countryPrefix : %s' % lookup.countryPrefix)
36+
print(' type : %s' % lookup.type)
37+
print(' formats.e164 : %s' % lookup.formats.e164)
38+
print(' formats.international : %s' % lookup.formats.international)
39+
print(' formats.national : %s' % lookup.formats.national)
40+
print(' formats.rfc3966 : %s' % lookup.formats.rfc3966)
41+
42+
except messagebird.client.ErrorException as e:
43+
print('\nAn error occured while requesting a Lookup object:\n')
44+
45+
for error in e.errors:
46+
print(' code : %d' % error.code)
47+
print(' description : %s' % error.description)
48+
print(' parameter : %s\n' % error.parameter)

examples/lookup_hlr.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
# ACCESS_KEY = ''
9+
# PHONE_NUMBER = ''
10+
11+
try:
12+
ACCESS_KEY
13+
except NameError:
14+
print('You need to set an ACCESS_KEY constant in this file')
15+
sys.exit(1)
16+
17+
try:
18+
PHONE_NUMBER
19+
except NameError:
20+
print('You need to set a PHONE_NUMBER constant in this file')
21+
sys.exit(1)
22+
23+
try:
24+
# Create a MessageBird client with the specified ACCESS_KEY.
25+
client = messagebird.Client(ACCESS_KEY)
26+
27+
# Create a new Lookup HLR object.
28+
lookup_hlr = client.lookup_hlr(PHONE_NUMBER)
29+
30+
# Print the object information.
31+
print('\nThe following information was returned as a Lookup HLR object:\n')
32+
print(' id : %s' % lookup_hlr.id)
33+
print(' href : %s' % lookup_hlr.href)
34+
print(' msisdn : %s' % lookup_hlr.msisdn)
35+
print(' network : %s' % lookup_hlr.network)
36+
print(' reference : %s' % lookup_hlr.reference)
37+
print(' status : %s' % lookup_hlr.status)
38+
print(' createdDatetime : %s' % lookup_hlr.createdDatetime)
39+
print(' statusDatetime : %s\n' % lookup_hlr.statusDatetime)
40+
41+
except messagebird.client.ErrorException as e:
42+
print('\nAn error occured while requesting a Lookup HLR object:\n')
43+
44+
for error in e.errors:
45+
print(' code : %d' % error.code)
46+
print(' description : %s' % error.description)
47+
print(' parameter : %s\n' % error.parameter)

examples/lookup_hlr_create.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
# ACCESS_KEY = ''
9+
# PHONE_NUMBER = ''
10+
11+
try:
12+
ACCESS_KEY
13+
except NameError:
14+
print('You need to set an ACCESS_KEY constant in this file')
15+
sys.exit(1)
16+
17+
try:
18+
PHONE_NUMBER
19+
except NameError:
20+
print('You need to set a PHONE_NUMBER constant in this file')
21+
sys.exit(1)
22+
23+
try:
24+
# Create a MessageBird client with the specified ACCESS_KEY.
25+
client = messagebird.Client(ACCESS_KEY)
26+
27+
# Create a new Lookup HLR object.
28+
lookup_hlr = client.lookup_hlr_create(PHONE_NUMBER, { 'reference' : 'Reference' })
29+
30+
# Print the object information.
31+
print('\nThe following information was returned as a Lookup HLR object:\n')
32+
print(' id : %s' % lookup_hlr.id)
33+
print(' href : %s' % lookup_hlr.href)
34+
print(' msisdn : %s' % lookup_hlr.msisdn)
35+
print(' network : %s' % lookup_hlr.network)
36+
print(' reference : %s' % lookup_hlr.reference)
37+
print(' status : %s' % lookup_hlr.status)
38+
print(' createdDatetime : %s' % lookup_hlr.createdDatetime)
39+
print(' statusDatetime : %s\n' % lookup_hlr.statusDatetime)
40+
41+
except messagebird.client.ErrorException as e:
42+
print('\nAn error occured while requesting a Lookup HLR object:\n')
43+
44+
for error in e.errors:
45+
print(' code : %d' % error.code)
46+
print(' description : %s' % error.description)
47+
print(' parameter : %s\n' % error.parameter)

messagebird/client.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
from messagebird.hlr import HLR
1414
from messagebird.message import Message
1515
from messagebird.voicemessage import VoiceMessage
16+
from messagebird.lookup import Lookup
1617

1718
ENDPOINT = 'https://rest.messagebird.com'
18-
CLIENT_VERSION = '1.0.3'
19+
CLIENT_VERSION = '1.1.0'
1920
PYTHON_VERSION = '%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2])
2021

2122

@@ -91,3 +92,15 @@ def voice_message_create(self, recipients, body, params={}):
9192

9293
params.update({ 'recipients' : recipients, 'body' : body })
9394
return VoiceMessage().load(self.request('voicemessages', 'POST', params))
95+
96+
def lookup(self, phonenumber, params={}):
97+
"""Do a new lookup."""
98+
return Lookup().load(self.request('lookup/' + str(phonenumber), 'GET', params))
99+
100+
def lookup_hlr(self, phonenumber, params={}):
101+
"""Retrieve the information of a specific HLR lookup."""
102+
return HLR().load(self.request('lookup/' + str(phonenumber) + '/hlr', 'GET', params))
103+
104+
def lookup_hlr_create(self, phonenumber, params={}):
105+
"""Perform a new HLR lookup."""
106+
return HLR().load(self.request('lookup/' + str(phonenumber) + '/hlr', 'POST', params))

messagebird/formats.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from messagebird.base import Base
2+
3+
class Formats(Base):
4+
def __init__(self):
5+
self.e164 = None
6+
self.international = None
7+
self.national = None
8+
self.rfc3966 = None

messagebird/lookup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from messagebird.base import Base
2+
from messagebird.formats import Formats
3+
from messagebird.hlr import HLR
4+
5+
class Lookup(Base):
6+
def __init__(self):
7+
self.href = None
8+
self.countryCode = None
9+
self.countryPrefix = None
10+
self.phoneNumber = None
11+
self.type = None
12+
self._formats = None
13+
self._hlr = None
14+
15+
def __str__(self):
16+
return str(self.__class__) + ": " + str(self.__dict__)
17+
18+
@property
19+
def formats(self):
20+
return self._formats
21+
22+
@formats.setter
23+
def formats(self, value):
24+
self._formats = Formats().load(value)
25+
26+
@property
27+
def hlr(self):
28+
return self._hlr
29+
30+
@hlr.setter
31+
def hlr(self, value):
32+
self._hlr = HLR().load(value)

0 commit comments

Comments
 (0)