Skip to content

Commit 0f36471

Browse files
committed
docs: Update examples with the usage of context manager
1 parent 7075193 commit 0f36471

1 file changed

Lines changed: 55 additions & 50 deletions

File tree

README.md

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -190,26 +190,23 @@ with Client(auth=(api_key, api_secret), version="v3.1") as mailjet:
190190
print(result.status_code)
191191
```
192192

193-
(Note:
194-
195-
> **Note**
196-
> If you choose not to use the context manager, you should manually call mailjet.close() when your application shuts down.
193+
> [!WARNING]
194+
> **Resource Management:** If you choose not to use the context manager, you **must** manually call `mailjet.close()` when your application shuts down to release the underlying sockets.
197195
198196
### Advanced Configuration
199197

200198
You can pass configuration overrides directly when initializing the `Client` or during individual API calls:
201199

202200
```python
203201
# Set custom base URL, timeout, and API version
204-
mailjet = Client(
202+
with Client(
205203
auth=(api_key, api_secret),
206204
version="v3.1",
207205
api_url="https://api.us.mailjet.com/",
208206
timeout=30,
209-
)
210-
211-
# Override timeout for a single, heavy request
212-
result = mailjet.contact.get(timeout=60)
207+
) as mailjet:
208+
# Override timeout for a single, heavy request
209+
result = mailjet.contact.get(timeout=60)
213210
```
214211

215212
#### API Versioning
@@ -224,7 +221,11 @@ Since most Email API endpoints are located under `v3`, it is set as the default
224221
For the others you need to specify the version using `version`. For example, if using Send API `v3.1`:
225222

226223
```python
227-
mailjet = Client(auth=(api_key, api_secret), version="v3.1")
224+
with Client(
225+
auth=(api_key, api_secret),
226+
version="v3.1",
227+
) as mailjet:
228+
pass # Your requests here
228229
```
229230

230231
For additional information refer to our [API Reference](https://dev.mailjet.com/reference/overview/versioning/).
@@ -234,7 +235,11 @@ For additional information refer to our [API Reference](https://dev.mailjet.com/
234235
The default base domain name for the Mailjet API is `api.mailjet.com`. You can modify this base URL by setting a value for `api_url` in your call:
235236

236237
```python
237-
mailjet = Client(auth=(api_key, api_secret), api_url="https://api.us.mailjet.com/")
238+
with Client(
239+
auth=(api_key, api_secret),
240+
api_url="https://api.us.mailjet.com/",
241+
) as mailjet:
242+
pass # Your requests here
238243
```
239244

240245
If your account has been moved to Mailjet's **US architecture**, the URL value you need to set is `https://api.us.mailjet.com`.
@@ -301,19 +306,19 @@ For example, to reach `statistics/link-click` path you should call `statistics_l
301306

302307
```python
303308
# GET `statistics/link-click`
304-
mailjet = Client(auth=(api_key, api_secret))
305-
filters = {"CampaignId": "xxxxxxx"}
306-
result = mailjet.statistics_linkClick.get(filters=filters)
307-
print(result.status_code)
308-
print(result.json())
309+
with Client(auth=(api_key, api_secret)) as mailjet:
310+
filters = {"CampaignId": "xxxxxxx"}
311+
result = mailjet.statistics_linkClick.get(filters=filters)
312+
print(result.status_code)
313+
print(result.json())
309314
```
310315

311316
For the **Content API (v1)**, sub-actions will be correctly routed using slashes (e.g. contents/lock). Additionally, the SDK maps the `data_images` resource specifically to `/v1/data/images` to support media uploads.
312317

313318
```python
314319
# GET '/v1/data/images'
315-
mailjet = Client(auth=(api_key, api_secret), version="v1")
316-
result = mailjet.data_images.get()
320+
with Client(auth=(api_key, api_secret), version="v1") as mailjet:
321+
result = mailjet.data_images.get()
317322
```
318323

319324
### Strict Payload Builders
@@ -370,7 +375,8 @@ from mailjet_rest import Client, Config
370375

371376
# Activate the PEP 578 Audit Listener
372377
cfg = Config(enable_security_audit=True)
373-
mailjet = Client(auth=(api_key, api_secret), config=cfg)
378+
with Client(auth=(api_key, api_secret), config=cfg) as mailjet:
379+
pass # Your secure requests here
374380
```
375381

376382
## Request examples
@@ -401,7 +407,6 @@ import os
401407

