diff options
4 files changed, 28 insertions, 22 deletions
diff --git a/testapi/testapi-client/testapiclient/cli/pods.py b/testapi/testapi-client/testapiclient/cli/pods.py index 8b2f3af..9cadee7 100644 --- a/testapi/testapi-client/testapiclient/cli/pods.py +++ b/testapi/testapi-client/testapiclient/cli/pods.py @@ -3,15 +3,15 @@ import json from testapiclient.utils import command from testapiclient.utils import http_client as client from testapiclient.utils import identity -from testapiclient.utils import url_parse +from testapiclient.utils import url_parse as up def pods_url(): - return url_parse.resource_join('pods') + return up.resource_join('pods') def pod_url(parsed_args): - return url_parse.path_join(pods_url(), parsed_args.name) + return up.path_join(pods_url(), parsed_args.name) class PodGet(command.Lister): @@ -34,10 +34,8 @@ class PodGet(command.Lister): "creation_date", ) - data = client.get( - self.filter_by_name(pods_url(), parsed_args)).get('pods', []) - - return self.format_output(columns, data) + data = client.get(up.query_by(pods_url(), 'name', parsed_args)) + return self.format_output(columns, data.get('pods', [])) class PodGetOne(command.ShowOne): diff --git a/testapi/testapi-client/testapiclient/cli/projects.py b/testapi/testapi-client/testapiclient/cli/projects.py index 94a8fac..5fa6894 100644 --- a/testapi/testapi-client/testapiclient/cli/projects.py +++ b/testapi/testapi-client/testapiclient/cli/projects.py @@ -3,15 +3,15 @@ import json from testapiclient.utils import command from testapiclient.utils import http_client as client from testapiclient.utils import identity -from testapiclient.utils import url_parse +from testapiclient.utils import url_parse as up def projects_url(): - return url_parse.resource_join('projects') + return up.resource_join('projects') def project_url(parsed_args): - return url_parse.path_join(projects_url(), parsed_args.name) + return up.path_join(projects_url(), parsed_args.name) class ProjectGet(command.Lister): @@ -30,10 +30,8 @@ class ProjectGet(command.Lister): 'creator', 'creation_date' ) - data = client.get( - self.filter_by_name(projects_url(), - parsed_args)).get('projects', []) - return self.format_output(columns, data) + data = client.get(up.query_by(projects_url(), 'name', parsed_args)) + return self.format_output(columns, data.get('project', [])) class ProjectGetOne(command.ShowOne): diff --git a/testapi/testapi-client/testapiclient/utils/command.py b/testapi/testapi-client/testapiclient/utils/command.py index c99a3f3..9614acf 100644 --- a/testapi/testapi-client/testapiclient/utils/command.py +++ b/testapi/testapi-client/testapiclient/utils/command.py @@ -7,7 +7,6 @@ from cliff import show import six from testapiclient import utils -from testapiclient.utils import url_parse class CommandMeta(abc.ABCMeta): @@ -39,13 +38,6 @@ class Command(command.Command): class Lister(Command, lister.Lister): @staticmethod - def filter_by_name(url, parsed_args): - def query_url(): - return url_parse.query_join(url, name=parsed_args.name) - - return query_url() if parsed_args.name else url - - @staticmethod def format_output(columns, data): return (columns, (utils.get_item_properties(s, columns) for s in data)) diff --git a/testapi/testapi-client/testapiclient/utils/url_parse.py b/testapi/testapi-client/testapiclient/utils/url_parse.py index 08f7a63..fbc503a 100644 --- a/testapi/testapi-client/testapiclient/utils/url_parse.py +++ b/testapi/testapi-client/testapiclient/utils/url_parse.py @@ -20,3 +20,21 @@ def query_join(base, **queries): def resource_join(url): testapi_url = os.environ.get('testapi_url') return path_join(testapi_url, url) + + +def get_queries(queries, parsed_args): + if not isinstance(queries, list): + queries = [queries] + + return {query: getattr(parsed_args, query) + for query in queries + if hasattr(parsed_args, query)} + + +def query_by(base, queries, parsed_args): + return query_join(base, + **get_queries(queries, parsed_args)) + + +def url_format(base, parsed_args): + return base.format(**(parsed_args.__dict__)) |