Skip to content

Commit ab387e7

Browse files
committed
modify client to take key and token auth at request time
1 parent ce570d0 commit ab387e7

File tree

4 files changed

+81
-28
lines changed

4 files changed

+81
-28
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,23 @@ The library needs to be configured with your account's API key which is availabl
2222

2323
### Setup
2424

25+
When using an API key you can initialize the client to use it for all future
26+
requests.
27+
2528
```python
2629
import emailable
2730

2831
client = emailable.Client('live_...')
2932
```
3033

34+
Alternatively, you can pass an OAuth access token to any of the endpoints. See
35+
[here](https://emailable.com/docs/api/#oauth) for more details.
36+
37+
```python
38+
client = emailable.Client()
39+
client.verify('[email protected]', access_token=<access_token>)
40+
```
41+
3142
### Verification
3243

3344
```python
@@ -36,7 +47,7 @@ response = client.verify('[email protected]')
3647
response.state
3748
=> 'deliverable'
3849

39-
# additional parameters are available. see API docs for additional info.
50+
# additional parameters are available. see API docs for more info.
4051
client.verify('[email protected]', smtp=False, accept_all=True, timeout=25)
4152
```
4253

emailable/client.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@
66

77
class Client:
88

9-
def __init__(self, api_key):
9+
def __init__(self, api_key=None):
1010
self.api_key = api_key
1111
self.base_url = 'https://api.emailable.com/v1/'
1212

13-
def verify(self, email, smtp=True, accept_all=False, timeout=None):
13+
def verify(self,
14+
email,
15+
smtp=True,
16+
accept_all=False,
17+
timeout=None,
18+
api_key=None,
19+
access_token=None):
1420
options = {
21+
'headers': {
22+
'Authorization': f'Bearer {self.api_key or api_key or access_token}'
23+
},
1524
'params': {
16-
'api_key': self.api_key,
1725
'email': email,
1826
'smtp': str(smtp).lower(),
1927
'accept_all': str(accept_all).lower(),
@@ -24,10 +32,12 @@ def verify(self, email, smtp=True, accept_all=False, timeout=None):
2432
url = self.base_url + 'verify'
2533
return self.__request('get', url, options)
2634

27-
def batch(self, emails, params={}):
35+
def batch(self, emails, params={}, api_key=None, access_token=None):
2836
options = {
37+
'headers': {
38+
'Authorization': f'Bearer {self.api_key or api_key or access_token}'
39+
},
2940
'params': {
30-
**{'api_key': self.api_key},
3141
**params
3242
},
3343
'json': {
@@ -37,10 +47,16 @@ def batch(self, emails, params={}):
3747
url = self.base_url + 'batch'
3848
return self.__request('post', url, options)
3949

40-
def batch_status(self, batch_id, simulate=None):
50+
def batch_status(self,
51+
batch_id,
52+
simulate=None,
53+
api_key=None,
54+
access_token=None):
4155
options = {
56+
'headers': {
57+
'Authorization': f'Bearer {self.api_key or api_key or access_token}'
58+
},
4259
'params': {
43-
'api_key': self.api_key,
4460
'id': batch_id,
4561
'simulate': simulate
4662
}
@@ -49,11 +65,11 @@ def batch_status(self, batch_id, simulate=None):
4965
url = self.base_url + 'batch'
5066
return self.__request('get', url, options)
5167

52-
def account(self):
68+
def account(self, api_key=None, access_token=None):
5369
options = {
54-
'params': {
55-
'api_key': self.api_key
56-
}
70+
'headers': {
71+
'Authorization': f'Bearer {self.api_key or api_key or access_token}'
72+
},
5773
}
5874

5975
url = self.base_url + 'account'

tests/test_authentication.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from unittest import TestCase
2+
import emailable
3+
import time
4+
5+
class TestAuthentication(TestCase):
6+
7+
def setUp(self):
8+
self.api_key = 'test_7aff7fc0142c65f86a00'
9+
self.email = '[email protected]'
10+
11+
12+
def test_invalid_api_key_authentication(self):
13+
client = emailable.Client('test_7aff7fc0141c65f86a00')
14+
self.assertRaises(
15+
emailable.AuthError,
16+
client.verify,
17+
18+
)
19+
20+
def test_missing_api_key_authentication(self):
21+
client = emailable.Client()
22+
self.assertRaises(
23+
emailable.AuthError,
24+
client.verify,
25+
26+
)
27+
28+
def test_global_api_key_authentication(self):
29+
client = emailable.Client(self.api_key)
30+
self.assertIsNotNone(client.verify(self.email).domain)
31+
batch_id = client.batch(self.emails).id
32+
self.assertIsNotNone(batch_id)
33+
self.assertIsNotNone(client.batch_status(batch_id).id)
34+
self.assertIsNotNone(client.account().available_credits)
35+
36+
def test_request_time_api_key_authentication(self):
37+
client = emailable.Client()
38+
self.assertIsNotNone(client.verify(self.email, api_key=self.api_key).domain)
39+
batch_id = client.batch(self.emails, api_key=self.api_key).id
40+
self.assertIsNotNone(batch_id)
41+
self.assertIsNotNone(client.batch_status(batch_id, api_key=self.api_key).id)
42+
self.assertIsNotNone(client.account(api_key=self.api_key).available_credits)

tests/test_client.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@ def setUp(self):
77
self.client = emailable.Client('test_7aff7fc0142c65f86a00')
88
time.sleep(0.5)
99

10-
def test_invalid_api_key(self):
11-
client = emailable.Client('test_7aff7fc0141c65f86a00')
12-
self.assertRaises(
13-
emailable.AuthError,
14-
client.verify,
15-
16-
)
17-
18-
def test_missing_api_key(self):
19-
self.client.api_key = None
20-
self.assertRaises(
21-
emailable.AuthError,
22-
self.client.verify,
23-
24-
)
25-
2610
def test_verify_returns_response(self):
2711
response = self.client.verify('[email protected]')
2812
self.assertIsInstance(response, emailable.Response)

0 commit comments

Comments
 (0)