402408
api_key = os.environ.get("MJ_APIKEY_PUBLIC", "")
403409
api_secret = os.environ.get("MJ_APIKEY_PRIVATE", "")
404-
mailjet = Client(auth=(api_key, api_secret), version="v3.1")
405410

406411
data = {
407412
"Messages": [
@@ -414,18 +419,18 @@ data = {
414419
}
415420
]
416421
}
417-
result = mailjet.send.create(data=data)
418-
print(result.status_code)
419-
print(result.json())
422+
423+
with Client(auth=(api_key, api_secret), version="v3.1") as mailjet:
424+
result = mailjet.send.create(data=data)
425+
print(result.status_code)
426+
print(result.json())
420427
```
421428

422429
### Send an email using a Mailjet Template
423430

424431
When using `TemplateLanguage`, ensure that you pass a standard Python dictionary to the `Variables` parameter.
425432

426433
```python
427-
mailjet = Client(auth=(api_key, api_secret), version="v3.1")
428-
429434
data = {
430435
"Messages": [
431436
{
@@ -438,7 +443,8 @@ data = {
438443
}
439444
]
440445
}
441-
result = mailjet.send.create(data=data)
446+
with Client(auth=(api_key, api_secret), version="v3.1") as mailjet:
447+
result = mailjet.send.create(data=data)
442448
```
443449

444450
### Building Complex Payloads (MessageBuilder)
@@ -472,6 +478,9 @@ mailjet.send.create(data=payload)
472478

473479
### Standard REST Actions (GET, POST, PUT, DELETE)
474480

481+
> [!NOTE]\
482+
> All examples in this section assume that you have already opened a session using the context manager, for example: with Client(auth=(api_key, api_secret)) as mailjet:.
483+
475484
#### POST (Create)
476485

477486
##### Simple POST request
@@ -505,10 +514,9 @@ Enable `dry_run=True` to safely intercept all network mutations (`POST`, `PUT`,
505514

506515
```python
507516
# Intercepts state-changing requests and injects SandboxMode where applicable
508-
dry_run_client = Client(auth=(API_KEY, API_SECRET), dry_run=True)
509-
510-
# This will NOT hit the actual database, returning a mock 200 OK safely
511-
dry_run_client.contact.create(data={"Email": "real_user@example.com"})
517+
with Client(auth=(api_key, api_secret), dry_run=True) as dry_run_client:
518+
# This will NOT hit the actual database, returning a mock 200 OK safely
519+
dry_run_client.contact.create(data={"Email": "real_user@example.com"})
512520
```
513521

514522
#### GET Request
@@ -645,22 +653,20 @@ Retrieve performance counters using `statcounters` or location-based statistics
645653
from mailjet_rest import Client
646654
import os
647655

648-
mailjet = Client(
649-
auth=(
650-
os.environ.get("MJ_APIKEY_PUBLIC", ""),
651-
os.environ.get("MJ_APIKEY_PRIVATE", ""),
652-
)
656+
auth = (
657+
os.environ.get("MJ_APIKEY_PUBLIC", ""),
658+
os.environ.get("MJ_APIKEY_PRIVATE", ""),
653659
)
654-
655-
filters = {
656-
"CounterSource": "APIKey",
657-
"CounterTiming": "Message",
658-
"CounterResolution": "Lifetime",
659-
}
660-
# Getting general statistics
661-
result = mailjet.statcounters.get(filters=filters)
662-
print(result.status_code)
663-
print(result.json())
660+
with Client(auth=auth) as mailjet:
661+
filters = {
662+
"CounterSource": "APIKey",
663+
"CounterTiming": "Message",
664+
"CounterResolution": "Lifetime",
665+
}
666+
# Getting general statistics
667+
result = mailjet.statcounters.get(filters=filters)
668+
print(result.status_code)
669+
print(result.json())
664670
```
665671

666672
### Content API
@@ -673,11 +679,10 @@ The Content API (`v1`) allows managing templates, generating API tokens, and upl
673679

674680
```python
675681
# Tokens endpoint requires Basic Auth initially
676-
client = Client(auth=(api_key, api_secret), version="v1")
677-
data = {"Name": "My Access Token", "Permissions": ["read_template", "create_template"]}
678-
679-
result = client.token.create(data=data)
680-
print(result.json())
682+
with Client(auth=(api_key, api_secret), version="v1") as client:
683+
data = {"Name": "My Access Token", "Permissions": ["read_template", "create_template"]}
684+
result = client.token.create(data=data)
685+
print(result.json())
681686
```
682687

683688
#### Uploading an Image via Multipart Form-Data

0 commit comments

Comments
 (0)