remove to short readtimeout, improved security of calls by removing extra / and enforcing Cotnent-Type
This commit is contained in:
parent
dfe6593968
commit
c9417883da
|
@ -34,7 +34,7 @@ class BaseClient(object):
|
||||||
self.baseResource = baseResource
|
self.baseResource = baseResource
|
||||||
|
|
||||||
def get(self, resPath, queryParams=None, timeout=(5, 10)):
|
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)
|
resp = requests.get(theUrl, params=queryParams, headers=self.headers, timeout=timeout)
|
||||||
self.__checkForSuccess(resp)
|
self.__checkForSuccess(resp)
|
||||||
if(resp.content == b''):
|
if(resp.content == b''):
|
||||||
|
@ -43,12 +43,13 @@ class BaseClient(object):
|
||||||
return resp.json()
|
return resp.json()
|
||||||
|
|
||||||
def post(self, resPath, queryParams, body, headers=None, timeout=(5, 10)):
|
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
|
theHeader = self.headers
|
||||||
if headers is not None:
|
if headers is not None:
|
||||||
theHeader = self.mergeTwoDicts(self.headers, headers)
|
theHeader = self.mergeTwoDicts(self.headers, headers)
|
||||||
if body is not None:
|
if body is not None:
|
||||||
jsonBody = json.dumps(body, ensure_ascii=True)
|
jsonBody = json.dumps(body, ensure_ascii=True)
|
||||||
|
print(theUrl, queryParams, theHeader, jsonBody)
|
||||||
resp = requests.post(theUrl, params=queryParams, data=jsonBody, headers=theHeader, timeout=timeout)
|
resp = requests.post(theUrl, params=queryParams, data=jsonBody, headers=theHeader, timeout=timeout)
|
||||||
else:
|
else:
|
||||||
resp = requests.post(theUrl, params=queryParams, headers=theHeaders, timeout=timeout)
|
resp = requests.post(theUrl, params=queryParams, headers=theHeaders, timeout=timeout)
|
||||||
|
@ -57,7 +58,7 @@ class BaseClient(object):
|
||||||
return self.__return(resp, theHeader)
|
return self.__return(resp, theHeader)
|
||||||
|
|
||||||
def put(self, resPath, queryParams=None, body=None, headers=None, timeout=(5, 10)):
|
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
|
theHeader = self.headers
|
||||||
if headers is not None:
|
if headers is not None:
|
||||||
theHeader = self.mergeTwoDicts(self.headers, headers)
|
theHeader = self.mergeTwoDicts(self.headers, headers)
|
||||||
|
@ -72,7 +73,7 @@ class BaseClient(object):
|
||||||
self.__checkForSuccess(resp)
|
self.__checkForSuccess(resp)
|
||||||
|
|
||||||
def delete(self, resPath, queryParams, timeout=(5, 10)):
|
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)
|
resp = requests.delete(theUrl, params=queryParams, headers=self.headers, timeout=timeout)
|
||||||
self.__print(resp)
|
self.__print(resp)
|
||||||
self.__checkForSuccess(resp)
|
self.__checkForSuccess(resp)
|
||||||
|
@ -182,7 +183,7 @@ class TaskClient(BaseClient):
|
||||||
|
|
||||||
def updateTask(self, taskObj):
|
def updateTask(self, taskObj):
|
||||||
url = self.makeUrl('')
|
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)
|
self.post(url, None, taskObj, headers)
|
||||||
|
|
||||||
def pollForTask(self, taskType, workerid, domain=None, extraParams=None, reRegisterFunction=None):
|
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()))
|
params = dict(list(extraParams.items()) + list(params.items()))
|
||||||
try:
|
try:
|
||||||
logging.getLogger("pyexec").debug("Polling at %s with %s", url, params)
|
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:
|
except requests.HTTPError as err:
|
||||||
if reRegisterFunction is not None:
|
if reRegisterFunction is not None:
|
||||||
reRegisterFunction()
|
reRegisterFunction()
|
||||||
|
|
Loading…
Reference in New Issue