added fail attribute

This commit is contained in:
dcore94 2021-09-17 09:46:23 +02:00
parent e904983fbb
commit 6e401e3dcb
1 changed files with 30 additions and 13 deletions

View File

@ -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()