Added `connection-timeout` (int), `connection-timeout` (int) and `no-timeout` (boolean) task parameters to control the newly introduced default timeouts (5s connect, 10s read)
This commit is contained in:
parent
9ad76dd918
commit
114b8e07cf
|
@ -6,7 +6,7 @@ from pyexecplugins.pyexecplugins import PyExecPlugin
|
|||
|
||||
class Plugin(PyExecPlugin):
|
||||
name = "Http"
|
||||
|
||||
|
||||
taskdef = {
|
||||
"name" : "pyrest",
|
||||
"description" : "Execute an HTTP request with pyrest worker",
|
||||
|
@ -14,7 +14,7 @@ class Plugin(PyExecPlugin):
|
|||
"outputKeys" : ["body", "status", "reason", "headers"],
|
||||
"ownerEmail" : "m.lettere@gmail.com"
|
||||
}
|
||||
|
||||
|
||||
def __init__(self, data, config=None):
|
||||
super().__init__(data, config)
|
||||
self.method = data.get("method") or "get"
|
||||
|
@ -27,7 +27,10 @@ class Plugin(PyExecPlugin):
|
|||
self.params = data.get("params")
|
||||
self.expect = data.get("expect")
|
||||
self.fail = data.get("fail")
|
||||
|
||||
self.conntimeout = data.get("connection-timeout") or 5
|
||||
self.readtimeout = data.get("read-timeout") or 10
|
||||
self.notimeout = (data.get("no-timeout") in (True, "true", "True", "TRUE", 1, "1", "y", "Y", "yes", "YES")) or False
|
||||
|
||||
def doRequest(self):
|
||||
#logging.getLogger("pyexec").debug("%s - %s - %s - %s",self.method, self.url, self.contenttype, self.accept)
|
||||
if self.contenttype != None and self.contenttype.find("json") != -1:
|
||||
|
@ -36,10 +39,16 @@ class Plugin(PyExecPlugin):
|
|||
self.request = requests.Request(self.method, self.url, headers=self.headers, data = self.body.encode('utf-8'))
|
||||
else:
|
||||
self.request = requests.Request(self.method, self.url, headers=self.headers, data = self.body, params = self.params)
|
||||
|
||||
|
||||
self.request = self.request.prepare()
|
||||
logging.getLogger("pyexec").debug("%s url=%s body=%s",self.method, self.request.url,self.body)
|
||||
self.response = requests.Session().send(self.request)
|
||||
if self.notimeout:
|
||||
timeout = None
|
||||
else:
|
||||
timeout= (self.conntimeout, self.readtimeout)
|
||||
|
||||
print("timeout: ", timeout)
|
||||
logging.getLogger("pyexec").debug("%s url=%s body=%s conntimeout=%d readtimeout=%d",self.method, self.request.url,self.body,self.conntimeout,self.readtimeout)
|
||||
self.response = requests.Session().send(self.request, timeout=timeout)
|
||||
return self.response
|
||||
|
||||
def computeStatus(self):
|
||||
|
@ -52,7 +61,7 @@ class Plugin(PyExecPlugin):
|
|||
return "COMPLETED" if (self.response.status_code in self.expect) else "FAILED"
|
||||
else:
|
||||
return "COMPLETED" if (self.response.status_code == self.expect) else "FAILED"
|
||||
|
||||
|
||||
def buildOutput(self, status):
|
||||
hdrs = {}
|
||||
for k in self.response.headers.keys(): hdrs[k] = self.response.headers[k]
|
||||
|
@ -63,7 +72,7 @@ class Plugin(PyExecPlugin):
|
|||
outbody = self.response.json() if len(self.response.content) != 0 else None
|
||||
else:
|
||||
outbody = self.response.text
|
||||
|
||||
|
||||
logging.getLogger("pyexec").debug("%s", outbody)
|
||||
if status == "FAILED":
|
||||
raise Exception("HTTP call failed with status {} ({}) - {}".format(self.response.status_code, self.response.reason, str(outbody)))
|
||||
|
@ -73,7 +82,7 @@ class Plugin(PyExecPlugin):
|
|||
"status" : self.response.status_code,
|
||||
"reason" : self.response.reason
|
||||
}
|
||||
|
||||
|
||||
def execute(self):
|
||||
self.doRequest()
|
||||
status = self.computeStatus()
|
||||
|
|
Loading…
Reference in New Issue