From 572e6ea87d93e98e0f66b4bee4742249c35ed5d3 Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Wed, 2 Mar 2016 10:31:09 -0500 Subject: [PATCH 1/6] Test against Python 2 + 3. --- script/test | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/script/test b/script/test index f3d9a34..2dd01aa 100755 --- a/script/test +++ b/script/test @@ -5,24 +5,26 @@ cd "`dirname \"$0\"`/.." cd test # check dependencies -python --version > /dev/null || (echo "python must be installed"; exit 1) +python2 --version > /dev/null || (echo "python2 must be installed"; exit 1) +python3 --version > /dev/null || (echo "python3 must be installed"; exit 1) virtualenv --version > /dev/null || (echo "virtualenv must be installed"; exit 1) -rm -rf venv -which virutualenv || pip install virtualenv -virtualenv venv -source venv/bin/activate -pip install ../ +for version in 2 3; do + rm -rf venv + virtualenv --python=`which python$version` venv + source venv/bin/activate + pip install ../ -# runs a test file with PASS/FAIL message -run_test() { - python $1 && echo "PASS $1" || (echo "FAIL $1"; exit 1) -} + # runs a test file with PASS/FAIL message + run_test() { + python $1 && echo "PASS $1 (Python $version)" || (echo "FAIL $1 (Python $version)"; exit 1) + } -if [ "$1" == "" ]; then - for test in $(ls *.py); do - run_test $test - done -else - run_test $1.py -fi + if [ "$1" == "" ]; then + for test in $(ls *.py); do + run_test $test + done + else + run_test $1.py + fi +done From 20b4a20eb717e5a8b6ce1426efc9360da4679a8d Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Wed, 2 Mar 2016 10:45:41 -0500 Subject: [PATCH 2/6] Fix minor syntax things for Python 3. --- examples/async.py | 16 ++++++++-------- examples/sync.py | 12 ++++++------ test/invalid_async.py | 2 +- test/invalid_sync.py | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/examples/async.py b/examples/async.py index ae06799..5eab6fd 100644 --- a/examples/async.py +++ b/examples/async.py @@ -40,17 +40,17 @@ file = open("/tmp/docraptor-python.pdf", "wb") file.write(doc_response) file.close - print "Wrote PDF to /tmp/docraptor-python.pdf" + print("Wrote PDF to /tmp/docraptor-python.pdf") break elif status_response.status == "failed": - print "FAILED" - print status_response + print("FAILED") + print(status_response) break else: time.sleep(1) -except docraptor.rest.ApiException, error: - print error - print error.message - print error.code - print error.response_body +except docraptor.rest.ApiException as error: + print(error) + print(error.message) + print(error.code) + print(error.response_body) diff --git a/examples/sync.py b/examples/sync.py index 9ac25fe..8bba880 100644 --- a/examples/sync.py +++ b/examples/sync.py @@ -35,10 +35,10 @@ file = open("/tmp/docraptor-python.pdf", "wb") file.write(create_response) file.close - print "Wrote PDF to /tmp/docraptor-python.pdf" + print("Wrote PDF to /tmp/docraptor-python.pdf") -except docraptor.rest.ApiException, error: - print error - print error.message - print error.code - print error.response_body +except docraptor.rest.ApiException as error: + print(error) + print(error.message) + print(error.code) + print(error.response_body) diff --git a/test/invalid_async.py b/test/invalid_async.py index 1461ba4..69d8778 100644 --- a/test/invalid_async.py +++ b/test/invalid_async.py @@ -20,5 +20,5 @@ exit(0) time.sleep(1) -print "Exception expected, but not raised" +print("Exception expected, but not raised") exit(1) diff --git a/test/invalid_sync.py b/test/invalid_sync.py index 83b98a6..225512d 100644 --- a/test/invalid_sync.py +++ b/test/invalid_sync.py @@ -12,9 +12,9 @@ "name": "s" * 201, "document_type": "pdf", }) -except docraptor.rest.ApiException, e: +except docraptor.rest.ApiException as e: exit(0) -print "Exception expected, but not raised" +print("Exception expected, but not raised") exit(1) From 1af723556d1e5231dfe8d9614105e402f1964a91 Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Wed, 2 Mar 2016 11:08:40 -0500 Subject: [PATCH 3/6] Version virtualenv directories for easy manual use. --- script/test | 8 ++++---- test/.gitignore | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/script/test b/script/test index 2dd01aa..e37aa75 100755 --- a/script/test +++ b/script/test @@ -10,10 +10,10 @@ python3 --version > /dev/null || (echo "python3 must be installed"; exit 1) virtualenv --version > /dev/null || (echo "virtualenv must be installed"; exit 1) for version in 2 3; do - rm -rf venv - virtualenv --python=`which python$version` venv - source venv/bin/activate - pip install ../ + rm -rf venv-$version + virtualenv --python=`which python$version` venv-$version + source venv-$version/bin/activate + pip install --upgrade ../ # runs a test file with PASS/FAIL message run_test() { diff --git a/test/.gitignore b/test/.gitignore index f5e96db..e54192c 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1 +1,2 @@ -venv \ No newline at end of file +/venv +/venv-* From 650a1bb57818690a7fed28819458f16f65227927 Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Wed, 2 Mar 2016 11:10:36 -0500 Subject: [PATCH 4/6] Add test for file writing to verify it works easily. --- test/file.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/file.py diff --git a/test/file.py b/test/file.py new file mode 100644 index 0000000..27bf298 --- /dev/null +++ b/test/file.py @@ -0,0 +1,16 @@ +import docraptor +import shutil + +docraptor.configuration.username = "YOUR_API_KEY_HERE" +# docraptor.configuration.debug = True +doc_api = docraptor.DocApi() + +create_response = doc_api.create_doc({ + "test": True, + "document_content": "Hello World", + "name": "docraptor-python.pdf", + "document_type": "pdf", +}) +file = open("/tmp/docraptor-python.pdf", "wb") +file.write(create_response) +file.close From 0cd0b7a7af7ae72f37dd0ce825b1903e98824bd0 Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Wed, 2 Mar 2016 11:11:24 -0500 Subject: [PATCH 5/6] Bypass utf8 encoding if string response is requested. The return value will be str on Python 2, and bytes on Python 3. --- README.md | 3 +++ docraptor/api_client.py | 5 ++++- docraptor/rest.py | 5 ----- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9d2cb26..832d54d 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,9 @@ Stuck? We're experts at using DocRaptor so please [email us](mailto:support@docr The majority of the code in this repo is generated using swagger-codegen on [docraptor.yaml](docraptor.yaml). You can modify this file and regenerate the client using `script/generate_language python`. +The generated client needed a few fixes +- Python3 was forcing all encoding to utf8 on binary data + ## Release Process diff --git a/docraptor/api_client.py b/docraptor/api_client.py index cd8d3fa..57fcac0 100644 --- a/docraptor/api_client.py +++ b/docraptor/api_client.py @@ -233,9 +233,12 @@ def deserialize(self, response, response_type): if "file" == response_type: return self.__deserialize_file(response) + if "str" == response_type: + return response.data + # fetch data from response object try: - data = json.loads(response.data) + data = json.loads(response.data.decode('utf8')) except ValueError: data = response.data diff --git a/docraptor/rest.py b/docraptor/rest.py index c5b9a4e..747b48e 100644 --- a/docraptor/rest.py +++ b/docraptor/rest.py @@ -165,11 +165,6 @@ def request(self, method, url, query_params=None, headers=None, r = RESTResponse(r) - # In the python 3, the response.data is bytes. - # we need to decode it to string. - if sys.version_info > (3,): - r.data = r.data.decode('utf8') - # log response body logger.debug("response body: %s" % r.data) From 714e23b182bcd6c6cc62c238335c95036f456886 Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Wed, 2 Mar 2016 11:14:02 -0500 Subject: [PATCH 6/6] Note Python 3 compatibility. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 832d54d..6befa65 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # DocRaptor Python Native Client Library -This is a Python package for using [DocRaptor API](https://docraptor.com/documentation) to convert [HTML to PDF and XLSX](https://docraptor.com). +This is a Python package for using [DocRaptor API](https://docraptor.com/documentation) to convert [HTML to PDF and XLSX](https://docraptor.com). It is compatible with Python 2 and Python 3. ## Installation