From 6e401e3dcb53629fb82e4a2ba597ca65d9218398 Mon Sep 17 00:00:00 2001 From: dcore94 Date: Fri, 17 Sep 2021 09:46:23 +0200 Subject: [PATCH] added fail attribute --- pyexecplugins/HttpBridge.py | 43 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/pyexecplugins/HttpBridge.py b/pyexecplugins/HttpBridge.py index da936a0..5954434 100644 --- a/pyexecplugins/HttpBridge.py +++ b/pyexecplugins/HttpBridge.py @@ -8,8 +8,8 @@ class Plugin(PyExecPlugin): taskdef = { "name" : "pyrestbridge", - "description" : "Make a Request to an endpoint and directly stream result to another endpoint. endpoint1 and endpoint2 inputs are JSON objects containing the same keys as PyRest tasks. endpoint1 and endpoint2 outputs contain status, reason and headers of the respective endpoints. Method defaults to GET for endpoint 1 and to POST for endpoint2. Content-type for endpoint2 request will be forwarded from endpoint1 Accepts if present.", - "inputKeys" : ["endpoint1", "endpoint2"], + "description" : "Make a Request to an endpoint and directly stream result to another endpoint. endpoint1 and endpoint2 inputs are JSON objects containing the same keys as PyRest tasks. endpoint1 and endpoint2 outputs contain status, reason and headers of the respective endpoints. Method defaults to GET for endpoint 1 and to POST for endpoint2. Content-type for endpoint2 request will be forwarded from endpoint1 Accepts if present. If fail is set to false the operation will return as completed but will carry error information.", + "inputKeys" : ["endpoint1", "endpoint2", "fail"], "outputKeys" : ["endpoint1", "endpoint2"], "ownerEmail" : "m.lettere@gmail.com" } @@ -36,6 +36,8 @@ class Plugin(PyExecPlugin): self.ep2["params"] = data.get("endpoint2").get("params") self.ep2["files"] = data.get("endpoint2").get("files") self.ep2["expect"] = data.get("endpoint2").get("expect") + + self.fail = data.get("fail") != False def doRequest1(self): self.session = requests.Session() @@ -80,19 +82,34 @@ class Plugin(PyExecPlugin): return "COMPLETED" if (response.status_code == ep["expect"]) else "FAILED" def buildOutput(self, status1, status2): - if status1 == "FAILED": - raise Exception("HTTP call to endpoint1 failed with status {} ({}) - ".format(self.response1.status_code, self.response1.reason)) - elif status2 == "FAILED": - raise Exception("HTTP call to endpoint2 failed with status {} ({}) - ".format(self.response2.status_code, self.response2.reason)) - + msg = None hdrs1 = {} - #for k in self.response1.headers.keys(): hdrs1[k] = self.response1.headers[k] hdrs2 = {} - #for k in self.response2.headers.keys(): hdrs2[k] = self.response2.headers[k] - return { - "endpoint1" : { "status" : self.response1.status_code, "reason" : self.response1.reason, "headers" : hdrs1}, - "endpoint2" : { "status" : self.response2.status_code, "reason" : self.response2.reason, "headers" : hdrs2} - } + if status1 == "FAILED": + msg = "{} to {} failed with status {} ({}) - ".format(self.ep1["method"], self.ep1["url"], self.response1.status_code, self.response1.reason) + if self.fail: + raise Exception(msg) + else: + return { + "endpoint1" : { "status" : self.response1.status_code, "reason" : self.response1.reason, "headers" : hdrs1, "error" : msg}, + "endpoint2" : { } + } + + elif status2 == "FAILED": + msg = "{} to {} failed with status {} ({}) - ".format(self.ep2["method"], self.ep2["url"], self.response2.status_code, self.response2.reason) + if self.fail: + raise Exception(msg) + else: + return { + "endpoint1" : { "status" : self.response1.status_code, "reason" : self.response1.reason, "headers" : hdrs1, "error" : msg}, + "endpoint2" : { "status" : self.response2.status_code, "reason" : self.response2.reason, "headers" : hdrs2, "error" : msg} + } + else: + return { + "endpoint1" : { "status" : self.response1.status_code, "reason" : self.response1.reason, "headers" : hdrs1}, + "endpoint2" : { "status" : self.response2.status_code, "reason" : self.response2.reason, "headers" : hdrs2} + } + def execute(self): self.doRequest1()