Skip to content

Commit f19ffc6

Browse files
committed
Use requests library.
1 parent 588a03b commit f19ffc6

3 files changed

Lines changed: 48 additions & 55 deletions

File tree

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ nose2==0.5.0
1111
pbr==1.8.1
1212
Pygments==2.0.2
1313
pytz==2015.6
14+
requests==2.8.0
15+
requests-mock==0.6.0
1416
six==1.10.0
1517
snowballstemmer==1.2.0
1618
Sphinx==1.3.1

ziptastic/tests.py

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,49 @@
11
import unittest
22
from mock import patch, Mock
3+
import requests_mock
4+
from nose.tools import eq_
5+
from json import loads
36

47
from ziptastic import Ziptastic
58

6-
compare_v3_json = ["""[{"city": "Owosso", "country": "US",
9+
compare_v3_json = """[{"city": "Owosso", "country": "US",
710
"county": "Shiawassee", "state": "Michigan", "state_short": "MI",
8-
"postal_code": "48867"}]"""]
11+
"postal_code": "48867"}]"""
912

10-
compare_v2_json = ["""{"city": "Owosso", "country": "US",
13+
compare_v2_json = """{"city": "Owosso", "country": "US",
1114
"county": "Shiawassee", "state": "Michigan", "state_short": "MI",
12-
"postal_code": "48867"}"""]
15+
"postal_code": "48867"}"""
1316

1417

1518
class TestZiptasticLib(unittest.TestCase):
16-
def setUp(self):
17-
self.patcher = patch('urllib2.urlopen')
18-
self.urllib_mock = self.patcher.start()
19-
20-
self.urllib2_v3_mock = Mock()
21-
self.urllib2_v3_mock.read.side_effect = compare_v3_json
22-
self.urllib_mock.return_value = self.urllib2_v3_mock
23-
24-
def setUpV3(self):
25-
self.urllib_mock.stop()
26-
self.urllib_mock = self.patcher.start()
27-
self.urllib2_v2_mock = Mock()
28-
self.urllib2_v2_mock.read.side_effect = compare_v2_json
29-
self.urllib_mock.return_value = self.urllib2_v2_mock
30-
31-
def tearDown(self):
32-
self.urllib_mock.stop()
33-
34-
def test_get_from_v3_postal_code(self):
19+
@requests_mock.mock()
20+
def test_get_from_v3_postal_code(self, m):
21+
url = 'https://zip.getziptastic.com/v3/US/48867'
22+
m.get(url, text=compare_v3_json)
3523
postal_code = '48867'
3624
Ziptastic.api_key = 'abc123'
37-
result = Ziptastic.get_from_postal_code(postal_code)
38-
39-
self.assertIn('postal_code', result[0])
40-
self.assertEqual('https://zip.getziptastic.com/v3/US/48867',
41-
self.urllib_mock.call_args[0][0].get_full_url())
25+
ziptastic = Ziptastic('abc123')
26+
result = ziptastic.get_from_postal_code(postal_code)
4227

43-
def test_get_from_v2_postal_code(self):
44-
self.setUpV3()
28+
req = m.request_history[0]
29+
eq_(url, req.url)
30+
eq_(loads(compare_v3_json), result)
4531

32+
@requests_mock.mock()
33+
def test_get_from_v2_postal_code(self, m):
34+
url = 'https://zip.getziptastic.com/v2/US/48867'
35+
m.get(url, text=compare_v2_json)
4636
postal_code = '48867'
47-
result = Ziptastic.get_from_postal_code(postal_code)
37+
ziptastic = Ziptastic('')
38+
result = ziptastic.get_from_postal_code(postal_code)
4839

49-
self.assertIn('postal_code', result)
50-
self.assertEqual('https://zip.getziptastic.com/v2/US/48867',
51-
self.urllib_mock.call_args[0][0].get_full_url())
40+
req = m.request_history[0]
41+
eq_(url, req.url)
42+
eq_(loads(compare_v2_json), result)
5243

5344
def test_build_url(self):
5445
version = 'v42'
5546
endpoint = 'test.endpoint'
56-
correct_url = 'https://test.endpoint/v42/'
47+
correct_url = 'https://test.endpoint/v42'
5748
self.assertEquals(correct_url, Ziptastic.build_url(endpoint,
5849
version=version))
59-
60-
def test_object_is_returned(self):
61-
result = Ziptastic.get_from_postal_code('48867')
62-
self.assertTrue(type(result), 'object')

ziptastic/ziptastic.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
import urllib2
22
import json
3+
import requests
34

45

56
class Ziptastic(object):
67
"""Ziptastic Python Module"""
78

8-
#: This is your Ziptastic API Key that you can get from
9-
#: https://www.getziptastic.com/dashboard
10-
api_key = ''
11-
129
#: The current endpoint where Ziptastic APIs are served from.
1310
endpoint = 'zip.getziptastic.com'
1411

12+
def __init__(self, api_key):
13+
"""
14+
Initialize Ziptastic API
15+
16+
:param: This is your Ziptastic API Key that you can get from
17+
https://www.getziptastic.com/dashboard
18+
"""
19+
self.api_key = api_key
20+
1521
@staticmethod
1622
def build_url(endpoint, version='v3', preferred_protocol='https'):
1723
"""Build the Ziptastic API url."""
18-
return preferred_protocol + '://' + endpoint + '/' + version + '/'
24+
return preferred_protocol + '://' + endpoint + '/' + version
1925

20-
@classmethod
21-
def get_from_postal_code(cls, postal_code, country='US'):
26+
def get_from_postal_code(self, postal_code, country='US'):
2227
"""Get geo data from postal code and country code"""
2328
headers = {}
2429

2530
#: If no api_key is set then default to Version 2 of Ziptastic API.
26-
if cls.api_key:
31+
if self.api_key:
2732
headers.update({
28-
"x-key": cls.api_key
33+
"x-key": self.api_key
2934
})
30-
uri = cls.build_url(cls.endpoint)
35+
uri = self.build_url(self.endpoint)
3136
else:
32-
uri = cls.build_url(cls.endpoint, version='v2')
37+
uri = self.build_url(self.endpoint, version='v2')
3338

34-
url = uri + str(country) + '/' + str(postal_code)
39+
url = "{uri}/{country}/{postal_code}".format(uri=uri, country=country,
40+
postal_code=postal_code)
3541

36-
request = urllib2.Request(url, headers=headers)
37-
contents = urllib2.urlopen(request).read()
42+
r = requests.get(url, headers=headers)
3843

39-
#: An object is returned.
40-
return json.loads(contents)
44+
return r.json()

0 commit comments

Comments
 (0)