conductor-worker-python/pyexecplugins/Http.py

79 lines
3.3 KiB
Python
Raw Normal View History

2020-10-14 18:06:28 +02:00
import requests
import json
import logging
from requests.structures import CaseInsensitiveDict
2020-10-14 18:06:28 +02:00
from pyexecplugins.pyexecplugins import PyExecPlugin
class Plugin(PyExecPlugin):
name = "Http"
taskdef = {
"name" : "pyrest",
"description" : "Execute an HTTP request with pyrest worker",
"inputKeys" : ["url", "body", "contentType", "method", "accept", "headers", "connectionTimeout", "readTimeout"],
"outputKeys" : ["body", "status", "reason", "headers"],
"ownerEmail" : "m.lettere@gmail.com"
}
2021-03-22 16:36:35 +01:00
def __init__(self, data, config=None):
super().__init__(data, config)
2020-10-14 18:06:28 +02:00
self.method = data.get("method") or "get"
self.url = data.get("url")
self.headers = data.get("headers") or {}
self.headers = CaseInsensitiveDict(self.headers)
2020-10-14 18:06:28 +02:00
self.contenttype = self.headers.get("Content-Type")
self.accept = self.headers.get("Accept")
self.body = data.get("body")
self.params = data.get("params")
self.expect = data.get("expect")
self.fail = data.get("fail")
def doRequest(self):
2020-11-12 18:38:54 +01:00
#logging.getLogger("pyexec").debug("%s - %s - %s - %s",self.method, self.url, self.contenttype, self.accept)
2020-10-14 18:06:28 +02:00
if self.contenttype != None and self.contenttype.find("json") != -1:
2020-11-12 18:38:54 +01:00
self.request = requests.Request(self.method, self.url, headers=self.headers, json = self.body)
2020-10-14 18:06:28 +02:00
else:
2020-11-12 18:38:54 +01:00
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)
2020-10-14 18:06:28 +02:00
return self.response
def computeStatus(self):
if self.fail == False:
return "COMPLETED"
elif self.expect == None:
return "COMPLETED" if self.response.ok else "FAILED"
else:
if type(self.expect) == list:
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]
2020-11-12 18:38:54 +01:00
logging.getLogger("pyexec").info("Response: {} {}".format(self.response.status_code, self.response.reason))
2020-10-14 18:06:28 +02:00
if hdrs.get("Content-Type") != None and hdrs["Content-Type"].find("json") != -1 or self.accept != None and self.accept.find("json") != -1:
outbody = self.response.json() if len(self.response.content) != 0 else None
else:
outbody = self.response.text
2020-11-12 18:38:54 +01:00
logging.getLogger("pyexec").debug("%s", outbody)
2020-10-14 18:06:28 +02:00
if status == "FAILED":
2020-11-12 18:38:54 +01:00
raise Exception("HTTP call failed with status {} ({}) - {}".format(self.response.status_code, self.response.reason, str(outbody)))
2020-10-14 18:06:28 +02:00
return {
"body" : outbody,
"headers" : hdrs,
"status" : self.response.status_code,
"reason" : self.response.reason
}
def execute(self):
self.doRequest()
status = self.computeStatus()
return self.buildOutput(status)