Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions examples/lookup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import messagebird

# ACCESS_KEY = ''
# PHONE_NUMBER = ''

try:
ACCESS_KEY
except NameError:
print('You need to set an ACCESS_KEY constant in this file')
sys.exit(1)

try:
PHONE_NUMBER
except NameError:
print('You need to set an PHONE_NUMBER constant in this file')
sys.exit(1)

try:
# Create a MessageBird client with the specified ACCESS_KEY.
client = messagebird.Client(ACCESS_KEY)

# Fetch the Lookup object for the specified PHONE_NUMBER.
lookup = client.lookup(PHONE_NUMBER)

# Print the object information.
print('\nThe following information was returned as a Lookup object:\n')
print(' href : %s' % lookup.href)
print(' phoneNumber : %d' % lookup.phoneNumber)
print(' countryCode : %s' % lookup.countryCode)
print(' countryPrefix : %d' % lookup.countryPrefix)
print(' type : %s' % lookup.type)
print(' formats.e164 : %s' % lookup.formats.e164)
print(' formats.international : %s' % lookup.formats.international)
print(' formats.national : %s' % lookup.formats.national)
print(' formats.rfc3966 : %s' % lookup.formats.rfc3966)

if lookup.hlr is not None:
print(' hlr.id : %s' % lookup.hlr.id)
print(' hlr.network : %d' % lookup.hlr.network)
print(' hlr.reference : %s' % lookup.hlr.reference)
print(' hlr.status : %s' % lookup.hlr.status)
print(' hlr.createdDatetime : %s' % lookup.hlr.createdDatetime)
print(' hlr.statusDatetime : %s' % lookup.hlr.statusDatetime)

except messagebird.client.ErrorException as e:
print('\nAn error occured while requesting a Lookup object:\n')

for error in e.errors:
print(' code : %d' % error.code)
print(' description : %s' % error.description)
print(' parameter : %s\n' % error.parameter)
47 changes: 47 additions & 0 deletions examples/lookup_hlr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import messagebird

# ACCESS_KEY = ''
# PHONE_NUMBER = ''

try:
ACCESS_KEY
except NameError:
print('You need to set an ACCESS_KEY constant in this file')
sys.exit(1)

try:
PHONE_NUMBER
except NameError:
print('You need to set a PHONE_NUMBER constant in this file')
sys.exit(1)

try:
# Create a MessageBird client with the specified ACCESS_KEY.
client = messagebird.Client(ACCESS_KEY)

# Create a new Lookup HLR object.
lookup_hlr = client.lookup_hlr(PHONE_NUMBER)

# Print the object information.
print('\nThe following information was returned as a Lookup HLR object:\n')
print(' id : %s' % lookup_hlr.id)
print(' href : %s' % lookup_hlr.href)
print(' msisdn : %d' % lookup_hlr.msisdn)
print(' network : %d' % lookup_hlr.network)
print(' reference : %s' % lookup_hlr.reference)
print(' status : %s' % lookup_hlr.status)
print(' createdDatetime : %s' % lookup_hlr.createdDatetime)
print(' statusDatetime : %s\n' % lookup_hlr.statusDatetime)

except messagebird.client.ErrorException as e:
print('\nAn error occured while requesting a Lookup HLR object:\n')

for error in e.errors:
print(' code : %d' % error.code)
print(' description : %s' % error.description)
print(' parameter : %s\n' % error.parameter)
46 changes: 46 additions & 0 deletions examples/lookup_hlr_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import messagebird

# ACCESS_KEY = ''
# PHONE_NUMBER = ''

try:
ACCESS_KEY
except NameError:
print('You need to set an ACCESS_KEY constant in this file')
sys.exit(1)

try:
PHONE_NUMBER
except NameError:
print('You need to set a PHONE_NUMBER constant in this file')
sys.exit(1)

try:
# Create a MessageBird client with the specified ACCESS_KEY.
client = messagebird.Client(ACCESS_KEY)

# Create a new Lookup HLR object.
lookup_hlr = client.lookup_hlr_create(PHONE_NUMBER, { 'reference' : 'Reference' })

# Print the object information.
print('\nThe following information was returned as a Lookup HLR object:\n')
print(' id : %s' % lookup_hlr.id)
print(' href : %s' % lookup_hlr.href)
print(' msisdn : %d' % lookup_hlr.msisdn)
print(' reference : %s' % lookup_hlr.reference)
print(' status : %s' % lookup_hlr.status)
print(' createdDatetime : %s' % lookup_hlr.createdDatetime)
print(' statusDatetime : %s\n' % lookup_hlr.statusDatetime)

except messagebird.client.ErrorException as e:
print('\nAn error occured while requesting a Lookup HLR object:\n')

for error in e.errors:
print(' code : %d' % error.code)
print(' description : %s' % error.description)
print(' parameter : %s\n' % error.parameter)
27 changes: 20 additions & 7 deletions messagebird/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
from messagebird.hlr import HLR
from messagebird.message import Message
from messagebird.voicemessage import VoiceMessage
from messagebird.lookup import Lookup

ENDPOINT = 'https://rest.messagebird.com'
CLIENT_VERSION = '1.0.3'
CLIENT_VERSION = '1.1.0'
PYTHON_VERSION = '%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2])


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

def request(self, path, params={}):
def request(self, path, method='GET', params={}):
url = urljoin(ENDPOINT, path)

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

if len(params) == 0:
response = requests.get(url, verify=True, headers=headers)
if method == 'GET':
response = requests.get(url, verify=True, headers=headers, params=params)
else:
response = requests.post(url, verify=True, headers=headers, data=json.dumps(params))

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

def hlr_create(self, msisdn, reference):
"""Perform a new HLR lookup."""
return HLR().load(self.request('hlr', { 'msisdn' : msisdn, 'reference' : reference }))
return HLR().load(self.request('hlr', 'POST', { 'msisdn' : msisdn, 'reference' : reference }))

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

params.update({ 'originator' : originator, 'body' : body, 'recipients' : recipients })
return Message().load(self.request('messages', params))
return Message().load(self.request('messages', 'POST', params))

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

params.update({ 'recipients' : recipients, 'body' : body })
return VoiceMessage().load(self.request('voicemessages', params))
return VoiceMessage().load(self.request('voicemessages', 'POST', params))

def lookup(self, phonenumber, params={}):
"""Do a new lookup."""
return Lookup().load(self.request('lookup/' + str(phonenumber), 'GET', params))

def lookup_hlr(self, phonenumber, params={}):
"""Retrieve the information of a specific HLR lookup."""
return HLR().load(self.request('lookup/' + str(phonenumber) + '/hlr', 'GET', params))

def lookup_hlr_create(self, phonenumber, params={}):
"""Perform a new HLR lookup."""
return HLR().load(self.request('lookup/' + str(phonenumber) + '/hlr', 'POST', params))
8 changes: 8 additions & 0 deletions messagebird/formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from messagebird.base import Base

class Formats(Base):
def __init__(self):
self.e164 = None
self.international = None
self.national = None
self.rfc3966 = None
32 changes: 32 additions & 0 deletions messagebird/lookup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from messagebird.base import Base
from messagebird.formats import Formats
from messagebird.hlr import HLR

class Lookup(Base):
def __init__(self):
self.href = None
self.countryCode = None
self.countryPrefix = None
self.phoneNumber = None
self.type = None
self._formats = None
self._hlr = None

def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

@property
def formats(self):
return self._formats

@formats.setter
def formats(self, value):
self._formats = Formats().load(value)

@property
def hlr(self):
return self._hlr

@hlr.setter
def hlr(self, value):
self._hlr = HLR().load(value)