Skip to content

Commit a27e420

Browse files
committed
Merge pull request #1 from chrishaines/requests
Use requests library
2 parents 588a03b + 3c2e6d8 commit a27e420

4 files changed

Lines changed: 51 additions & 61 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ Official python library for GetZiptastic.com
1313
from ziptastic import Ziptastic
1414

1515
# Set your API key. (Available at https://www.getziptastic.com/dashboard)
16-
Ziptastic.api_key = '<your api key>'
17-
result = Ziptastic.get_from_postal_code('48867')
16+
api = Ziptastic('<your api key>')
17+
result = api.get_from_postal_code('48867')

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ docutils==0.12
55
funcsigs==0.4
66
Jinja2==2.8
77
MarkupSafe==0.23
8-
mock==1.3.0
98
nose==1.3.7
109
nose2==0.5.0
1110
pbr==1.8.1
1211
Pygments==2.0.2
1312
pytz==2015.6
13+
requests==2.8.0
14+
requests-mock==0.6.0
1415
six==1.10.0
1516
snowballstemmer==1.2.0
1617
Sphinx==1.3.1

ziptastic/tests.py

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,47 @@
11
import unittest
2-
from mock import patch, Mock
2+
import requests_mock
3+
from nose.tools import eq_
4+
from json import loads
35

46
from ziptastic import Ziptastic
57

6-
compare_v3_json = ["""[{"city": "Owosso", "country": "US",
8+
compare_v3_json = """[{"city": "Owosso", "country": "US",
79
"county": "Shiawassee", "state": "Michigan", "state_short": "MI",
8-
"postal_code": "48867"}]"""]
10+
"postal_code": "48867"}]"""
911

10-
compare_v2_json = ["""{"city": "Owosso", "country": "US",
12+
compare_v2_json = """{"city": "Owosso", "country": "US",
1113
"county": "Shiawassee", "state": "Michigan", "state_short": "MI",
12-
"postal_code": "48867"}"""]
14+
"postal_code": "48867"}"""
1315

1416

1517
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):
18+
@requests_mock.mock()
19+
def test_get_from_v3_postal_code(self, m):
20+
url = 'https://zip.getziptastic.com/v3/US/48867'
21+
m.get(url, text=compare_v3_json)
3522
postal_code = '48867'
3623
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())
24+
ziptastic = Ziptastic('abc123')
25+
result = ziptastic.get_from_postal_code(postal_code)
4226

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

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

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())
39+
req = m.request_history[0]
40+
eq_(url, req.url)
41+
eq_(loads(compare_v2_json), result)
5242

5343
def test_build_url(self):
5444
version = 'v42'
5545
endpoint = 'test.endpoint'
56-
correct_url = 'https://test.endpoint/v42/'
57-
self.assertEquals(correct_url, Ziptastic.build_url(endpoint,
58-
version=version))
59-
60-
def test_object_is_returned(self):
61-
result = Ziptastic.get_from_postal_code('48867')
62-
self.assertTrue(type(result), 'object')
46+
correct_url = 'https://test.endpoint/v42'
47+
eq_(correct_url, Ziptastic.build_url(endpoint, version=version))

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)