diff --git a/.travis.yml b/.travis.yml index 9969a54..50414b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,7 @@ install: - "pip install coveralls --use-wheel" - "python setup.py install" script: - - "pep8 --repeat go_http" - - "pyflakes go_http" + - "flake8 go_http" - "py.test --cov=go_http go_http" after_success: - coveralls diff --git a/go_http/__init__.py b/go_http/__init__.py index d2fec79..185cd10 100644 --- a/go_http/__init__.py +++ b/go_http/__init__.py @@ -1,9 +1,9 @@ """Vumi Go HTTP API client library.""" -__version__ = "0.2.3a" - from .send import HttpApiSender, LoggingSender +__version__ = "0.2.3a" + __all__ = [ 'HttpApiSender', 'LoggingSender', ] diff --git a/go_http/contacts.py b/go_http/contacts.py index 46024b1..792f28d 100644 --- a/go_http/contacts.py +++ b/go_http/contacts.py @@ -39,7 +39,8 @@ def __init__(self, auth_token, api_url=None, session=None): session = requests.Session() self.session = session - def _api_request(self, method, api_collection, api_path, data=None): + def _api_request( + self, method, api_collection, api_path, data=None, params=None): url = "%s/%s/%s" % (self.api_url, api_collection, api_path) headers = { "Content-Type": "application/json; charset=utf-8", @@ -47,7 +48,8 @@ def _api_request(self, method, api_collection, api_path, data=None): } if data is not None: data = json.dumps(data) - r = self.session.request(method, url, data=data, headers=headers) + r = self.session.request( + method, url, data=data, headers=headers, params=params) r.raise_for_status() return r.json() @@ -86,14 +88,42 @@ def create_contact(self, contact_data): """ return self._api_request("POST", "contacts", "", contact_data) - def get_contact(self, contact_key): + def _contact_by_key(self, contact_key): + return self._api_request("GET", "contacts", contact_key) + + def _contact_by_field(self, field, value): + contact = self._api_request( + "GET", "contacts", "", params={'query': '%s=%s' % (field, value)}) + return contact.get('data')[0] + + def get_contact(self, *args, **kw): """ - Get a contact. + Get a contact. May either be called as ``.get_contact(contact_key)`` + to get a contact from its key, or ``.get_contact(field=value)``, to + get the contact from an address field ``field`` having a value + ``value``. + + Contact key example: + contact = api.get_contact('abcdef123456') + + Field/value example: + contact = api.get_contact(msisdn='+12345') :param str contact_key: Key for the contact to get. - """ - return self._api_request("GET", "contacts", contact_key) + :param str field: + ``field`` is the address field that is searched on (e.g. ``msisdn`` + , ``twitter_handle``). The value of ``field`` is the value to + search for (e.g. ``+12345``, `@foobar``). + """ + if not kw and len(args) == 1: + return self._contact_by_key(args[0]) + elif len(kw) == 1 and not args: + field, value = kw.items()[0] + return self._contact_by_field(field, value) + raise ValueError( + "get_contact may either be called as .get_contact(contact_key) or" + " .get_contact(field=value)") def update_contact(self, contact_key, update_data): """ diff --git a/go_http/tests/test_contacts.py b/go_http/tests/test_contacts.py index f3ed000..78075e2 100644 --- a/go_http/tests/test_contacts.py +++ b/go_http/tests/test_contacts.py @@ -152,9 +152,11 @@ def test_contacts_multiple_pages(self): })) contacts_api = self.make_client() contacts = list(contacts_api.contacts()) - sort_by_msidn = lambda l: sorted(l, key=lambda d: d['msisdn']) - self.assertEqual( - sort_by_msidn(contacts), sort_by_msidn(expected_contacts)) + + contacts.sort(key=lambda d: d['msisdn']) + expected_contacts.sort(key=lambda d: d['msisdn']) + + self.assertEqual(contacts, expected_contacts) def test_contacts_multiple_pages_with_cursor(self): expected_contacts = [] @@ -174,9 +176,9 @@ def test_contacts_multiple_pages_with_cursor(self): cursor = first_page['cursor'] contacts = list(contacts_api.contacts(start_cursor=cursor)) contacts.extend(first_page['data']) - sort_by_msidn = lambda l: sorted(l, key=lambda d: d['msisdn']) - self.assertEqual( - sort_by_msidn(contacts), sort_by_msidn(expected_contacts)) + contacts.sort(key=lambda d: d['msisdn']) + expected_contacts.sort(key=lambda d: d['msisdn']) + self.assertEqual(contacts, expected_contacts) def test_create_contact(self): contacts = self.make_client() @@ -253,6 +255,28 @@ def test_get_missing_contact(self): contacts = self.make_client() self.assert_http_error(404, contacts.get_contact, "foo") + def test_get_contact_from_field(self): + contacts = self.make_client() + existing_contact = self.make_existing_contact({ + u"msisdn": u"+15556483", + u"name": u"Arthur", + u"surname": u"of Camelot", + }) + + contact = contacts.get_contact(msisdn='+15556483') + self.assertEqual(contact, existing_contact) + + def test_get_contact_from_field_missing(self): + contacts = self.make_client() + self.make_existing_contact({ + u"msisdn": u"+15556483", + u"name": u"Arthur", + u"surname": u"of Camelot", + }) + + self.assert_http_error( + 400, contacts.get_contact, msisdn='+12345') + def test_update_contact(self): contacts = self.make_client() existing_contact = self.make_existing_contact({ diff --git a/requirements-dev.txt b/requirements-dev.txt index d8b51a7..1c7c5ff 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,8 +1,7 @@ -pep8 -pyflakes +flake8 coverage pytest pytest-xdist pytest-cov requests_testadapter -fake-go-contacts>=0.1.3 +fake-go-contacts>=0.1.8