Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.
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
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go_http/__init__.py
Original file line number Diff line number Diff line change
@@ -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',
]
42 changes: 36 additions & 6 deletions go_http/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ 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",
"Authorization": "Bearer %s" % (self.auth_token,),
}
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()

Expand Down Expand Up @@ -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):
"""
Expand Down
36 changes: 30 additions & 6 deletions go_http/tests/test_contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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()
Expand Down Expand Up @@ -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({
Expand Down
5 changes: 2 additions & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -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