apitool: Properly encapsulate HTTP errors in an Exception
... don't just print to stdout from within the API client
This commit is contained in:
parent
da194235fc
commit
72d6ae6603
|
@ -4,7 +4,13 @@ import sys
|
|||
import argparse
|
||||
import requests
|
||||
|
||||
class HttpException(Exception):
|
||||
def __init__(self, api_suffix, response):
|
||||
self.api_suffix = api_suffix
|
||||
self.response = response
|
||||
|
||||
class ApiClient:
|
||||
"""Implementation of an API client for the osmo-lpmgd REST API."""
|
||||
|
||||
def __init__(self, server_host: str, server_port: int):
|
||||
self.server_host = server_host
|
||||
|
@ -15,36 +21,18 @@ class ApiClient:
|
|||
return "http://%s:%u%s%s" % (self.server_host, self.server_port, BASE_PATH, suffix)
|
||||
|
||||
def _rest_get(self, suffix):
|
||||
try:
|
||||
resp = requests.get(self._build_url(suffix))
|
||||
except Exception as e:
|
||||
print("REST GET failed:", e)
|
||||
return
|
||||
resp = requests.get(self._build_url(suffix))
|
||||
if resp.ok:
|
||||
try:
|
||||
js = resp.json()
|
||||
print("%s: %s" % (suffix, js))
|
||||
except:
|
||||
return
|
||||
return resp.json()
|
||||
else:
|
||||
print("%s: Error %s" % (suffix, resp))
|
||||
return None
|
||||
raise HttpException(suffix, resp)
|
||||
|
||||
def _rest_post(self, suffix, js = None):
|
||||
try:
|
||||
resp = requests.post(self._build_url(suffix), json=js)
|
||||
except Exception as e:
|
||||
print("REST POST failed: %s", e)
|
||||
return
|
||||
resp = requests.post(self._build_url(suffix), json=js)
|
||||
if resp.ok:
|
||||
try:
|
||||
js = resp.json()
|
||||
print("%s: %s" % (suffix, js))
|
||||
except:
|
||||
return
|
||||
return resp.json()
|
||||
else:
|
||||
print("%s: Error %s" % (suffix, resp))
|
||||
return None
|
||||
raise HttpException(suffix, resp)
|
||||
|
||||
def get_resource_status(self, resource: str):
|
||||
return self._rest_get("/resource/%s/status" % resource)
|
||||
|
@ -111,16 +99,18 @@ if __name__ == "__main__":
|
|||
api = ApiClient(args.host, args.port)
|
||||
|
||||
if args.operation == "get-status":
|
||||
api.get_resource_status(args.resource)
|
||||
rsp = api.get_resource_status(args.resource)
|
||||
elif args.operation == "obtain-usage-token":
|
||||
api.obtain_usage_token(args.resource, args.user_name, args.usage, args.duration)
|
||||
rsp = api.obtain_usage_token(args.resource, args.user_name, args.usage, args.duration)
|
||||
elif args.operation == "release-usage-token":
|
||||
api.release_usage_token(args.resource, args.token)
|
||||
rsp = api.release_usage_token(args.resource, args.token)
|
||||
elif args.operation == "show-usage-token":
|
||||
api.show_usage_token(args.resource, args.token)
|
||||
rsp = api.show_usage_token(args.resource, args.token)
|
||||
elif args.operation == "list-resources":
|
||||
api.list_resources()
|
||||
rsp = api.list_resources()
|
||||
elif args.operation == "list-tokens":
|
||||
api.list_usage_tokens(args.resource)
|
||||
rsp = api.list_usage_tokens(args.resource)
|
||||
else:
|
||||
raise Exception("Unknown/Unsupported operation")
|
||||
|
||||
print(rsp)
|
||||
|
|
Loading…
Reference in New Issue