Skip to content

Commit cfb2d3f

Browse files
committed
Use API key environment variable by default
1 parent 2e5dceb commit cfb2d3f

9 files changed

Lines changed: 48 additions & 39 deletions

File tree

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,30 @@ Mailosaur lets you automate email and SMS tests as part of software development
1010

1111
This guide provides several key sections:
1212

13+
- [Mailosaur - Python library · ](#mailosaur---python-library--)
1314
- [Get Started](#get-started)
15+
- [Installation](#installation)
16+
- [Set your API key](#set-your-api-key)
17+
- [Create your code](#create-your-code)
18+
- [API Reference](#api-reference)
1419
- [Creating an account](#creating-an-account)
1520
- [Test email addresses with Mailosaur](#test-email-addresses-with-mailosaur)
1621
- [Find an email](#find-an-email)
22+
- [What is this code doing?](#what-is-this-code-doing)
1723
- [Find an SMS message](#find-an-sms-message)
1824
- [Testing plain text content](#testing-plain-text-content)
25+
- [Extracting verification codes from plain text](#extracting-verification-codes-from-plain-text)
1926
- [Testing HTML content](#testing-html-content)
27+
- [Working with HTML using Beautiful Soup](#working-with-html-using-beautiful-soup)
2028
- [Working with hyperlinks](#working-with-hyperlinks)
29+
- [Links in plain text (including SMS messages)](#links-in-plain-text-including-sms-messages)
2130
- [Working with attachments](#working-with-attachments)
2231
- [Working with images and web beacons](#working-with-images-and-web-beacons)
32+
- [Remotely-hosted images](#remotely-hosted-images)
33+
- [Triggering web beacons](#triggering-web-beacons)
2334
- [Spam checking](#spam-checking)
35+
- [Development](#development)
36+
- [Contacting us](#contacting-us)
2437

2538
You can find the full [Mailosaur documentation](https://mailosaur.com/docs/) on the website.
2639

@@ -32,11 +45,21 @@ If you get stuck, just contact us at support@mailosaur.com.
3245
pip install --upgrade mailosaur
3346
```
3447

35-
Then import the library into your code. The value for `YOUR_API_KEY` is covered in the next step ([creating an account](#creating-an-account)):
48+
### Set your API key
49+
50+
Get your API key from the Mailosaur Dashboard and set it as an environment variable:
51+
52+
```sh
53+
export MAILOSAUR_API_KEY='your-api-key-here'
54+
```
55+
56+
### Create your code
57+
58+
Then import the library and create a client:
3659

3760
```py
3861
from mailosaur import MailosaurClient
39-
mailosaur = MailosaurClient("YOUR_API_KEY")
62+
mailosaur = MailosaurClient()
4063
```
4164

4265
### API Reference
@@ -80,7 +103,7 @@ In automated tests you will want to wait for a new email to arrive. This library
80103
from mailosaur import MailosaurClient
81104
from mailosaur.models import SearchCriteria
82105

83-
mailosaur = MailosaurClient("API_KEY")
106+
mailosaur = MailosaurClient()
84107

85108
# See https://mailosaur.com/app/project/api
86109
server_id = "abc123"
@@ -96,7 +119,7 @@ print(email.subject) # "Hello world!"
96119

97120
### What is this code doing?
98121

99-
1. Sets up an instance of `MailosaurClient` with your API key.
122+
1. Sets up an instance of `MailosaurClient` using the `MAILOSAUR_API_KEY` environment variable.
100123
2. Waits for an email to arrive at the server with ID `abc123`.
101124
3. Outputs the subject line of the email.
102125

@@ -110,7 +133,7 @@ If your account has [SMS testing](https://mailosaur.com/sms-testing/) enabled, y
110133
from mailosaur import MailosaurClient
111134
from mailosaur.models import SearchCriteria
112135

113-
mailosaur = MailosaurClient("API_KEY")
136+
mailosaur = MailosaurClient()
114137

115138
# See https://mailosaur.com/app/project/api
116139
server_id = "abc123"

mailosaur/mailosaur_client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
More options at https://mailosaur.com/docs/email/
99
"""
1010

11+
import os
1112
import uuid
1213
import requests
1314

@@ -24,8 +25,15 @@
2425
class MailosaurClient(object):
2526
""" Main class to access Mailosaur.com api. """
2627

27-
def __init__(self, api_key, base_url="https://mailosaur.com/"):
28+
def __init__(self, api_key=None, base_url="https://mailosaur.com/"):
2829
""" Pass in your mailbox id and api key to authenticate """
30+
api_key = api_key or os.environ.get('MAILOSAUR_API_KEY')
31+
32+
if not api_key:
33+
raise ValueError(
34+
"'api_key' must be set via the MAILOSAUR_API_KEY environment "
35+
"variable, or passed to the MailosaurClient constructor.")
36+
2937
session = requests.Session()
3038
session.auth = (api_key, '')
3139
session.headers.update({'User-Agent': 'mailosaur-python/8.0.0'})

tests/devices_test.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88
class DevicesTest(TestCase):
99
@classmethod
1010
def setUpClass(self):
11-
api_key = os.getenv('MAILOSAUR_API_KEY')
1211
base_url = os.getenv('MAILOSAUR_BASE_URL')
1312

14-
if api_key is None:
15-
raise Exception(
16-
"Missing necessary environment variables - refer to README.md")
17-
18-
self.client = MailosaurClient(api_key, base_url)
13+
self.client = MailosaurClient(base_url=base_url)
1914

2015
def test_crud(self):
2116
device_name = "My test"

tests/emails_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
class EmailsTest(TestCase):
1212
@classmethod
1313
def setUpClass(cls):
14-
api_key = os.getenv('MAILOSAUR_API_KEY')
1514
base_url = os.getenv('MAILOSAUR_BASE_URL')
1615
cls.server = os.getenv('MAILOSAUR_SERVER')
1716
cls.verified_domain = os.getenv('MAILOSAUR_VERIFIED_DOMAIN')
1817

19-
if (api_key or cls.server) is None:
18+
if cls.server is None:
2019
raise Exception(
2120
"Missing necessary environment variables - refer to README.md")
2221

23-
cls.client = MailosaurClient(api_key, base_url)
22+
cls.client = MailosaurClient(base_url=base_url)
2423

2524
cls.client.messages.delete_all(cls.server)
2625

tests/errors_test.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77
class ErrorsTest(TestCase):
88
@classmethod
99
def setUpClass(self):
10-
self.api_key = os.getenv('MAILOSAUR_API_KEY')
1110
self.base_url = os.getenv('MAILOSAUR_BASE_URL')
1211

13-
if self.api_key is None:
14-
raise Exception(
15-
"Missing necessary environment variables - refer to README.md")
16-
1712
def test_unauthorized(self):
1813
client = MailosaurClient('invalid_key', self.base_url)
1914

@@ -24,7 +19,7 @@ def test_unauthorized(self):
2419
'Authentication failed, check your API key.', context.exception.message)
2520

2621
def test_not_found(self):
27-
client = MailosaurClient(self.api_key, self.base_url)
22+
client = MailosaurClient(base_url=self.base_url)
2823

2924
with self.assertRaises(MailosaurException) as context:
3025
client.servers.get('not_found')
@@ -33,7 +28,7 @@ def test_not_found(self):
3328
'Not found, check input parameters.', context.exception.message)
3429

3530
def test_bad_request(self):
36-
client = MailosaurClient(self.api_key, self.base_url)
31+
client = MailosaurClient(base_url=self.base_url)
3732

3833
with self.assertRaises(MailosaurException) as context:
3934
options = ServerCreateOptions()

tests/files_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
class EmailsTest(TestCase):
88
@classmethod
99
def setUpClass(cls):
10-
api_key = os.getenv('MAILOSAUR_API_KEY')
1110
base_url = os.getenv('MAILOSAUR_BASE_URL')
1211
cls.server = os.getenv('MAILOSAUR_SERVER')
1312

14-
if (api_key or cls.server) is None:
13+
if cls.server is None:
1514
raise Exception("Missing necessary environment variables - refer to README.md")
1615

17-
cls.client = MailosaurClient(api_key, base_url)
16+
cls.client = MailosaurClient(base_url=base_url)
1817

1918
cls.client.messages.delete_all(cls.server)
2019

tests/previews_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
class PreviewsTest(TestCase):
1212
@classmethod
1313
def setUpClass(cls):
14-
api_key = os.getenv('MAILOSAUR_API_KEY')
1514
base_url = os.getenv('MAILOSAUR_BASE_URL')
1615
cls.server = os.getenv('MAILOSAUR_SERVER')
1716

18-
if api_key is None or cls.server is None:
17+
if cls.server is None:
1918
raise Exception(
2019
"Missing necessary environment variables - refer to README.md")
2120

22-
cls.client = MailosaurClient(api_key, base_url)
21+
cls.client = MailosaurClient(base_url=base_url)
2322

2423
def test_list_clients(self):
2524
result = self.client.previews.list_email_clients()

tests/servers_test.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88
class ServersTest(TestCase):
99
@classmethod
1010
def setUpClass(self):
11-
api_key = os.getenv('MAILOSAUR_API_KEY')
1211
base_url = os.getenv('MAILOSAUR_BASE_URL')
1312

14-
if api_key is None:
15-
raise Exception(
16-
"Missing necessary environment variables - refer to README.md")
17-
18-
self.client = MailosaurClient(api_key, base_url)
13+
self.client = MailosaurClient(base_url=base_url)
1914

2015
def test_list(self):
2116
result = self.client.servers.list()

tests/usage_test.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
class UsageTest(TestCase):
66
@classmethod
77
def setUpClass(self):
8-
api_key = os.getenv('MAILOSAUR_API_KEY')
98
base_url = os.getenv('MAILOSAUR_BASE_URL')
109

11-
if api_key is None:
12-
raise Exception("Missing necessary environment variables - refer to README.md")
13-
14-
self.client = MailosaurClient(api_key, base_url)
10+
self.client = MailosaurClient(base_url=base_url)
1511

1612
def test_account_limits(self):
1713
result = self.client.usage.limits()

0 commit comments

Comments
 (0)