diff options
author | stark, steven <steven.stark@att.com> | 2020-11-05 09:32:35 -0800 |
---|---|---|
committer | stark, steven <steven.stark@att.com> | 2020-11-06 14:02:24 -0800 |
commit | 8d35775b374116182a5262fa6249c989d3d7509e (patch) | |
tree | 153938cd406eb42e6a12efa0e9f346bcf737f7f3 /onap-client/onap_client | |
parent | a0984e365a7c610e9cb91cf73e5d09db2be6df6a (diff) |
[VVP] Adding history attr to oc
Issue-ID: VVP-482
Signed-off-by: stark, steven <steven.stark@att.com>
Change-Id: I57efdc2b8d442d04374a62446b05cfebc5549f36
Diffstat (limited to 'onap-client/onap_client')
-rw-r--r-- | onap-client/onap_client/client/catalog.py | 78 | ||||
-rw-r--r-- | onap-client/onap_client/lib.py | 18 |
2 files changed, 80 insertions, 16 deletions
diff --git a/onap-client/onap_client/client/catalog.py b/onap-client/onap_client/client/catalog.py index fed3acf..989d975 100644 --- a/onap-client/onap_client/client/catalog.py +++ b/onap-client/onap_client/client/catalog.py @@ -42,8 +42,9 @@ import os import importlib from abc import ABC, abstractmethod +from datetime import datetime from onap_client.lib import make_request -from onap_client import config +from onap_client import config, exceptions CACHED_MODULES = {} @@ -58,19 +59,44 @@ def get_modules(): class Catalog(ABC): """Abstract class for an ONAP client, automatically loads child classes as attributes.""" - class CallHandle: """Attached as an attribute for each catalog entry in a catalog. Used to make a request to ONAP.""" - def __init__(self, catalog_resource, verify=False): + def __init__(self, catalog_resource, response_callback=None, verify=False): self.resource = catalog_resource self.verify_request = verify + self.callback = response_callback if response_callback else self.empty_callback + + def empty_callback(self, *args, **kwargs): + pass def __call__(self, **kwargs): - return make_request(self.resource, self.verify_request, **kwargs) + self.callback(message=f"Submitting request: {self.resource.description}") + + response_handler = make_request(self.resource, self.verify_request, **kwargs) + + self.callback(response_handler=response_handler) + + if not response_handler.success: + self.callback(message=f"Request Failure: {self.resource.catalog_resource_name} {response_handler.response_data}") + raise exceptions.RequestFailure( + "Failed making request for catalog item {}: {}".format( + self.resource.catalog_resource_name, + response_handler.response_data + ) + ) - def __init__(self, config_file=None, **kwargs): + self.callback(message="Request was Successful") + + return response_handler + + def __init__( + self, + config_file=None, + history_buffer=[], + **kwargs + ): """Iterates through all child classes and attaches them as attributes, named after the namespace property. @@ -84,9 +110,13 @@ class Catalog(ABC): self.catalog_items = {} self.modules = get_modules() self._config_overrides = kwargs + self.history = history_buffer + + if not self.history: + self.add_to_history("Creating ONAP Client...") for cls in self.__class__.__subclasses__(): - subclass = cls(config_file=config_file, **kwargs) + subclass = cls(config_file=config_file, history_buffer=self.history, **kwargs) namespace = subclass.namespace catalog_resources = subclass.catalog_resources @@ -103,7 +133,41 @@ class Catalog(ABC): resource = APICatalogResource(item_name, resource_data) self.catalog_items[item_name] = resource - setattr(self, item_name.lower(), self.CallHandle(resource, verify=verify)) + + callback = self.add_to_history + + setattr(self, item_name.lower(), self.CallHandle(resource, response_callback=callback, verify=verify)) + + def add_to_history(self, message="", response_handler=None): + if response_handler: + request_object = response_handler.request_object + request_data = {} + if request_object.verb: + request_data["method"] = request_object.verb + + if request_object.uri: + request_data["url"] = request_object.uri + + if request_object.headers: + request_data["headers"] = request_object.headers + + if request_object.payload: + request_data["data"] = request_object.payload + + if request_object.files: + request_data["files"] = str(request_object.files) + + message = request_data + + self.add_message_to_history(message) + + def add_message_to_history(self, message): + current_time = datetime.now() + history_message = { + "date": f"{current_time}", + "message": message + } + self.history.append(history_message) @property @abstractmethod diff --git a/onap-client/onap_client/lib.py b/onap-client/onap_client/lib.py index c87c4df..40024ce 100644 --- a/onap-client/onap_client/lib.py +++ b/onap-client/onap_client/lib.py @@ -58,17 +58,17 @@ def make_request(catalog_item, verify_request, **kwargs): request_handler = RequestHandler(catalog_request) - response_handler = request_handler.make_request(verify_request) + return request_handler.make_request(verify_request) - if not response_handler.success: - raise exceptions.RequestFailure( - "Failed making request for catalog item {}: {}".format( - catalog_item.catalog_resource_name, - response_handler.response_data - ) - ) + # if not response_handler.success: + # raise exceptions.RequestFailure( + # "Failed making request for catalog item {}: {}".format( + # catalog_item.catalog_resource_name, + # response_handler.response_data + # ) + # ) - return response_handler + # return response_handler def validate_request(catalog_item, kwargs): |