Skip to content

Commit aed53b6

Browse files
committed
Merge pull request #6 from messagebird/lookup
Add support for the Lookup API
2 parents 90f8e11 + 286b484 commit aed53b6

File tree

6 files changed

+209
-7
lines changed

6 files changed

+209
-7
lines changed

examples/lookup.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 : %d' % lookup.phoneNumber)
34+
print(' countryCode : %s' % lookup.countryCode)
35+
print(' countryPrefix : %d' % 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+
if lookup.hlr is not None:
43+
print(' hlr.id : %s' % lookup.hlr.id)
44+
print(' hlr.network : %d' % lookup.hlr.network)
45+
print(' hlr.reference : %s' % lookup.hlr.reference)
46+
print(' hlr.status : %s' % lookup.hlr.status)
47+
print(' hlr.createdDatetime : %s' % lookup.hlr.createdDatetime)
48+
print(' hlr.statusDatetime : %s' % lookup.hlr.statusDatetime)
49+
50+
except messagebird.client.ErrorException as e:
51+
print('\nAn error occured while requesting a Lookup object:\n')
52+
53+
for error in e.errors:
54+
print(' code : %d' % error.code)
55+
print(' description : %s' % error.description)
56+
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 : %d' % lookup_hlr.msisdn)
35+
print(' network : %d' % 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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 : %d' % lookup_hlr.msisdn)
35+
print(' reference : %s' % lookup_hlr.reference)
36+
print(' status : %s' % lookup_hlr.status)
37+
print(' createdDatetime : %s' % lookup_hlr.createdDatetime)
38+
print(' statusDatetime : %s\n' % lookup_hlr.statusDatetime)
39+
40+
except messagebird.client.ErrorException as e:
41+
print('\nAn error occured while requesting a Lookup HLR object:\n')
42+
43+
for error in e.errors:
44+
print(' code : %d' % error.code)
45+
print(' description : %s' % error.description)
46+
print(' parameter : %s\n' % error.parameter)

messagebird/client.py

Lines changed: 20 additions & 7 deletions
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

@@ -31,7 +32,7 @@ def __init__(self, access_key):
3132
self.access_key = access_key
3233
self._supported_status_codes = [200, 201, 204, 401, 404, 405, 422]
3334

34-
def request(self, path, params={}):
35+
def request(self, path, method='GET', params={}):
3536
url = urljoin(ENDPOINT, path)
3637

3738
headers = {
@@ -41,8 +42,8 @@ def request(self, path, params={}):
4142
'Content-Type' : 'application/json'
4243
}
4344

44-
if len(params) == 0:
45-
response = requests.get(url, verify=True, headers=headers)
45+
if method == 'GET':
46+
response = requests.get(url, verify=True, headers=headers, params=params)
4647
else:
4748
response = requests.post(url, verify=True, headers=headers, data=json.dumps(params))
4849

@@ -66,7 +67,7 @@ def hlr(self, id):
6667

6768
def hlr_create(self, msisdn, reference):
6869
"""Perform a new HLR lookup."""
69-
return HLR().load(self.request('hlr', { 'msisdn' : msisdn, 'reference' : reference }))
70+
return HLR().load(self.request('hlr', 'POST', { 'msisdn' : msisdn, 'reference' : reference }))
7071

7172
def message(self, id):
7273
"""Retrieve the information of a specific message."""
@@ -78,7 +79,7 @@ def message_create(self, originator, recipients, body, params={}):
7879
recipients = ','.join(recipients)
7980

8081
params.update({ 'originator' : originator, 'body' : body, 'recipients' : recipients })
81-
return Message().load(self.request('messages', params))
82+
return Message().load(self.request('messages', 'POST', params))
8283

8384
def voice_message(self, id):
8485
"Retrieve the information of a specific voice message."
@@ -90,4 +91,16 @@ def voice_message_create(self, recipients, body, params={}):
9091
recipients = ','.join(recipients)
9192

9293
params.update({ 'recipients' : recipients, 'body' : body })
93-
return VoiceMessage().load(self.request('voicemessages', params))
94+
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)