python: added request status check for file

This commit is contained in:
Vladimir Kurguzov
2022-11-10 19:07:17 +03:00
parent 751a886bfa
commit b67e768dce
3 changed files with 10 additions and 5 deletions

View File

@ -211,7 +211,9 @@ def createFile(stream, path, req = None, meta = False):
# create file response
def createFileResponse(response, path, req, meta):
response.raise_for_status()
status_code = response.status_code
if status_code != 200: # checking status code
raise RuntimeError('Document editing service returned status: %s' % status_code)
with open(path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
@ -219,7 +221,7 @@ def createFileResponse(response, path, req, meta):
# save file from the given url
def saveFileFromUri(uri, path, req = None, meta = False):
resp = requests.get(uri, stream=True, verify = config.DOC_SERV_VERIFY_PEER)
resp = requests.get(uri, stream=True, verify = config.DOC_SERV_VERIFY_PEER, timeout=5)
createFileResponse(resp, path, req, meta)
return

View File

@ -58,7 +58,10 @@ def getConverterUri(docUri, fromExt, toExt, docKey, isAsync, filePass = None, la
payload['token'] = jwtManager.encode(payload) # encode a payload object into a body token
headers[jwtHeader] = f'Bearer {headerToken}' # add a header Authorization with a header token with Authorization prefix in it
response = requests.post(config.DOC_SERV_SITE_URL + config.DOC_SERV_CONVERTER_URL, json=payload, headers=headers, verify = config.DOC_SERV_VERIFY_PEER) # send the headers and body values to the converter and write the result to the response
response = requests.post(config.DOC_SERV_SITE_URL + config.DOC_SERV_CONVERTER_URL, json=payload, headers=headers, verify = config.DOC_SERV_VERIFY_PEER, timeout=5) # send the headers and body values to the converter and write the result to the response
status_code = response.status_code
if status_code != 200: # checking status code
raise RuntimeError('Convertation service returned status: %s' % status_code)
json = response.json()
return getResponseUri(json)

View File

@ -368,8 +368,8 @@ def track(request):
trackManager.processForceSave(body, filename, usAddr)
except Exception as e:
response.setdefault('error', 1) # set the default error value as 1 (document key is missing or no document with such key could be found)
response.setdefault('message', e.args[0])
response.setdefault("error", 1) # set the default error value as 1 (document key is missing or no document with such key could be found)
response.setdefault("message", str(e.args[0]))
response.setdefault('error', 0) # if no exceptions are raised, the default error value is 0 (no errors)
# the response status is 200 if the changes are saved successfully; otherwise, it is equal to 500