Skip to content

Commit 7abf8a9

Browse files
laurynas-convertapiLaurynas Butkus
andauthored
Add token auth support (#44)
* Add token auth support * Remove nose-setenv * Update readme --------- Co-authored-by: Laurynas Butkus <[email protected]>
1 parent f751be7 commit 7abf8a9

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ import convertapi
3434
convertapi.api_secret = 'your-api-secret'
3535
```
3636

37+
If you prefer to use [token and API key](https://www.convertapi.com/doc/auth#token), you can configure like this:
38+
39+
```python
40+
import convertapi
41+
42+
convertapi.api_token = 'your-api-token'
43+
convertapi.api_key = 000000 # your api key
44+
```
45+
46+
If both API secret and token are configured, token will be used.
47+
3748
#### Proxy configuration
3849

3950
If you need to use a proxy, you can specify it using `HTTPS_PROXY` environment variable when running your script.

convertapi/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# configuration
99

1010
api_secret = None
11+
api_key = None
12+
api_token = None
1113
base_uri = 'https://v2.convertapi.com/'
1214
user_agent = 'ConvertAPI-Python/' + __version__
1315
timeout = 1800

convertapi/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def __handle_response(self, r):
5454
return r.json()
5555

5656
def __url(self, path):
57+
if convertapi.api_token != None and convertapi.api_key != None:
58+
return "%s%s?Token=%s&ApiKey=%d" % (convertapi.base_uri, path, convertapi.api_token, convertapi.api_key)
59+
60+
if convertapi.api_token != None:
61+
return "%s%s?Token=%s" % (convertapi.base_uri, path, convertapi.api_token)
62+
5763
return "%s%s?Secret=%s" % (convertapi.base_uri, path, convertapi.api_secret)
5864

5965
def __session(self):

test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
nose
2-
nose-setenv
32
mock
3+
responses

tests/test_convertapi.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import io
44
import tempfile
55
import requests
6+
import responses
67

78
from . import utils
89
from nose.tools import *
910

1011
class TestConvertapi(utils.TestCase):
1112
def setUp(self):
1213
convertapi.api_secret = os.environ['CONVERT_API_SECRET']
14+
convertapi.api_key = None
15+
convertapi.api_token = None
1316
convertapi.max_parallel_uploads = 10
1417

1518
def test_defaults(self):
@@ -79,3 +82,18 @@ def test_api_error(self):
7982
def test_user_info(self):
8083
user_info = convertapi.user()
8184
assert user_info['Active']
85+
86+
@responses.activate
87+
def test_with_token(self):
88+
convertapi.api_token = 'TEST'
89+
responses.add(responses.POST, 'https://v2.convertapi.com/convert/web/to/pdf?Token=TEST', json = { 'ConversionCost': 123 })
90+
result = convertapi.convert('pdf', { 'Url': 'dummyurl' })
91+
assert result.conversion_cost == 123
92+
93+
@responses.activate
94+
def test_with_token_and_api_key(self):
95+
convertapi.api_token = 'TEST'
96+
convertapi.api_key = 1
97+
responses.add(responses.POST, 'https://v2.convertapi.com/convert/web/to/pdf?Token=TEST&ApiKey=1', json = { 'ConversionCost': 123 })
98+
result = convertapi.convert('pdf', { 'Url': 'dummyurl' })
99+
assert result.conversion_cost == 123

0 commit comments

Comments
 (0)