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 diff --git a/LICENSE.txt b/LICENSE.txt index eea94d3..37cd8bc 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -3,7 +3,7 @@ https://www.convertapi.com (c) 2011-2018 Baltsoft ConvertAPI.Python -https://github.com/ConvertAPI/convertapi-python +https://github.com/ConvertAPI/convertapi-library-python Copyright (c) 2018 Tomas Rutkauskas The MIT License diff --git a/README.md b/README.md index 13de2b6..28204fe 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # ConvertAPI Python Client [![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) +[![Build Status](https://github.com/ConvertAPI/convertapi-library-python/actions/workflows/main.yml/badge.svg)](https://github.com/ConvertAPI/convertapi-library-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 @@ -26,12 +26,12 @@ Install from source with: ### Configuration -You can get your secret at https://www.convertapi.com/a +You can get your API credentials at https://www.convertapi.com/a ```python import convertapi -convertapi.api_secret = 'your-api-secret' +convertapi.api_credentials = 'api-token' ``` #### Proxy configuration @@ -41,7 +41,7 @@ If you need to use a proxy, you can specify it using `HTTPS_PROXY` environment v Example: ``` -CONVERT_API_SECRET=secret HTTPS_PROXY=https://user:pass@127.0.0.1:9000/ python convert_word_to_pdf_and_png.py +API_TOKEN=api-token HTTPS_PROXY=https://user:pass@127.0.0.1:9000/ python convert_word_to_pdf_and_png.py ``` ### File conversion @@ -100,12 +100,13 @@ result = convertapi.convert( ### User information -You can always check your remaining seconds amount programatically by fetching [user information](https://www.convertapi.com/doc/user). +You can always check your usage by fetching [user information](https://www.convertapi.com/doc/user). ```python user_info = convertapi.user() -print(user_info['SecondsLeft']) +print(user_info['ConversionsTotal']) +print(user_info['ConversionsConsumed']) ``` ### Alternative domain @@ -118,15 +119,15 @@ 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. +Find more advanced examples in the [/examples](https://github.com/ConvertAPI/convertapi-library-python/tree/master/examples) folder. ## Development -Execute `CONVERT_API_SECRET=your_secret nosetests --nocapture` to run the tests. +Execute `API_TOKEN=api-token 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. +Bug reports and pull requests are welcome on GitHub at https://github.com/ConvertAPI/convertapi-library-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 diff --git a/convertapi/__init__.py b/convertapi/__init__.py index 094eb44..1f5ce61 100644 --- a/convertapi/__init__.py +++ b/convertapi/__init__.py @@ -1,4 +1,4 @@ -__version__ = '1.7.0' +__version__ = '2.0.0' from .exceptions import * from .client import Client @@ -7,14 +7,14 @@ # configuration -api_secret = None +api_credentials = None base_uri = 'https://v2.convertapi.com/' user_agent = 'ConvertAPI-Python/' + __version__ timeout = 1800 conversion_timeout = None conversion_timeout_delta = 10 -upload_timeout = 1800 -download_timeout = 1800 +upload_timeout = None +download_timeout = None max_parallel_uploads = 10 verify_ssl = True diff --git a/convertapi/client.py b/convertapi/client.py index 77ce152..206b10b 100644 --- a/convertapi/client.py +++ b/convertapi/client.py @@ -54,11 +54,12 @@ def __handle_response(self, r): return r.json() def __url(self, path): - return "%s%s?Secret=%s" % (convertapi.base_uri, path, convertapi.api_secret) + return convertapi.base_uri + path def __session(self): s = requests.Session() s.headers.update({ 'User-Agent': convertapi.user_agent }) + s.headers.update({ 'Authorization': 'Bearer ' + convertapi.api_credentials }) s.verify = convertapi.verify_ssl return s diff --git a/convertapi/task.py b/convertapi/task.py index af15fc6..261a5bc 100644 --- a/convertapi/task.py +++ b/convertapi/task.py @@ -21,12 +21,8 @@ def run(self): params = self.__normalize_params() from_format = self.from_format or self.__detect_format() timeout = self.timeout + convertapi.conversion_timeout_delta if self.timeout else None - converter = self.__detect_converter() path = "convert/%s/to/%s" % (from_format, self.to_format) - if converter != None: - path += "/converter/%s" % (converter) - response = convertapi.client.post(path, params, timeout = timeout) return Result(response) @@ -62,10 +58,3 @@ def __detect_format(self): if 'Files' in self.params: return format_detector.detect(self.params['Files'][0]) - - def __detect_converter(self): - for k, v in self.params.items(): - if k.lower() == 'converter': - return v - - return None diff --git a/examples/alternative_converter.py b/examples/alternative_converter.py deleted file mode 100644 index 02a2fd1..0000000 --- a/examples/alternative_converter.py +++ /dev/null @@ -1,15 +0,0 @@ -import convertapi -import os -import tempfile - -convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret - -# Example of saving Word docx to PDF using OpenOffice converter -# https://www.convertapi.com/doc-to-pdf/openoffice - -upload_io = convertapi.UploadIO(open('files/test.docx', 'rb')) -params = { 'File': upload_io, 'converter': 'openoffice' } - -saved_files = convertapi.convert('pdf', params).save_files(tempfile.gettempdir()) - -print("The PDF saved to %s" % saved_files) diff --git a/examples/conversions_chaining.py b/examples/conversions_chaining.py index 6cf632b..9e61a36 100644 --- a/examples/conversions_chaining.py +++ b/examples/conversions_chaining.py @@ -2,7 +2,7 @@ import os import tempfile -convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret +convertapi.api_credentials = os.environ['API_TOKEN'] # your api token # Short example of conversions chaining, the PDF pages extracted and saved as separated JPGs and then ZIP'ed # https://www.convertapi.com/doc/chaining diff --git a/examples/convert_stream.py b/examples/convert_stream.py index 1cc8c55..dd21280 100644 --- a/examples/convert_stream.py +++ b/examples/convert_stream.py @@ -3,7 +3,7 @@ import io import tempfile -convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret +convertapi.api_credentials = os.environ['API_TOKEN'] # your api token # Example of using content stream to convert to pdf # https://www.convertapi.com/txt-to-pdf diff --git a/examples/convert_url_to_pdf.py b/examples/convert_url_to_pdf.py index b850b03..8e44273 100644 --- a/examples/convert_url_to_pdf.py +++ b/examples/convert_url_to_pdf.py @@ -2,7 +2,7 @@ import os import tempfile -convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret +convertapi.api_credentials = os.environ['API_TOKEN'] # your api token # Example of converting Web Page URL to PDF file # https://www.convertapi.com/web-to-pdf diff --git a/examples/convert_word_to_pdf_and_png.py b/examples/convert_word_to_pdf_and_png.py index 8645f26..670cd40 100644 --- a/examples/convert_word_to_pdf_and_png.py +++ b/examples/convert_word_to_pdf_and_png.py @@ -2,7 +2,7 @@ import os import tempfile -convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret +convertapi.api_credentials = os.environ['API_TOKEN'] # your api token # Example of saving Word docx to PDF and to PNG # https://www.convertapi.com/docx-to-pdf diff --git a/examples/create_pdf_thumbnail.py b/examples/create_pdf_thumbnail.py index b864b6f..43a7956 100644 --- a/examples/create_pdf_thumbnail.py +++ b/examples/create_pdf_thumbnail.py @@ -2,7 +2,7 @@ import os import tempfile -convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret +convertapi.api_credentials = os.environ['API_TOKEN'] # your api token # Example of extracting first page from PDF and then chaining conversion PDF page to JPG. # https://www.convertapi.com/pdf-to-extract diff --git a/examples/retrieve_user_information.py b/examples/retrieve_user_information.py index 6c65935..f835c11 100644 --- a/examples/retrieve_user_information.py +++ b/examples/retrieve_user_information.py @@ -1,7 +1,7 @@ import convertapi import os -convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret +convertapi.api_credentials = os.environ['API_TOKEN'] # your api token # Retrieve user information # https://www.convertapi.com/doc/user diff --git a/examples/split_and_merge_pdf.py b/examples/split_and_merge_pdf.py index 679d3e8..c5da9b5 100644 --- a/examples/split_and_merge_pdf.py +++ b/examples/split_and_merge_pdf.py @@ -2,7 +2,7 @@ import os import tempfile -convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret +convertapi.api_credentials = os.environ['API_TOKEN'] # your api token # Example of extracting first and last pages from PDF and then merging them back to new PDF. # https://www.convertapi.com/pdf-to-split diff --git a/setup.py b/setup.py index a848eaa..9d0b508 100644 --- a/setup.py +++ b/setup.py @@ -11,8 +11,8 @@ long_description = 'Convert various files like MS Word, Excel, PowerPoint, Images to PDF and Images. Create PDF and Images from url and raw HTML. Extract and create PowerPoint presentation from PDF. Merge, Encrypt, Split, Repair and Decrypt PDF files. All supported files conversions and manipulations can be found at https://www.convertapi.com/doc/supported-formats', author = 'Tomas Rutkauskas', author_email = 'support@convertapi.com', - url = 'https://github.com/ConvertAPI/convertapi-python', - download_url = 'https://github.com/ConvertAPI/convertapi-python', + url = 'https://github.com/ConvertAPI/convertapi-library-python', + download_url = 'https://github.com/ConvertAPI/convertapi-library-python', keywords = ['convert', 'api', 'client', 'conversion'], install_requires= ['requests>=2.4.2'], classifiers = [], 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..bd47e9c 100644 --- a/tests/test_convertapi.py +++ b/tests/test_convertapi.py @@ -3,21 +3,22 @@ import io import tempfile import requests +import responses from . import utils from nose.tools import * class TestConvertapi(utils.TestCase): def setUp(self): - convertapi.api_secret = os.environ['CONVERT_API_SECRET'] + convertapi.api_credentials = os.environ['CONVERT_API_SECRET'] convertapi.max_parallel_uploads = 10 def test_defaults(self): eq_('https://v2.convertapi.com/', convertapi.base_uri) def test_configuration(self): - convertapi.api_secret = 'TEST' - eq_('TEST', convertapi.api_secret) + convertapi.api_credentials = 'TEST' + eq_('TEST', convertapi.api_credentials) def test_convert_file(self): result = convertapi.convert('pdf', { 'File': 'examples/files/test.docx' }) @@ -29,11 +30,6 @@ def test_convert_file_no_parallelizm(self): result = convertapi.convert('pdf', { 'File': 'examples/files/test.docx' }) assert result.save_files(tempfile.gettempdir()) - def test_convert_file_alternative(self): - result = convertapi.convert('pdf', { 'File': 'examples/files/test.docx', 'Converter': 'openoffice' }) - assert result.save_files(tempfile.gettempdir()) - assert result.conversion_cost > 0 - def test_convert_file_url(self): result = convertapi.convert('pdf', { 'File': 'https://cdn.convertapi.com/cara/testfiles/document.docx?test=1' }) assert result.conversion_cost > 0 @@ -73,7 +69,7 @@ def test_compare_files(self): @raises(convertapi.ApiError) def test_api_error(self): - convertapi.api_secret = 'TEST' + convertapi.api_credentials = 'TEST' convertapi.convert('pdf', { 'Url': 'https://www.w3.org/TR/PNG/iso_8859-1.txt' }) def test_user_info(self):