From c0c5eb7e0e678a601a1cb4e1773a509504192945 Mon Sep 17 00:00:00 2001 From: Kostas <62603039+kostas-baltsoft@users.noreply.github.com> Date: Wed, 16 Aug 2023 13:53:42 +0300 Subject: [PATCH 1/5] Create GETTING-STARTED.md Getting Started page for ConvertAPI web rendering --- GETTING-STARTED.md | 131 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 GETTING-STARTED.md diff --git a/GETTING-STARTED.md b/GETTING-STARTED.md new file mode 100644 index 0000000..d8651a3 --- /dev/null +++ b/GETTING-STARTED.md @@ -0,0 +1,131 @@ +[![PyPI version](https://badge.fury.io/py/convertapi.svg)](https://badge.fury.io/py/convertapi) +[![Build Status](https://github.com/ConvertAPI/convertapi-python/actions/workflows/main.yml/badge.svg)](https://github.com/ConvertAPI/convertapi-python/actions) +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) + +## Convert your files with our online file conversion API + +ConvertAPI helps to convert various file formats. Creating PDFs and Images from various sources like Word, Excel, Powerpoint, images, web pages or raw HTML codes. Merge, Encrypt, Split, Repair and Decrypt PDF files and many other manipulations. You can integrate it into your application in just a few minutes and use it easily. + +## Installation + +Install with [pip](https://pypi.org/project/pip/): + + pip install --upgrade convertapi + +Install from source with: + + python setup.py install + +### Requirements + +* Python 3.3+ + +## Usage + +### Configuration + +You can get your secret at https://www.convertapi.com/a + +```python +import convertapi + +convertapi.api_secret = 'your-api-secret' +``` + +#### Proxy configuration + +If you need to use a proxy, you can specify it using `HTTPS_PROXY` environment variable when running your script. + +Example: + +``` +CONVERT_API_SECRET=secret HTTPS_PROXY=https://user:pass@127.0.0.1:9000/ python convert_word_to_pdf_and_png.py +``` + +### File conversion + +Convert a file to PDF example. All supported file formats and options can be found +[here](https://www.convertapi.com/conversions). + +```python +result = convertapi.convert('pdf', { 'File': '/path/to/my_file.docx' }) + +# save to file +result.file.save('/path/to/save/file.pdf') +``` + +Other result operations: + +```python +# save all result files to folder +result.save_files('/path/to/save/files') + +# get conversion cost +conversion_cost = result.conversion_cost +``` + +#### Convert file url + +```python +result = convertapi.convert('pdf', { 'File': 'https://website/my_file.docx' }) +``` + +#### Specifying from format + +```python +result = convertapi.convert( + 'pdf', + { 'File': '/path/to/my_file' }, + from_format = 'docx' +) +``` + +#### Additional conversion parameters + +ConvertAPI accepts additional conversion parameters depending on selected formats. All conversion +parameters and explanations can be found [here](https://www.convertapi.com/conversions). + +```python +result = convertapi.convert( + 'pdf', + { + 'File': '/path/to/my_file.docx', + 'PageRange': '1-10', + 'PdfResolution': '150', + } +) +``` + +### User information + +You can always check your remaining seconds amount programatically by fetching [user information](https://www.convertapi.com/doc/user). + +```python +user_info = convertapi.user() + +print(user_info['SecondsLeft']) +``` + +### Alternative domain + +Set `base_uri` parameter to use other service domains. Dedicated to the region [domain list](https://www.convertapi.com/doc/servers-location). + +```python +convertapi.base_uri = 'https://eu-v2.convertapi.com/' +``` + +### More examples + +Find more advanced examples in the [/examples](https://github.com/ConvertAPI/convertapi-python/tree/master/examples) folder. + +## Development + +Execute `CONVERT_API_SECRET=your_secret nosetests --nocapture` to run the tests. + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/ConvertAPI/convertapi-python. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). From f751be715bf2413363ff2f68ca99fdaa55d09d49 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Tue, 22 Aug 2023 14:47:31 +0300 Subject: [PATCH 2/5] Delete GETTING-STARTED.md --- GETTING-STARTED.md | 131 --------------------------------------------- 1 file changed, 131 deletions(-) delete mode 100644 GETTING-STARTED.md diff --git a/GETTING-STARTED.md b/GETTING-STARTED.md deleted file mode 100644 index d8651a3..0000000 --- a/GETTING-STARTED.md +++ /dev/null @@ -1,131 +0,0 @@ -[![PyPI version](https://badge.fury.io/py/convertapi.svg)](https://badge.fury.io/py/convertapi) -[![Build Status](https://github.com/ConvertAPI/convertapi-python/actions/workflows/main.yml/badge.svg)](https://github.com/ConvertAPI/convertapi-python/actions) -[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) - -## Convert your files with our online file conversion API - -ConvertAPI helps to convert various file formats. Creating PDFs and Images from various sources like Word, Excel, Powerpoint, images, web pages or raw HTML codes. Merge, Encrypt, Split, Repair and Decrypt PDF files and many other manipulations. You can integrate it into your application in just a few minutes and use it easily. - -## Installation - -Install with [pip](https://pypi.org/project/pip/): - - pip install --upgrade convertapi - -Install from source with: - - python setup.py install - -### Requirements - -* Python 3.3+ - -## Usage - -### Configuration - -You can get your secret at https://www.convertapi.com/a - -```python -import convertapi - -convertapi.api_secret = 'your-api-secret' -``` - -#### Proxy configuration - -If you need to use a proxy, you can specify it using `HTTPS_PROXY` environment variable when running your script. - -Example: - -``` -CONVERT_API_SECRET=secret HTTPS_PROXY=https://user:pass@127.0.0.1:9000/ python convert_word_to_pdf_and_png.py -``` - -### File conversion - -Convert a file to PDF example. All supported file formats and options can be found -[here](https://www.convertapi.com/conversions). - -```python -result = convertapi.convert('pdf', { 'File': '/path/to/my_file.docx' }) - -# save to file -result.file.save('/path/to/save/file.pdf') -``` - -Other result operations: - -```python -# save all result files to folder -result.save_files('/path/to/save/files') - -# get conversion cost -conversion_cost = result.conversion_cost -``` - -#### Convert file url - -```python -result = convertapi.convert('pdf', { 'File': 'https://website/my_file.docx' }) -``` - -#### Specifying from format - -```python -result = convertapi.convert( - 'pdf', - { 'File': '/path/to/my_file' }, - from_format = 'docx' -) -``` - -#### Additional conversion parameters - -ConvertAPI accepts additional conversion parameters depending on selected formats. All conversion -parameters and explanations can be found [here](https://www.convertapi.com/conversions). - -```python -result = convertapi.convert( - 'pdf', - { - 'File': '/path/to/my_file.docx', - 'PageRange': '1-10', - 'PdfResolution': '150', - } -) -``` - -### User information - -You can always check your remaining seconds amount programatically by fetching [user information](https://www.convertapi.com/doc/user). - -```python -user_info = convertapi.user() - -print(user_info['SecondsLeft']) -``` - -### Alternative domain - -Set `base_uri` parameter to use other service domains. Dedicated to the region [domain list](https://www.convertapi.com/doc/servers-location). - -```python -convertapi.base_uri = 'https://eu-v2.convertapi.com/' -``` - -### More examples - -Find more advanced examples in the [/examples](https://github.com/ConvertAPI/convertapi-python/tree/master/examples) folder. - -## Development - -Execute `CONVERT_API_SECRET=your_secret nosetests --nocapture` to run the tests. - -## Contributing - -Bug reports and pull requests are welcome on GitHub at https://github.com/ConvertAPI/convertapi-python. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. - -## License - -The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). From 7abf8a97057390736bbe8076e3cd894fa3621530 Mon Sep 17 00:00:00 2001 From: laurynas-convertapi <38224044+laurynas-convertapi@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:52:58 +0300 Subject: [PATCH 3/5] Add token auth support (#44) * Add token auth support * Remove nose-setenv * Update readme --------- Co-authored-by: Laurynas Butkus --- README.md | 11 +++++++++++ convertapi/__init__.py | 2 ++ convertapi/client.py | 6 ++++++ test-requirements.txt | 2 +- tests/test_convertapi.py | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 13de2b6..a1aaad7 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,17 @@ import convertapi convertapi.api_secret = 'your-api-secret' ``` +If you prefer to use [token and API key](https://www.convertapi.com/doc/auth#token), you can configure like this: + +```python +import convertapi + +convertapi.api_token = 'your-api-token' +convertapi.api_key = 000000 # your api key +``` + +If both API secret and token are configured, token will be used. + #### Proxy configuration If you need to use a proxy, you can specify it using `HTTPS_PROXY` environment variable when running your script. diff --git a/convertapi/__init__.py b/convertapi/__init__.py index 094eb44..4c8f2b2 100644 --- a/convertapi/__init__.py +++ b/convertapi/__init__.py @@ -8,6 +8,8 @@ # configuration api_secret = None +api_key = None +api_token = None base_uri = 'https://v2.convertapi.com/' user_agent = 'ConvertAPI-Python/' + __version__ timeout = 1800 diff --git a/convertapi/client.py b/convertapi/client.py index 77ce152..b0d0d9a 100644 --- a/convertapi/client.py +++ b/convertapi/client.py @@ -54,6 +54,12 @@ def __handle_response(self, r): return r.json() def __url(self, path): + if convertapi.api_token != None and convertapi.api_key != None: + return "%s%s?Token=%s&ApiKey=%d" % (convertapi.base_uri, path, convertapi.api_token, convertapi.api_key) + + if convertapi.api_token != None: + return "%s%s?Token=%s" % (convertapi.base_uri, path, convertapi.api_token) + return "%s%s?Secret=%s" % (convertapi.base_uri, path, convertapi.api_secret) def __session(self): diff --git a/test-requirements.txt b/test-requirements.txt index 8a35f17..5ac06ed 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,3 @@ nose -nose-setenv mock +responses diff --git a/tests/test_convertapi.py b/tests/test_convertapi.py index e66fc49..21c1a63 100644 --- a/tests/test_convertapi.py +++ b/tests/test_convertapi.py @@ -3,6 +3,7 @@ import io import tempfile import requests +import responses from . import utils from nose.tools import * @@ -10,6 +11,8 @@ class TestConvertapi(utils.TestCase): def setUp(self): convertapi.api_secret = os.environ['CONVERT_API_SECRET'] + convertapi.api_key = None + convertapi.api_token = None convertapi.max_parallel_uploads = 10 def test_defaults(self): @@ -79,3 +82,18 @@ def test_api_error(self): def test_user_info(self): user_info = convertapi.user() assert user_info['Active'] + + @responses.activate + def test_with_token(self): + convertapi.api_token = 'TEST' + responses.add(responses.POST, 'https://v2.convertapi.com/convert/web/to/pdf?Token=TEST', json = { 'ConversionCost': 123 }) + result = convertapi.convert('pdf', { 'Url': 'dummyurl' }) + assert result.conversion_cost == 123 + + @responses.activate + def test_with_token_and_api_key(self): + convertapi.api_token = 'TEST' + convertapi.api_key = 1 + responses.add(responses.POST, 'https://v2.convertapi.com/convert/web/to/pdf?Token=TEST&ApiKey=1', json = { 'ConversionCost': 123 }) + result = convertapi.convert('pdf', { 'Url': 'dummyurl' }) + assert result.conversion_cost == 123 From baa07f6ab7fe3d5f165a8119f94eef322739d2fe Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Tue, 2 Apr 2024 19:07:48 +0300 Subject: [PATCH 4/5] Enable github publishing --- .github/workflows/publish.yml | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..791962a --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,51 @@ +name: Publish Python 🐍 distribution 📦 to PyPI + +on: + release: + types: [published] + +jobs: + build: + name: Build distribution 📦 + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.x" + - name: Install pypa/build + run: >- + python3 -m + pip install + build + --user + - name: Build a binary wheel and a source tarball + run: python3 -m build + - name: Store the distribution packages + uses: actions/upload-artifact@v3 + with: + name: python-package-distributions + path: dist/ + + publish-to-pypi: + name: >- + Publish Python 🐍 distribution 📦 to PyPI + needs: + - build + runs-on: ubuntu-latest + environment: + name: pypi + url: https://pypi.org/p/convertapi + permissions: + id-token: write # IMPORTANT: mandatory for trusted publishing + + steps: + - name: Download all the dists + uses: actions/download-artifact@v3 + with: + name: python-package-distributions + path: dist/ + - name: Publish distribution 📦 to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 From ac78774cff6c082f6d8417281f7f0154f9c631d6 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Tue, 2 Apr 2024 19:09:50 +0300 Subject: [PATCH 5/5] Bump version --- convertapi/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/convertapi/__init__.py b/convertapi/__init__.py index 4c8f2b2..aeef32b 100644 --- a/convertapi/__init__.py +++ b/convertapi/__init__.py @@ -1,4 +1,4 @@ -__version__ = '1.7.0' +__version__ = '1.8.0' from .exceptions import * from .client import Client