Skip to content

Commit

Permalink
Add convenience method for each GET endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AMeng committed Jul 20, 2016
1 parent 6749ba8 commit a81ccfe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Usage
>>> fc = FullContact('your_api_key')

# returns a real requests object
>>> r = fc.api_get('person', **{'email': '[email protected]'})
>>> r = fc.person(email='[email protected]')
>>> r.status_code
200
>>> r.headers['x-rate-limit-remaining']
Expand Down
4 changes: 4 additions & 0 deletions fullcontact/fullcontact.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def __init__(self, api_key):
'batch': 'batch.json'
}

for endpoint in self.get_endpoints:
method = lambda endpoint=endpoint, **kwargs: self.api_get(endpoint, **kwargs)
setattr(self, endpoint, method)

def api_get(self, endpoint, **kwargs):
""" Makes a FullContact API call
Expand Down
21 changes: 12 additions & 9 deletions tests/fullcontact_tests.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
from types import FunctionType
from fullcontact import FullContact
from nose.tools import assert_equal
from nose.tools import assert_equal, assert_true


class TestFullContact(object):
def test_trivial_pass(self):
assert_equal(1, 1)

def test_init(self):
fc = FullContact('')
assert_equal(fc.api_key, '')
fc = FullContact('test_key')
assert_equal(fc.api_key, 'test_key')

def test__prepare_batch_url(self):
fc = FullContact('')
fc = FullContact('test_key')
assert_equal(
fc._prepare_batch_url(('person', {'email': '[email protected]'})),
'https://api.fullcontact.com/v2/person.json?email=test%40test.com'
)

def test_invalid_api_keys(self):
fc = FullContact('')
r = fc.api_get('person', **{'email': '[email protected]'})
fc = FullContact('test_key')
r = fc.person(email='[email protected]')
assert_equal(r.status_code, 403)

test_batch = [
Expand All @@ -29,3 +27,8 @@ def test_invalid_api_keys(self):

r = fc.api_batch(test_batch)
assert_equal(r.status_code, 403)

def test_adds_endpoint_methods(self):
fc = FullContact('')
for endpoint in fc.get_endpoints:
assert_true(isinstance(getattr(fc, endpoint), FunctionType))

0 comments on commit a81ccfe

Please sign in to comment.