From 8d35775b374116182a5262fa6249c989d3d7509e Mon Sep 17 00:00:00 2001 From: "stark, steven" Date: Thu, 5 Nov 2020 09:32:35 -0800 Subject: [VVP] Adding history attr to oc Issue-ID: VVP-482 Signed-off-by: stark, steven Change-Id: I57efdc2b8d442d04374a62446b05cfebc5549f36 --- onap-client/onap_client/client/catalog.py | 78 ++++++++++++++++++++++++++++--- onap-client/onap_client/lib.py | 18 +++---- onap-client/setup.py | 2 +- 3 files changed, 81 insertions(+), 17 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): diff --git a/onap-client/setup.py b/onap-client/setup.py index 1f2cb0a..9635c4d 100644 --- a/onap-client/setup.py +++ b/onap-client/setup.py @@ -43,7 +43,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="onap-client", - version="1.2.0", + version="1.3.0", author="Steven Stark", author_email="steven.stark@att.com", description="Python API wrapper for ONAP applications", -- cgit 1.2.3-korg