# License for the specific language governing permissions and limitations
# under the License.
-import httplib
+import netaddr
+import requests
import urlparse
from cinder.openstack.common import jsonutils
if response:
message = _('%(message)s\nStatus Code: %(_status)s\n'
- 'Body: %(_body)s') % {'_status': response.status,
- '_body': response.read()}
+ 'Body: %(_body)s') % {'_status': response.status_code,
+ '_body': response.text}
super(OpenStackApiException, self).__init__(message)
# default project_id
self.project_id = 'openstack'
- def request(self, url, method='GET', body=None, headers=None):
+ def request(self, url, method='GET', body=None, headers=None,
+ ssl_verify=True, stream=False):
_headers = {'Content-Type': 'application/json'}
_headers.update(headers or {})
hostname = parsed_url.hostname
scheme = parsed_url.scheme
- if scheme == 'http':
- conn = httplib.HTTPConnection(hostname,
- port=port)
- elif scheme == 'https':
- conn = httplib.HTTPSConnection(hostname,
- port=port)
- else:
- raise OpenStackApiException("Unknown scheme: %s" % url)
+ if netaddr.valid_ipv6(hostname):
+ hostname = "[%s]" % hostname
relative_url = parsed_url.path
if parsed_url.query:
if body:
LOG.info(_("Body: %s") % body)
- conn.request(method, relative_url, body, _headers)
- response = conn.getresponse()
+ if port:
+ _url = "%s://%s:%d%s" % (scheme, hostname, int(port), relative_url)
+ else:
+ _url = "%s://%s%s" % (scheme, hostname, relative_url)
+
+ response = requests.request(method, _url, data=body, headers=_headers,
+ verify=ssl_verify, stream=stream)
+
return response
def _authenticate(self):
response = self.request(auth_uri,
headers=headers)
- http_status = response.status
+ http_status = response.status_code
LOG.debug(_("%(auth_uri)s => code %(http_status)s"),
{'auth_uri': auth_uri, 'http_status': http_status})
if http_status == 401:
raise OpenStackApiAuthenticationException(response=response)
- auth_headers = {}
- for k, v in response.getheaders():
- auth_headers[k] = v
-
- self.auth_result = auth_headers
+ self.auth_result = response.headers
return self.auth_result
def api_request(self, relative_uri, check_response_status=None, **kwargs):
response = self.request(full_uri, **kwargs)
- http_status = response.status
+ http_status = response.status_code
LOG.debug(_("%(relative_uri)s => code %(http_status)s"),
{'relative_uri': relative_uri, 'http_status': http_status})
return response
def _decode_json(self, response):
- body = response.read()
+ body = response.text
LOG.debug(_("Decoding JSON: %s") % (body))
if body:
return jsonutils.loads(body)