Skip to content

Switch to authorization header #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,14 @@ 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 = 'your-api-secret-or-token'
```

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.
Expand Down
4 changes: 1 addition & 3 deletions convertapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

# configuration

api_secret = None
api_key = None
api_token = None
api_credentials = None
base_uri = 'https://v2.convertapi.com/'
user_agent = 'ConvertAPI-Python/' + __version__
timeout = 1800
Expand Down
9 changes: 2 additions & 7 deletions convertapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +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)
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
2 changes: 1 addition & 1 deletion examples/conversions_chaining.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import tempfile

convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
convertapi.api_credentials = os.environ['CONVERT_API_SECRET'] # your api secret or 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
Expand Down
2 changes: 1 addition & 1 deletion examples/convert_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io
import tempfile

convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
convertapi.api_credentials = os.environ['CONVERT_API_SECRET'] # your api secret or token

# Example of using content stream to convert to pdf
# https://www.convertapi.com/txt-to-pdf
Expand Down
2 changes: 1 addition & 1 deletion examples/convert_url_to_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import tempfile

convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
convertapi.api_credentials = os.environ['CONVERT_API_SECRET'] # your api secret or token

# Example of converting Web Page URL to PDF file
# https://www.convertapi.com/web-to-pdf
Expand Down
2 changes: 1 addition & 1 deletion examples/convert_word_to_pdf_and_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import tempfile

convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
convertapi.api_credentials = os.environ['CONVERT_API_SECRET'] # your api secret or token

# Example of saving Word docx to PDF and to PNG
# https://www.convertapi.com/docx-to-pdf
Expand Down
2 changes: 1 addition & 1 deletion examples/create_pdf_thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import tempfile

convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
convertapi.api_credentials = os.environ['CONVERT_API_SECRET'] # your api secret or token

# Example of extracting first page from PDF and then chaining conversion PDF page to JPG.
# https://www.convertapi.com/pdf-to-extract
Expand Down
2 changes: 1 addition & 1 deletion examples/retrieve_user_information.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import convertapi
import os

convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
convertapi.api_credentials = os.environ['CONVERT_API_SECRET'] # your api secret or token

# Retrieve user information
# https://www.convertapi.com/doc/user
Expand Down
2 changes: 1 addition & 1 deletion examples/split_and_merge_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import tempfile

convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
convertapi.api_credentials = os.environ['CONVERT_API_SECRET'] # your api secret or 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
Expand Down
25 changes: 4 additions & 21 deletions tests/test_convertapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@

class TestConvertapi(utils.TestCase):
def setUp(self):
convertapi.api_secret = os.environ['CONVERT_API_SECRET']
convertapi.api_key = None
convertapi.api_token = None
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' })
Expand Down Expand Up @@ -71,24 +69,9 @@ 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):
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
Loading