[SEBA-497]

Change to using alpine-grpc-base
Fix issue with nested exceptions causing an error in structlog
Reformat and python3 fixes, v3.5 mock support
Record execution times in the loader

Change-Id: I6d7923818d57012fca32ce44668820de422206d6
diff --git a/src/web_server/main.py b/src/web_server/main.py
index 271b1b2..34a83f5 100644
--- a/src/web_server/main.py
+++ b/src/web_server/main.py
@@ -1,4 +1,3 @@
-
 # Copyright 2017-present Open Networking Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,29 +12,34 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from xosconfig import Config
-from multistructlog import create_logger
-log = create_logger(Config().get('logging'))
+from __future__ import absolute_import, print_function
 
-from grpc_client.main import GRPC_Client
-from klein import Klein
-import os
-from tosca.parser import TOSCA_Parser
-from tosca.default import TOSCA_DEFS_DIR
 import json
+import os
 
-BANNER = """
-   _  ______  _____    __________  _____ _________ 
+from grpc_client import GRPC_Client
+from klein import Klein
+from multistructlog import create_logger
+from tosca.default import TOSCA_DEFS_DIR
+from tosca.parser import TOSCA_Parser
+from xosconfig import Config
+
+log = create_logger(Config().get("logging"))
+
+
+BANNER = r"""
+   _  ______  _____    __________  _____ _________
   | |/ / __ \/ ___/   /_  __/ __ \/ ___// ____/   |
   |   / / / /\__ \     / / / / / /\__ \/ /   / /| |
  /   / /_/ /___/ /    / / / /_/ /___/ / /___/ ___ |
 /_/|_\____//____/    /_/  \____//____/\____/_/  |_|
 """
 
+
 class TOSCA_WebServer:
 
     current_dir = os.path.dirname(os.path.realpath(__file__))
-    template_dir = os.path.join(current_dir, 'templates/')
+    template_dir = os.path.join(current_dir, "templates/")
 
     app = Klein()
 
@@ -59,48 +63,43 @@
             log.info("[XOS-TOSCA] Fatal Error: \n\n", failure=failure)
             return "Internal server error, please report this along with the failed recipe."
 
-    @app.route('/', methods=['GET'])
+    @app.route("/", methods=["GET"])
     def index(self, request):
         request.responseHeaders.addRawHeader(b"content-type", b"application/json")
-        tosca_defs = [f for f in os.listdir(TOSCA_DEFS_DIR) if not f.startswith('.')]
+        tosca_defs = [f for f in os.listdir(TOSCA_DEFS_DIR) if not f.startswith(".")]
 
         response = {}
         for d in tosca_defs:
-            name = d.replace('.yaml', '')
+            name = d.replace(".yaml", "")
             response[name] = "/custom_type/%s" % name
         return json.dumps(response)
 
     @app.route("/custom_type/<name>")
     def custom_type(self, request, name):
         request.responseHeaders.addRawHeader(b"content-type", b"text/plain")
-        custom_type = open(TOSCA_DEFS_DIR + '/' + name + '.yaml').read()
+        custom_type = open(TOSCA_DEFS_DIR + "/" + name + ".yaml").read()
         return custom_type
 
-    @app.route('/run', methods=['POST'])
+    @app.route("/run", methods=["POST"])
     def run(self, request):
-        recipe = request.content.read()
-        headers = request.getAllHeaders()
-        username = headers['xos-username']
-        password = headers['xos-password']
+        return self._handle_post(request, delete=False)
 
-        parser = TOSCA_Parser(recipe, username, password)
-        d = GRPC_Client().create_secure_client(username, password, parser)
-        tosca_execution = d.addCallback(self.execute_tosca)
-        tosca_execution.addErrback(self.errorCallback, request)
-        return d
-
-    @app.route('/delete', methods=['POST'])
+    @app.route("/delete", methods=["POST"])
     def delete(self, request):
-        recipe = request.content.read()
-        headers = request.getAllHeaders()
-        username = headers['xos-username']
-        password = headers['xos-password']
+        return self._handle_post(request, delete=True)
 
-        parser = TOSCA_Parser(recipe, username, password, delete=True)
+    def _handle_post(self, request, delete=False):
+
+        headers = request.getAllHeaders()
+        username = headers["xos-username"]
+        password = headers["xos-password"]
+        recipe = request.content.read()
+
+        parser = TOSCA_Parser(recipe, username, password, delete=delete)
         d = GRPC_Client().create_secure_client(username, password, parser)
         tosca_execution = d.addCallback(self.execute_tosca)
         tosca_execution.addErrback(self.errorCallback, request)
         return d
 
     def __init__(self):
-        self.app.run('0.0.0.0', '9102')
\ No newline at end of file
+        self.app.run("0.0.0.0", "9102")