SendGrid using an httpx client
Project description
Async SendGrid
A modern, asynchronous SendGrid client built on top of httpx. This library provides a simple and efficient way to send emails using SendGrid's API with Python's async/await syntax.
Features
- 🚀 Asynchronous API client for SendGrid
- 🔄 Connection pooling for better performance
- 📊 OpenTelemetry integration for monitoring
- 🔍 Detailed error tracking and tracing
- 🛠️ Customizable configuration
- 📝 Comprehensive documentation
Installation
Install the package using pip:
pip install sendgrid-async
Quick Start
from async_sendgrid import SendgridAPI
from sendgrid.helpers.mail import Mail
# Initialize the client
sendgrid = SendgridAPI(api_key="YOUR_API_KEY")
# Create and send an email
email = Mail(
from_email="from@example.com",
to_emails="to@example.com",
subject="Hello World",
plain_text_content="Hello World!",
)
response = await sendgrid.send(email)
Advanced Features
Connection Pooling
Optimize performance with connection pooling:
from async_sendgrid import SendgridAPI
from async_sendgrid.pool import ConnectionPool
pool = ConnectionPool(
max_connections=20,
max_keepalive_connections=10,
keepalive_expiry=10.0,
)
sendgrid = SendgridAPI(
api_key="YOUR_API_KEY",
pool=pool,
)
Send emails on behalf of another user
Send emails on behalf of subusers:
sendgrid = SendgridAPI(
api_key="YOUR_API_KEY",
on_behalf_of="John Smith",
)
Custom Endpoints
Use custom API endpoints:
sendgrid = SendgridAPI(
api_key="YOUR_API_KEY",
endpoint="https://custom.endpoint.com/v3/mail/send",
)
Telemetry Integration
Monitor and trace your SendGrid operations with OpenTelemetry:
Setup
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
# Configure OpenTelemetry
tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
# Add your exporter
otlp_exporter = OTLPSpanExporter()
span_processor = BatchSpanProcessor(otlp_exporter)
tracer_provider.add_span_processor(span_processor)
Available Metrics
The library automatically tracks:
HTTP Metrics
- Status codes
- Response sizes
- URLs
- Methods
SendGrid Metrics
- Number of recipients
- Attachment presence
- Email content type
Configuration
Control telemetry behavior with environment variables:
# Disable telemetry
SENDGRID_TELEMETRY_IS_ENABLED=false
# Custom span name
SENDGRID_TELEMETRY_SPAN_NAME=custom.span.name
Error Handling
Robust error handling for API operations:
try:
response = await sendgrid.send(email)
except Exception as e:
# Handle the error
print(f"Error sending email: {e}")
Development
Setup
# Clone the repository
git clone https://github.com/yourusername/async-sendgrid.git
cd async-sendgrid
# Install development dependencies
pip install -e ".[test]"
Testing
# Run all tests
pytest
# Run with coverage
pytest --cov=async_sendgrid
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sendgrid_async-2.1.1.tar.gz.
File metadata
- Download URL: sendgrid_async-2.1.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05406383c74de3a2799ffc95ade4123b3046dd6e25ae1e8428aa7bcce2f67c09
|
|
| MD5 |
2f4c1c89b188eee3d69552132331d28a
|
|
| BLAKE2b-256 |
f662caf073c735e0c59d29f41e6dad33c003b2c2a01757265ffe9b27a5d7c3c3
|
Provenance
The following attestation bundles were made for sendgrid_async-2.1.1.tar.gz:
Publisher:
release.yml on EM51641/async-sendgrid-
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sendgrid_async-2.1.1.tar.gz -
Subject digest:
05406383c74de3a2799ffc95ade4123b3046dd6e25ae1e8428aa7bcce2f67c09 - Sigstore transparency entry: 238520342
- Sigstore integration time:
-
Permalink:
EM51641/async-sendgrid-@7aeab62f1ec8e3586e08edf64629496eec25dbcb -
Branch / Tag:
refs/tags/v2.1.1 - Owner: https://github.com/EM51641
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7aeab62f1ec8e3586e08edf64629496eec25dbcb -
Trigger Event:
release
-
Statement type:
File details
Details for the file sendgrid_async-2.1.1-py3-none-any.whl.
File metadata
- Download URL: sendgrid_async-2.1.1-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
deb802405d5536acaf712d25612346f3f55b87b9798d762ee12631edfadb795c
|
|
| MD5 |
b0e4a9482b3269e274bfc687a6b1e535
|
|
| BLAKE2b-256 |
7c522ee60d5f13b8fe1785e5478024024f463b4dddab2109a4208cda38890eba
|
Provenance
The following attestation bundles were made for sendgrid_async-2.1.1-py3-none-any.whl:
Publisher:
release.yml on EM51641/async-sendgrid-
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sendgrid_async-2.1.1-py3-none-any.whl -
Subject digest:
deb802405d5536acaf712d25612346f3f55b87b9798d762ee12631edfadb795c - Sigstore transparency entry: 238520345
- Sigstore integration time:
-
Permalink:
EM51641/async-sendgrid-@7aeab62f1ec8e3586e08edf64629496eec25dbcb -
Branch / Tag:
refs/tags/v2.1.1 - Owner: https://github.com/EM51641
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7aeab62f1ec8e3586e08edf64629496eec25dbcb -
Trigger Event:
release
-
Statement type: