From: Davanum Srinivas Date: Wed, 24 Jul 2013 02:08:20 +0000 (-0400) Subject: Replace urllib2 with requests in urlfetch module X-Git-Tag: 2014.1~327^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=50e177e3c9c207ff832aac6adcafcf81a181a145;p=openstack-build%2Fheat-build.git Replace urllib2 with requests in urlfetch module simple replacement of urllib2.urlopen with requests.get Fixes LP# 1201534 Change-Id: Ia0a2ed3b092e22246339a42f432086dd05f788fa --- diff --git a/heat/common/urlfetch.py b/heat/common/urlfetch.py index bde5f756..05ef8bb2 100644 --- a/heat/common/urlfetch.py +++ b/heat/common/urlfetch.py @@ -17,7 +17,7 @@ Utility for fetching a resource (e.g. a template) from a URL. ''' -import urllib2 +import requests import urlparse from heat.openstack.common import log as logging @@ -38,7 +38,6 @@ def get(url): components = urlparse.urlparse(url) if components.scheme not in ('http', 'https'): - raise urllib2.URLError('Invalid URL scheme %s' % components.scheme) + raise IOError('Invalid URL scheme %s' % components.scheme) - response = urllib2.urlopen(url) - return response.read() + return requests.get(url).text diff --git a/heat/tests/test_urlfetch.py b/heat/tests/test_urlfetch.py index 7d794ef8..7cf29533 100644 --- a/heat/tests/test_urlfetch.py +++ b/heat/tests/test_urlfetch.py @@ -13,17 +13,25 @@ # License for the specific language governing permissions and limitations # under the License. -import StringIO -import urllib2 +import requests from heat.common import urlfetch from heat.tests.common import HeatTestCase +class Response: + def __init__(self, buf=''): + self._text = buf + + @property + def text(self): + return self._text + + class UrlFetchTest(HeatTestCase): def setUp(self): super(UrlFetchTest, self).setUp() - self.m.StubOutWithMock(urllib2, 'urlopen') + self.m.StubOutWithMock(requests, 'get') def test_file_scheme(self): self.m.ReplayAll() @@ -34,7 +42,7 @@ class UrlFetchTest(HeatTestCase): url = 'http://example.com/template' data = '{ "foo": "bar" }' - urllib2.urlopen(url).AndReturn(StringIO.StringIO(data)) + requests.get(url).AndReturn(Response(data)) self.m.ReplayAll() self.assertEqual(urlfetch.get(url), data) @@ -44,7 +52,7 @@ class UrlFetchTest(HeatTestCase): url = 'https://example.com/template' data = '{ "foo": "bar" }' - urllib2.urlopen(url).AndReturn(StringIO.StringIO(data)) + requests.get(url).AndReturn(Response(data)) self.m.ReplayAll() self.assertEqual(urlfetch.get(url), data) @@ -53,7 +61,7 @@ class UrlFetchTest(HeatTestCase): def test_http_error(self): url = 'http://example.com/template' - urllib2.urlopen(url).AndRaise(urllib2.URLError('fubar')) + requests.get(url).AndRaise(IOError('fubar')) self.m.ReplayAll() self.assertRaises(IOError, urlfetch.get, url)