Run black/isort on ara-clients
There will be a follow up patch to add black/isort CI. Change-Id: I8f16ad01d9a7c986b12a543ff351cbf17a0df70d
This commit is contained in:
parent
8834105645
commit
b1dc3a0944
|
@ -20,22 +20,23 @@
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import requests
|
|
||||||
import pbr.version
|
import pbr.version
|
||||||
|
import requests
|
||||||
|
|
||||||
CLIENT_VERSION = pbr.version.VersionInfo(__name__).release_string()
|
CLIENT_VERSION = pbr.version.VersionInfo(__name__).release_string()
|
||||||
|
|
||||||
|
|
||||||
class HttpClient(object):
|
class HttpClient(object):
|
||||||
def __init__(self, endpoint='http://127.0.0.1:8000', timeout=30):
|
def __init__(self, endpoint="http://127.0.0.1:8000", timeout=30):
|
||||||
self.log = logging.getLogger(__name__)
|
self.log = logging.getLogger(__name__)
|
||||||
|
|
||||||
self.endpoint = endpoint
|
self.endpoint = endpoint
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.headers = {
|
self.headers = {
|
||||||
'User-Agent': 'ara-http-client_%s' % CLIENT_VERSION,
|
"User-Agent": "ara-http-client_%s" % CLIENT_VERSION,
|
||||||
'Accept': 'application/json',
|
"Accept": "application/json",
|
||||||
'Content-Type': 'application/json'
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
self.http = requests.Session()
|
self.http = requests.Session()
|
||||||
self.http.headers.update(self.headers)
|
self.http.headers.update(self.headers)
|
||||||
|
@ -51,58 +52,42 @@ class HttpClient(object):
|
||||||
|
|
||||||
def get(self, url, **payload):
|
def get(self, url, **payload):
|
||||||
if payload:
|
if payload:
|
||||||
return self._request('get', url, params=json.dumps(payload))
|
return self._request("get", url, params=json.dumps(payload))
|
||||||
else:
|
else:
|
||||||
return self._request('get', url)
|
return self._request("get", url)
|
||||||
|
|
||||||
def patch(self, url, **payload):
|
def patch(self, url, **payload):
|
||||||
return self._request('patch', url, data=json.dumps(payload))
|
return self._request("patch", url, data=json.dumps(payload))
|
||||||
|
|
||||||
def post(self, url, **payload):
|
def post(self, url, **payload):
|
||||||
return self._request('post', url, data=json.dumps(payload))
|
return self._request("post", url, data=json.dumps(payload))
|
||||||
|
|
||||||
def put(self, url, **payload):
|
def put(self, url, **payload):
|
||||||
return self._request('put', url, data=json.dumps(payload))
|
return self._request("put", url, data=json.dumps(payload))
|
||||||
|
|
||||||
def delete(self, url):
|
def delete(self, url):
|
||||||
return self._request('delete', url)
|
return self._request("delete", url)
|
||||||
|
|
||||||
|
|
||||||
class AraHttpClient(object):
|
class AraHttpClient(object):
|
||||||
def __init__(self, endpoint='http://127.0.0.1:8000', timeout=30):
|
def __init__(self, endpoint="http://127.0.0.1:8000", timeout=30):
|
||||||
self.log = logging.getLogger(__name__)
|
self.log = logging.getLogger(__name__)
|
||||||
self.client = HttpClient(endpoint, timeout)
|
self.client = HttpClient(endpoint, timeout)
|
||||||
|
|
||||||
def _request(self, method, url, **kwargs):
|
def _request(self, method, url, **kwargs):
|
||||||
func = getattr(self.client, method)
|
func = getattr(self.client, method)
|
||||||
if method == 'delete':
|
if method == "delete":
|
||||||
response = func(url)
|
response = func(url)
|
||||||
else:
|
else:
|
||||||
response = func(url, **kwargs)
|
response = func(url, **kwargs)
|
||||||
|
|
||||||
if response.status_code >= 500:
|
if response.status_code >= 500:
|
||||||
self.log.error(
|
self.log.error("Failed to {method} on {url}: {content}".format(method=method, url=url, content=kwargs))
|
||||||
'Failed to {method} on {url}: {content}'.format(
|
|
||||||
method=method,
|
|
||||||
url=url,
|
|
||||||
content=kwargs
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
self.log.debug('HTTP {status}: {method} on {url}'.format(
|
self.log.debug("HTTP {status}: {method} on {url}".format(status=response.status_code, method=method, url=url))
|
||||||
status=response.status_code,
|
|
||||||
method=method,
|
|
||||||
url=url
|
|
||||||
))
|
|
||||||
|
|
||||||
if response.status_code not in [200, 201, 204]:
|
if response.status_code not in [200, 201, 204]:
|
||||||
self.log.error(
|
self.log.error("Failed to {method} on {url}: {content}".format(method=method, url=url, content=kwargs))
|
||||||
'Failed to {method} on {url}: {content}'.format(
|
|
||||||
method=method,
|
|
||||||
url=url,
|
|
||||||
content=kwargs
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if response.status_code == 204:
|
if response.status_code == 204:
|
||||||
return response
|
return response
|
||||||
|
@ -110,16 +95,16 @@ class AraHttpClient(object):
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def get(self, endpoint, **kwargs):
|
def get(self, endpoint, **kwargs):
|
||||||
return self._request('get', endpoint, **kwargs)
|
return self._request("get", endpoint, **kwargs)
|
||||||
|
|
||||||
def patch(self, endpoint, **kwargs):
|
def patch(self, endpoint, **kwargs):
|
||||||
return self._request('patch', endpoint, **kwargs)
|
return self._request("patch", endpoint, **kwargs)
|
||||||
|
|
||||||
def post(self, endpoint, **kwargs):
|
def post(self, endpoint, **kwargs):
|
||||||
return self._request('post', endpoint, **kwargs)
|
return self._request("post", endpoint, **kwargs)
|
||||||
|
|
||||||
def put(self, endpoint, **kwargs):
|
def put(self, endpoint, **kwargs):
|
||||||
return self._request('put', endpoint, **kwargs)
|
return self._request("put", endpoint, **kwargs)
|
||||||
|
|
||||||
def delete(self, endpoint, **kwargs):
|
def delete(self, endpoint, **kwargs):
|
||||||
return self._request('delete', endpoint)
|
return self._request("delete", endpoint)
|
||||||
|
|
|
@ -32,15 +32,15 @@ class AraOfflineClient(object):
|
||||||
from django.core.management import execute_from_command_line
|
from django.core.management import execute_from_command_line
|
||||||
from django.test import Client
|
from django.test import Client
|
||||||
|
|
||||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ara.server.settings')
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ara.server.settings")
|
||||||
|
|
||||||
# Automatically create the database and run migrations (is there a better way?)
|
# Automatically create the database and run migrations (is there a better way?)
|
||||||
execute_from_command_line(['django', 'migrate'])
|
execute_from_command_line(["django", "migrate"])
|
||||||
|
|
||||||
# Set up the things Django needs
|
# Set up the things Django needs
|
||||||
django_setup()
|
django_setup()
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
self.log.error('The offline client requires ara-server to be installed')
|
self.log.error("The offline client requires ara-server to be installed")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
self.client = Client()
|
self.client = Client()
|
||||||
|
@ -49,39 +49,24 @@ class AraOfflineClient(object):
|
||||||
func = getattr(self.client, method)
|
func = getattr(self.client, method)
|
||||||
# TODO: Is there a better way than doing this if/else ?
|
# TODO: Is there a better way than doing this if/else ?
|
||||||
if kwargs:
|
if kwargs:
|
||||||
response = func(
|
response = func(endpoint, json.dumps(kwargs), content_type="application/json")
|
||||||
endpoint,
|
|
||||||
json.dumps(kwargs),
|
|
||||||
content_type='application/json'
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
response = func(
|
response = func(endpoint, content_type="application/json")
|
||||||
endpoint,
|
|
||||||
content_type='application/json'
|
|
||||||
)
|
|
||||||
|
|
||||||
if response.status_code >= 500:
|
if response.status_code >= 500:
|
||||||
self.log.error(
|
self.log.error(
|
||||||
'Failed to {method} on {endpoint}: {content}'.format(
|
"Failed to {method} on {endpoint}: {content}".format(method=method, endpoint=endpoint, content=kwargs)
|
||||||
method=method,
|
|
||||||
endpoint=endpoint,
|
|
||||||
content=kwargs
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.log.debug('HTTP {status}: {method} on {endpoint}'.format(
|
self.log.debug(
|
||||||
status=response.status_code,
|
"HTTP {status}: {method} on {endpoint}".format(
|
||||||
method=method,
|
status=response.status_code, method=method, endpoint=endpoint
|
||||||
endpoint=endpoint
|
)
|
||||||
))
|
)
|
||||||
|
|
||||||
if response.status_code not in [200, 201, 204]:
|
if response.status_code not in [200, 201, 204]:
|
||||||
self.log.error(
|
self.log.error(
|
||||||
'Failed to {method} on {endpoint}: {content}'.format(
|
"Failed to {method} on {endpoint}: {content}".format(method=method, endpoint=endpoint, content=kwargs)
|
||||||
method=method,
|
|
||||||
endpoint=endpoint,
|
|
||||||
content=kwargs
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if response.status_code == 204:
|
if response.status_code == 204:
|
||||||
|
@ -90,16 +75,16 @@ class AraOfflineClient(object):
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def get(self, endpoint, **kwargs):
|
def get(self, endpoint, **kwargs):
|
||||||
return self._request('get', endpoint, **kwargs)
|
return self._request("get", endpoint, **kwargs)
|
||||||
|
|
||||||
def patch(self, endpoint, **kwargs):
|
def patch(self, endpoint, **kwargs):
|
||||||
return self._request('patch', endpoint, **kwargs)
|
return self._request("patch", endpoint, **kwargs)
|
||||||
|
|
||||||
def post(self, endpoint, **kwargs):
|
def post(self, endpoint, **kwargs):
|
||||||
return self._request('post', endpoint, **kwargs)
|
return self._request("post", endpoint, **kwargs)
|
||||||
|
|
||||||
def put(self, endpoint, **kwargs):
|
def put(self, endpoint, **kwargs):
|
||||||
return self._request('put', endpoint, **kwargs)
|
return self._request("put", endpoint, **kwargs)
|
||||||
|
|
||||||
def delete(self, endpoint, **kwargs):
|
def delete(self, endpoint, **kwargs):
|
||||||
return self._request('delete', endpoint, **kwargs)
|
return self._request("delete", endpoint, **kwargs)
|
||||||
|
|
|
@ -15,14 +15,14 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from ara.clients.offline import AraOfflineClient
|
|
||||||
from ara.clients.http import AraHttpClient
|
from ara.clients.http import AraHttpClient
|
||||||
|
from ara.clients.offline import AraOfflineClient
|
||||||
|
|
||||||
|
|
||||||
def get_client(client=None, **kwargs):
|
def get_client(client=None, **kwargs):
|
||||||
if client is None or client == 'offline':
|
if client is None or client == "offline":
|
||||||
return AraOfflineClient(**kwargs)
|
return AraOfflineClient(**kwargs)
|
||||||
elif client == 'http':
|
elif client == "http":
|
||||||
return AraHttpClient(**kwargs)
|
return AraHttpClient(**kwargs)
|
||||||
else:
|
else:
|
||||||
raise ValueError('Unsupported client: %s' % client)
|
raise ValueError("Unsupported client: %s" % client)
|
||||||
|
|
Loading…
Reference in New Issue