diff --git a/README.md b/README.md index b651d11..54a9875 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,5 @@ Official python library for GetZiptastic.com from ziptastic import Ziptastic # Set your API key. (Available at https://www.getziptastic.com/dashboard) - Ziptastic.api_key = '' - result = Ziptastic.get_from_postal_code('48867') + api = Ziptastic('') + result = api.get_from_postal_code('48867') diff --git a/requirements.txt b/requirements.txt index 94eefeb..b0fb46c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,12 +5,13 @@ docutils==0.12 funcsigs==0.4 Jinja2==2.8 MarkupSafe==0.23 -mock==1.3.0 nose==1.3.7 nose2==0.5.0 pbr==1.8.1 Pygments==2.0.2 pytz==2015.6 +requests==2.8.0 +requests-mock==0.6.0 six==1.10.0 snowballstemmer==1.2.0 Sphinx==1.3.1 diff --git a/ziptastic/tests.py b/ziptastic/tests.py index dcbad41..1e0fbb7 100644 --- a/ziptastic/tests.py +++ b/ziptastic/tests.py @@ -1,62 +1,47 @@ import unittest -from mock import patch, Mock +import requests_mock +from nose.tools import eq_ +from json import loads from ziptastic import Ziptastic -compare_v3_json = ["""[{"city": "Owosso", "country": "US", +compare_v3_json = """[{"city": "Owosso", "country": "US", "county": "Shiawassee", "state": "Michigan", "state_short": "MI", - "postal_code": "48867"}]"""] + "postal_code": "48867"}]""" -compare_v2_json = ["""{"city": "Owosso", "country": "US", +compare_v2_json = """{"city": "Owosso", "country": "US", "county": "Shiawassee", "state": "Michigan", "state_short": "MI", - "postal_code": "48867"}"""] + "postal_code": "48867"}""" class TestZiptasticLib(unittest.TestCase): - def setUp(self): - self.patcher = patch('urllib2.urlopen') - self.urllib_mock = self.patcher.start() - - self.urllib2_v3_mock = Mock() - self.urllib2_v3_mock.read.side_effect = compare_v3_json - self.urllib_mock.return_value = self.urllib2_v3_mock - - def setUpV3(self): - self.urllib_mock.stop() - self.urllib_mock = self.patcher.start() - self.urllib2_v2_mock = Mock() - self.urllib2_v2_mock.read.side_effect = compare_v2_json - self.urllib_mock.return_value = self.urllib2_v2_mock - - def tearDown(self): - self.urllib_mock.stop() - - def test_get_from_v3_postal_code(self): + @requests_mock.mock() + def test_get_from_v3_postal_code(self, m): + url = 'https://zip.getziptastic.com/v3/US/48867' + m.get(url, text=compare_v3_json) postal_code = '48867' Ziptastic.api_key = 'abc123' - result = Ziptastic.get_from_postal_code(postal_code) - - self.assertIn('postal_code', result[0]) - self.assertEqual('https://zip.getziptastic.com/v3/US/48867', - self.urllib_mock.call_args[0][0].get_full_url()) + ziptastic = Ziptastic('abc123') + result = ziptastic.get_from_postal_code(postal_code) - def test_get_from_v2_postal_code(self): - self.setUpV3() + req = m.request_history[0] + eq_(url, req.url) + eq_(loads(compare_v3_json), result) + @requests_mock.mock() + def test_get_from_v2_postal_code(self, m): + url = 'https://zip.getziptastic.com/v2/US/48867' + m.get(url, text=compare_v2_json) postal_code = '48867' - result = Ziptastic.get_from_postal_code(postal_code) + ziptastic = Ziptastic('') + result = ziptastic.get_from_postal_code(postal_code) - self.assertIn('postal_code', result) - self.assertEqual('https://zip.getziptastic.com/v2/US/48867', - self.urllib_mock.call_args[0][0].get_full_url()) + req = m.request_history[0] + eq_(url, req.url) + eq_(loads(compare_v2_json), result) def test_build_url(self): version = 'v42' endpoint = 'test.endpoint' - correct_url = 'https://test.endpoint/v42/' - self.assertEquals(correct_url, Ziptastic.build_url(endpoint, - version=version)) - - def test_object_is_returned(self): - result = Ziptastic.get_from_postal_code('48867') - self.assertTrue(type(result), 'object') + correct_url = 'https://test.endpoint/v42' + eq_(correct_url, Ziptastic.build_url(endpoint, version=version)) diff --git a/ziptastic/ziptastic.py b/ziptastic/ziptastic.py index 2679b6d..0e00de8 100644 --- a/ziptastic/ziptastic.py +++ b/ziptastic/ziptastic.py @@ -1,40 +1,44 @@ import urllib2 import json +import requests class Ziptastic(object): """Ziptastic Python Module""" - #: This is your Ziptastic API Key that you can get from - #: https://www.getziptastic.com/dashboard - api_key = '' - #: The current endpoint where Ziptastic APIs are served from. endpoint = 'zip.getziptastic.com' + def __init__(self, api_key): + """ + Initialize Ziptastic API + + :param: This is your Ziptastic API Key that you can get from + https://www.getziptastic.com/dashboard + """ + self.api_key = api_key + @staticmethod def build_url(endpoint, version='v3', preferred_protocol='https'): """Build the Ziptastic API url.""" - return preferred_protocol + '://' + endpoint + '/' + version + '/' + return preferred_protocol + '://' + endpoint + '/' + version - @classmethod - def get_from_postal_code(cls, postal_code, country='US'): + def get_from_postal_code(self, postal_code, country='US'): """Get geo data from postal code and country code""" headers = {} #: If no api_key is set then default to Version 2 of Ziptastic API. - if cls.api_key: + if self.api_key: headers.update({ - "x-key": cls.api_key + "x-key": self.api_key }) - uri = cls.build_url(cls.endpoint) + uri = self.build_url(self.endpoint) else: - uri = cls.build_url(cls.endpoint, version='v2') + uri = self.build_url(self.endpoint, version='v2') - url = uri + str(country) + '/' + str(postal_code) + url = "{uri}/{country}/{postal_code}".format(uri=uri, country=country, + postal_code=postal_code) - request = urllib2.Request(url, headers=headers) - contents = urllib2.urlopen(request).read() + r = requests.get(url, headers=headers) - #: An object is returned. - return json.loads(contents) + return r.json()