remove to short readtimeout, improved security of calls by removing extra / and enforcing Cotnent-Type

This commit is contained in:
dcore94 2024-01-15 16:38:57 +01:00
parent dfe6593968
commit c9417883da
1 changed files with 7 additions and 6 deletions

View File

@ -34,7 +34,7 @@ class BaseClient(object):
self.baseResource = baseResource
def get(self, resPath, queryParams=None, timeout=(5, 10)):
theUrl = "{}/{}".format(self.baseURL, resPath)
theUrl = "{}/{}".format(self.baseURL, resPath.strip("/"))
resp = requests.get(theUrl, params=queryParams, headers=self.headers, timeout=timeout)
self.__checkForSuccess(resp)
if(resp.content == b''):
@ -43,12 +43,13 @@ class BaseClient(object):
return resp.json()
def post(self, resPath, queryParams, body, headers=None, timeout=(5, 10)):
theUrl = "{}/{}".format(self.baseURL, resPath.lstrip("/"))
theUrl = "{}/{}".format(self.baseURL, resPath.strip("/"))
theHeader = self.headers
if headers is not None:
theHeader = self.mergeTwoDicts(self.headers, headers)
if body is not None:
jsonBody = json.dumps(body, ensure_ascii=True)
print(theUrl, queryParams, theHeader, jsonBody)
resp = requests.post(theUrl, params=queryParams, data=jsonBody, headers=theHeader, timeout=timeout)
else:
resp = requests.post(theUrl, params=queryParams, headers=theHeaders, timeout=timeout)
@ -57,7 +58,7 @@ class BaseClient(object):
return self.__return(resp, theHeader)
def put(self, resPath, queryParams=None, body=None, headers=None, timeout=(5, 10)):
theUrl = "{}/{}".format(self.baseURL, resPath.lstrip("/"))
theUrl = "{}/{}".format(self.baseURL, resPath.strip("/"))
theHeader = self.headers
if headers is not None:
theHeader = self.mergeTwoDicts(self.headers, headers)
@ -72,7 +73,7 @@ class BaseClient(object):
self.__checkForSuccess(resp)
def delete(self, resPath, queryParams, timeout=(5, 10)):
theUrl = "{}/{}".format(self.baseURL, resPath.lstrip("/"))
theUrl = "{}/{}".format(self.baseURL, resPath.strip("/"))
resp = requests.delete(theUrl, params=queryParams, headers=self.headers, timeout=timeout)
self.__print(resp)
self.__checkForSuccess(resp)
@ -182,7 +183,7 @@ class TaskClient(BaseClient):
def updateTask(self, taskObj):
url = self.makeUrl('')
headers = {'Accept': 'text/plain; encodingcharset=utf-8'}
headers = {'Content-Type':'application/json', 'Accept': 'text/plain; encodingcharset=utf-8'}
self.post(url, None, taskObj, headers)
def pollForTask(self, taskType, workerid, domain=None, extraParams=None, reRegisterFunction=None):
@ -196,7 +197,7 @@ class TaskClient(BaseClient):
params = dict(list(extraParams.items()) + list(params.items()))
try:
logging.getLogger("pyexec").debug("Polling at %s with %s", url, params)
return self.get(url, params, timeout=(1, 3))
return self.get(url, params)
except requests.HTTPError as err:
if reRegisterFunction is not None:
reRegisterFunction()