From b67e768dce896b5dd107db7ce236abb3cd00487a Mon Sep 17 00:00:00 2001 From: Vladimir Kurguzov Date: Thu, 10 Nov 2022 19:07:17 +0300 Subject: [PATCH] python: added request status check for file --- web/documentserver-example/python/src/utils/docManager.py | 6 ++++-- .../python/src/utils/serviceConverter.py | 5 ++++- web/documentserver-example/python/src/views/actions.py | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/web/documentserver-example/python/src/utils/docManager.py b/web/documentserver-example/python/src/utils/docManager.py index 3f22fd00..708ecd30 100755 --- a/web/documentserver-example/python/src/utils/docManager.py +++ b/web/documentserver-example/python/src/utils/docManager.py @@ -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 diff --git a/web/documentserver-example/python/src/utils/serviceConverter.py b/web/documentserver-example/python/src/utils/serviceConverter.py index dea90579..af7ba752 100755 --- a/web/documentserver-example/python/src/utils/serviceConverter.py +++ b/web/documentserver-example/python/src/utils/serviceConverter.py @@ -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) diff --git a/web/documentserver-example/python/src/views/actions.py b/web/documentserver-example/python/src/views/actions.py index e5e00b89..6d3d9e3f 100755 --- a/web/documentserver-example/python/src/views/actions.py +++ b/web/documentserver-example/python/src/views/actions.py @@ -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