Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all query preparation out of the send method #5

Merged
merged 2 commits into from
Apr 16, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 6 additions & 11 deletions rapidsms_multimodem/outgoing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import collections
import logging
import requests
import collections
import pprint

from rapidsms.backends.base import BackendBase

Expand All @@ -23,9 +22,8 @@ def configure(self, sendsms_url, sendsms_user, sendsms_pass,
self.sendsms_pass = sendsms_pass
self.sendsms_params = sendsms_params or {}

def prepare_request(self, id_, text, identities, context):
"""Construct outbound data for requests.get."""
kwargs = {'url': self.sendsms_url}
def prepare_querystring(self, id_, text, identities, context):
"""Construct querystring to pass to requests.get."""
to = ', '.join('"' + identity + '"' for identity in identities)
# Send API requires a specific query params order
params = collections.OrderedDict()
Expand All @@ -51,15 +49,12 @@ def prepare_request(self, id_, text, identities, context):
else:
params['text'] = unicode_to_ismsformat(text)
params.update(self.sendsms_params)
kwargs['params'] = params
return kwargs
return isms_urlencode(params)

def send(self, id_, text, identities, context={}):
logger.debug('Sending message: %s' % text)
kwargs = self.prepare_request(id_, text, identities, context)
logger.debug('params: %s' % pprint.pformat(kwargs["params"]))
params = isms_urlencode(kwargs['params'])
r = requests.get(url=kwargs['url'], params=params)
query_string = self.prepare_querystring(id_, text, identities, context)
r = requests.get(url=self.sendsms_url, params=query_string)
if r.status_code != requests.codes.ok:
r.raise_for_status()
if "Err" in r.text:
Expand Down
41 changes: 21 additions & 20 deletions rapidsms_multimodem/tests/test_outgoing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

from rapidsms.tests.harness import CreateDataMixin

from rapidsms_multimodem.outgoing import MultiModemBackend, ISMS_UNICODE
from rapidsms_multimodem.utils import unicode_to_ismsformat
from rapidsms_multimodem.outgoing import MultiModemBackend, ISMS_ASCII, ISMS_UNICODE


class SendTest(CreateDataMixin, TestCase):
Expand All @@ -25,27 +24,29 @@ def test_required_fields(self):
"""Multimodem backend requires Gateway URL and credentials."""
self.assertRaises(TypeError, MultiModemBackend, None, "multimodem")

def test_prepare_request(self):
def test_prepare_querystring(self):
message = self.create_outgoing_message(data={'text': 'a message'})
data = self.backend.prepare_request(id_=message.id,
text=message.text,
identities=message.connections[0].identity,
context={})
self.assertTrue('url' in data)
self.assertTrue('params' in data)
self.assertEqual(data['params']['text'], 'a message')

def test_prepare_unicode_request(self):
query_string = self.backend.prepare_querystring(id_=message.id,
text=message.text,
identities=message.connections[0].identity,
context={})
self.assertIn('user=admin', query_string)
self.assertIn('passwd=admin', query_string)
self.assertIn('enc={}'.format(ISMS_ASCII), query_string)
self.assertIn('modem=1', query_string)
# just ensure the text param is there. content is tested in test_utils.py
self.assertIn('text=', query_string)

def test_prepare_unicode_querystring(self):
unicode_string = 'Щнпзнмгмжнм'
message = self.create_outgoing_message(data={'text': unicode_string})
data = self.backend.prepare_request(id_=message.id,
text=message.text,
identities=message.connections[0].identity,
context={})
self.assertTrue('url' in data)
self.assertTrue('params' in data)
self.assertEqual(data['params']['enc'], ISMS_UNICODE)
self.assertEqual(data['params']['text'], unicode_to_ismsformat(unicode_string))
query_string = self.backend.prepare_querystring(id_=message.id,
text=message.text,
identities=message.connections[0].identity,
context={})
self.assertIn('enc={}'.format(ISMS_UNICODE), query_string)
# just ensure the text param is there. content is tested in test_utils.py
self.assertIn('text=', query_string)

@patch('rapidsms_multimodem.outgoing.requests')
def test_send(self, mock_requests):
Expand Down