Update sources.py to use new handle_api_result

Use the new function to properly handle error responses for all the
commands.

(cherry picked from commit 3205e47a13)
This commit is contained in:
Brian C. Lane 2018-07-26 10:44:36 -07:00
parent 7e999cc9c6
commit 926d88a3bd

View File

@ -18,7 +18,6 @@ import logging
log = logging.getLogger("composer-cli") log = logging.getLogger("composer-cli")
import os import os
import json
from composer import http_client as client from composer import http_client as client
from composer.cli.help import sources_help from composer.cli.help import sources_help
@ -64,12 +63,12 @@ def sources_list(socket_path, api_version, args, show_json=False):
""" """
api_route = client.api_url(api_version, "/projects/source/list") api_route = client.api_url(api_version, "/projects/source/list")
result = client.get_url_json(socket_path, api_route) result = client.get_url_json(socket_path, api_route)
if show_json: (rc, exit_now) = handle_api_result(result, show_json)
print(json.dumps(result, indent=4)) if exit_now:
return 0 return rc
print("Sources: %s" % ", ".join(result["sources"])) print("Sources: %s" % ", ".join(result["sources"]))
return 0 return rc
def sources_info(socket_path, api_version, args, show_json=False): def sources_info(socket_path, api_version, args, show_json=False):
"""Output info on a list of projects """Output info on a list of projects
@ -92,13 +91,18 @@ def sources_info(socket_path, api_version, args, show_json=False):
if show_json: if show_json:
api_route = client.api_url(api_version, "/projects/source/info/%s" % ",".join(args)) api_route = client.api_url(api_version, "/projects/source/info/%s" % ",".join(args))
result = client.get_url_json(socket_path, api_route) result = client.get_url_json(socket_path, api_route)
print(json.dumps(result, indent=4)) rc = handle_api_result(result, show_json)[0]
return 0
else: else:
api_route = client.api_url(api_version, "/projects/source/info/%s?format=toml" % ",".join(args)) api_route = client.api_url(api_version, "/projects/source/info/%s?format=toml" % ",".join(args))
result = client.get_url_raw(socket_path, api_route) try:
print(result) result = client.get_url_raw(socket_path, api_route)
return 0 print(result)
rc = 0
except RuntimeError as e:
print(str(e))
rc = 1
return rc
def sources_add(socket_path, api_version, args, show_json=False): def sources_add(socket_path, api_version, args, show_json=False):
"""Add or change a source """Add or change a source
@ -123,7 +127,7 @@ def sources_add(socket_path, api_version, args, show_json=False):
source_toml = open(source, "r").read() source_toml = open(source, "r").read()
result = client.post_url_toml(socket_path, api_route, source_toml) result = client.post_url_toml(socket_path, api_route, source_toml)
if handle_api_result(result, show_json): if handle_api_result(result, show_json)[0]:
rval = 1 rval = 1
return rval return rval
@ -144,4 +148,4 @@ def sources_delete(socket_path, api_version, args, show_json=False):
api_route = client.api_url(api_version, "/projects/source/delete/%s" % args[0]) api_route = client.api_url(api_version, "/projects/source/delete/%s" % args[0])
result = client.delete_url_json(socket_path, api_route) result = client.delete_url_json(socket_path, api_route)
return handle_api_result(result, show_json) return handle_api_result(result, show_json)[0]