diff --git a/examples/lookup.py b/examples/lookup.py new file mode 100644 index 0000000..81c1940 --- /dev/null +++ b/examples/lookup.py @@ -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) diff --git a/examples/lookup_hlr.py b/examples/lookup_hlr.py new file mode 100644 index 0000000..50a55f9 --- /dev/null +++ b/examples/lookup_hlr.py @@ -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) diff --git a/examples/lookup_hlr_create.py b/examples/lookup_hlr_create.py new file mode 100644 index 0000000..ace135d --- /dev/null +++ b/examples/lookup_hlr_create.py @@ -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) diff --git a/messagebird/client.py b/messagebird/client.py index 8e07437..023e2c8 100644 --- a/messagebird/client.py +++ b/messagebird/client.py @@ -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]) @@ -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 = { @@ -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)) @@ -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.""" @@ -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." @@ -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)) diff --git a/messagebird/formats.py b/messagebird/formats.py new file mode 100644 index 0000000..c45aa0d --- /dev/null +++ b/messagebird/formats.py @@ -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 diff --git a/messagebird/lookup.py b/messagebird/lookup.py new file mode 100644 index 0000000..006eb77 --- /dev/null +++ b/messagebird/lookup.py @@ -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)