From 0affba6dc003f6d94453ab0363e3f4ecc9f31476 Mon Sep 17 00:00:00 2001 From: Sam Wierema Date: Mon, 28 Dec 2015 15:42:49 +0100 Subject: [PATCH 1/4] Add support for GET parameters The new Lookup API has endpoints that accept GET parameters. Change the request method to optionally set a method parameter. If the method is GET, add params to the request (if they exist). --- messagebird/client.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/messagebird/client.py b/messagebird/client.py index 8e07437..316d773 100644 --- a/messagebird/client.py +++ b/messagebird/client.py @@ -31,7 +31,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 +41,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 +66,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 +78,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 +90,4 @@ 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)) From 9e08281a6cb4d04e78adb081d6dc7eb96fc1ff44 Mon Sep 17 00:00:00 2001 From: Sam Wierema Date: Mon, 28 Dec 2015 15:53:15 +0100 Subject: [PATCH 2/4] Add support for the Lookup API --- examples/lookup.py | 48 +++++++++++++++++++++++++++++++++++ examples/lookup_hlr.py | 47 ++++++++++++++++++++++++++++++++++ examples/lookup_hlr_create.py | 47 ++++++++++++++++++++++++++++++++++ messagebird/client.py | 15 ++++++++++- messagebird/formats.py | 8 ++++++ messagebird/lookup.py | 32 +++++++++++++++++++++++ 6 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 examples/lookup.py create mode 100644 examples/lookup_hlr.py create mode 100644 examples/lookup_hlr_create.py create mode 100644 messagebird/formats.py create mode 100644 messagebird/lookup.py diff --git a/examples/lookup.py b/examples/lookup.py new file mode 100644 index 0000000..4be9b19 --- /dev/null +++ b/examples/lookup.py @@ -0,0 +1,48 @@ +#!/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 : %s' % lookup.phoneNumber) + print(' countryCode : %s' % lookup.countryCode) + print(' countryPrefix : %s' % 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) + +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..74a8e5a --- /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 : %s' % lookup_hlr.msisdn) + print(' network : %s' % 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..b253fa6 --- /dev/null +++ b/examples/lookup_hlr_create.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_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 : %s' % lookup_hlr.msisdn) + print(' network : %s' % 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/messagebird/client.py b/messagebird/client.py index 316d773..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]) @@ -91,3 +92,15 @@ def voice_message_create(self, recipients, body, params={}): params.update({ 'recipients' : recipients, 'body' : body }) 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) From fb0a423980e30ef893af5ac8fa9ce892c35e0e38 Mon Sep 17 00:00:00 2001 From: Sam Wierema Date: Mon, 28 Dec 2015 16:16:53 +0100 Subject: [PATCH 3/4] Also add HLR results to the Lookup example --- examples/lookup.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/lookup.py b/examples/lookup.py index 4be9b19..cf78c27 100644 --- a/examples/lookup.py +++ b/examples/lookup.py @@ -39,6 +39,13 @@ 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.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') From 286b484016376d0971f9aee129169c1e635b7234 Mon Sep 17 00:00:00 2001 From: Sam Wierema Date: Mon, 28 Dec 2015 16:51:33 +0100 Subject: [PATCH 4/4] Some things are ints, print them that way in the Lookup example --- examples/lookup.py | 5 +++-- examples/lookup_hlr.py | 4 ++-- examples/lookup_hlr_create.py | 3 +-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/lookup.py b/examples/lookup.py index cf78c27..81c1940 100644 --- a/examples/lookup.py +++ b/examples/lookup.py @@ -30,9 +30,9 @@ # Print the object information. print('\nThe following information was returned as a Lookup object:\n') print(' href : %s' % lookup.href) - print(' phoneNumber : %s' % lookup.phoneNumber) + print(' phoneNumber : %d' % lookup.phoneNumber) print(' countryCode : %s' % lookup.countryCode) - print(' countryPrefix : %s' % lookup.countryPrefix) + print(' countryPrefix : %d' % lookup.countryPrefix) print(' type : %s' % lookup.type) print(' formats.e164 : %s' % lookup.formats.e164) print(' formats.international : %s' % lookup.formats.international) @@ -41,6 +41,7 @@ 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) diff --git a/examples/lookup_hlr.py b/examples/lookup_hlr.py index 74a8e5a..50a55f9 100644 --- a/examples/lookup_hlr.py +++ b/examples/lookup_hlr.py @@ -31,8 +31,8 @@ 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 : %s' % lookup_hlr.msisdn) - print(' network : %s' % lookup_hlr.network) + 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) diff --git a/examples/lookup_hlr_create.py b/examples/lookup_hlr_create.py index b253fa6..ace135d 100644 --- a/examples/lookup_hlr_create.py +++ b/examples/lookup_hlr_create.py @@ -31,8 +31,7 @@ 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 : %s' % lookup_hlr.msisdn) - print(' network : %s' % lookup_hlr.network) + print(' msisdn : %d' % lookup_hlr.msisdn) print(' reference : %s' % lookup_hlr.reference) print(' status : %s' % lookup_hlr.status) print(' createdDatetime : %s' % lookup_hlr.createdDatetime)