]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Replace urllib2 with requests in urlfetch module
authorDavanum Srinivas <dims@linux.vnet.ibm.com>
Wed, 24 Jul 2013 02:08:20 +0000 (22:08 -0400)
committerDavanum Srinivas <dims@linux.vnet.ibm.com>
Wed, 24 Jul 2013 02:09:55 +0000 (22:09 -0400)
simple replacement of urllib2.urlopen with requests.get

Fixes LP# 1201534

Change-Id: Ia0a2ed3b092e22246339a42f432086dd05f788fa

heat/common/urlfetch.py
heat/tests/test_urlfetch.py

index bde5f75683142c05eb31de631bf0025bb479870c..05ef8bb2c6e7ab427536d4e27be2fa75b2082285 100644 (file)
@@ -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
index 7d794ef8ffcf8acf99ab0e4d23566c45827dc9a6..7cf29533bdd0219baf6f016caa3f40728897e5c8 100644 (file)
 #    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)