HTTPClient improvements
Change-Id: Ic9256ac255e0392634c90047363a6069f970e881
This commit is contained in:
parent
97dbeb3686
commit
bfb3ea44f7
@ -1,3 +1,4 @@
|
|||||||
oslo.serialization
|
oslo.serialization
|
||||||
prettytable
|
prettytable
|
||||||
pbr==0.10.4
|
pbr==0.10.4
|
||||||
|
six
|
||||||
|
@ -13,16 +13,21 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
from six.moves import http_client as httplib
|
||||||
|
|
||||||
|
from surveilclient.openstack.common.py3kcompat import urlutils
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
import httplib
|
|
||||||
import json
|
import json
|
||||||
import urlparse
|
|
||||||
|
|
||||||
|
USER_AGENT = 'python-surveilclient'
|
||||||
|
|
||||||
|
|
||||||
class HTTPClient(object):
|
class HTTPClient(object):
|
||||||
|
|
||||||
def __init__(self, endpoint):
|
def __init__(self, endpoint):
|
||||||
endpoint_parts = urlparse.urlparse(endpoint)
|
endpoint_parts = urlutils.urlparse(endpoint)
|
||||||
self.endpoint_hostname = endpoint_parts.hostname
|
self.endpoint_hostname = endpoint_parts.hostname
|
||||||
self.endpoint_port = endpoint_parts.port
|
self.endpoint_port = endpoint_parts.port
|
||||||
self.endpoint_path = endpoint_parts.path
|
self.endpoint_path = endpoint_parts.path
|
||||||
@ -36,20 +41,24 @@ class HTTPClient(object):
|
|||||||
|
|
||||||
return con
|
return con
|
||||||
|
|
||||||
|
def _http_request(self, url, method, **kwargs):
|
||||||
|
"""Send an http request with the specified characteristics.
|
||||||
|
|
||||||
|
Wrapper around httplib.HTTP(S)Connection.request to handle tasks such
|
||||||
|
as setting headers and error handling.
|
||||||
|
"""
|
||||||
|
kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {}))
|
||||||
|
kwargs['headers'].setdefault('User-Agent', USER_AGENT)
|
||||||
|
conn = self.get_connection()
|
||||||
|
conn.request(method, self.endpoint_path + url, **kwargs)
|
||||||
|
resp = conn.getresponse()
|
||||||
|
return resp
|
||||||
|
|
||||||
def json_request(self, url, method, **kwargs):
|
def json_request(self, url, method, **kwargs):
|
||||||
"""Send an http request with the specified characteristics.
|
"""Send an http request with the specified characteristics.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
conn = self.get_connection()
|
|
||||||
|
|
||||||
kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {}))
|
kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {}))
|
||||||
kwargs['headers'].setdefault('Content-Type', 'application/json')
|
kwargs['headers'].setdefault('Content-Type', 'application/json')
|
||||||
|
resp = self._http_request(url, method, **kwargs)
|
||||||
conn.request(
|
return resp, json.loads(resp.read().decode())
|
||||||
method,
|
|
||||||
self.endpoint_path + url,
|
|
||||||
headers=kwargs['headers']
|
|
||||||
)
|
|
||||||
|
|
||||||
resp = conn.getresponse()
|
|
||||||
return json.loads(resp.read())
|
|
||||||
|
61
surveilclient/openstack/common/py3kcompat/urlutils.py
Normal file
61
surveilclient/openstack/common/py3kcompat/urlutils.py
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#
|
||||||
|
# Copyright 2013 Canonical Ltd.
|
||||||
|
# All Rights Reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Python2/Python3 compatibility layer for OpenStack
|
||||||
|
"""
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
|
if six.PY3:
|
||||||
|
# python3
|
||||||
|
import urllib.error
|
||||||
|
import urllib.parse
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
urlencode = urllib.parse.urlencode
|
||||||
|
urljoin = urllib.parse.urljoin
|
||||||
|
quote = urllib.parse.quote
|
||||||
|
parse_qsl = urllib.parse.parse_qsl
|
||||||
|
unquote = urllib.parse.unquote
|
||||||
|
urlparse = urllib.parse.urlparse
|
||||||
|
urlsplit = urllib.parse.urlsplit
|
||||||
|
urlunsplit = urllib.parse.urlunsplit
|
||||||
|
|
||||||
|
urlopen = urllib.request.urlopen
|
||||||
|
URLError = urllib.error.URLError
|
||||||
|
pathname2url = urllib.request.pathname2url
|
||||||
|
else:
|
||||||
|
# python2
|
||||||
|
import urllib
|
||||||
|
import urllib2
|
||||||
|
import urlparse
|
||||||
|
|
||||||
|
urlencode = urllib.urlencode
|
||||||
|
quote = urllib.quote
|
||||||
|
unquote = urllib.unquote
|
||||||
|
|
||||||
|
parse = urlparse
|
||||||
|
parse_qsl = parse.parse_qsl
|
||||||
|
urljoin = parse.urljoin
|
||||||
|
urlparse = parse.urlparse
|
||||||
|
urlsplit = parse.urlsplit
|
||||||
|
urlunsplit = parse.urlunsplit
|
||||||
|
|
||||||
|
urlopen = urllib2.urlopen
|
||||||
|
URLError = urllib2.URLError
|
||||||
|
pathname2url = urllib.pathname2url
|
@ -33,9 +33,9 @@ class TestHttp(unittest.TestCase):
|
|||||||
self.surveil_url + "/test",
|
self.surveil_url + "/test",
|
||||||
body=json.dumps(example_result))
|
body=json.dumps(example_result))
|
||||||
|
|
||||||
result = self.client.json_request('/test', 'GET')
|
resp, body = self.client.json_request('/test', 'GET')
|
||||||
self.assertEqual(httpretty.last_request().method, 'GET')
|
self.assertEqual(httpretty.last_request().method, 'GET')
|
||||||
self.assertEqual(result, example_result)
|
self.assertEqual(body, example_result)
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
httpretty.last_request().headers['Content-Type'],
|
httpretty.last_request().headers['Content-Type'],
|
||||||
|
@ -20,5 +20,7 @@ class HostsManager(surveil_manager.SurveilManager):
|
|||||||
|
|
||||||
def list(self):
|
def list(self):
|
||||||
"""Get a list of hosts."""
|
"""Get a list of hosts."""
|
||||||
hosts = self.http_client.json_request(HostsManager.base_url, 'GET')
|
resp, body = self.http_client.json_request(
|
||||||
return hosts
|
HostsManager.base_url, 'GET'
|
||||||
|
)
|
||||||
|
return body
|
||||||
|
@ -20,5 +20,7 @@ class ServicesManager(surveil_manager.SurveilManager):
|
|||||||
|
|
||||||
def list(self):
|
def list(self):
|
||||||
"""Get a list of hosts."""
|
"""Get a list of hosts."""
|
||||||
hosts = self.http_client.json_request(ServicesManager.base_url, 'GET')
|
resp, body = self.http_client.json_request(
|
||||||
return hosts
|
ServicesManager.base_url, 'GET'
|
||||||
|
)
|
||||||
|
return body
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
hacking>=0.9.2,<0.10
|
hacking>=0.9.2,<0.10
|
||||||
sphinx
|
sphinx
|
||||||
oslosphinx
|
oslosphinx
|
||||||
six
|
|
||||||
testrepository
|
testrepository
|
||||||
mox3>=0.7.0
|
mox3>=0.7.0
|
||||||
httpretty
|
httpretty
|
||||||
|
Loading…
x
Reference in New Issue
Block a user