sendgrid-async 2.0.2__tar.gz → 2.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,129 @@
1
+ Metadata-Version: 2.1
2
+ Name: sendgrid-async
3
+ Version: 2.0.3
4
+ Summary: SendGrid using a client based on httpx.
5
+ Home-page: https://github.com/EM51641/async-sendgrid/
6
+ License: MIT
7
+ Keywords: sendgrid,async,httpx
8
+ Author: Elyes Mahjoubo
9
+ Author-email: elyesmahjoubi@gmail.com
10
+ Requires-Python: >=3.10,<4.0
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Requires-Dist: certifi (>=2024.6.2,<2025.0.0)
21
+ Requires-Dist: httpx (>=0.24.1,<0.25.0)
22
+ Requires-Dist: sendgrid (>=6.7.0,<7.0.0)
23
+ Requires-Dist: toml (>=0.10.2,<0.11.0)
24
+ Project-URL: Repository, https://github.com/EM51641/async-sendgrid/
25
+ Description-Content-Type: text/markdown
26
+
27
+ # Async-Sendgrid
28
+
29
+ [![Python](https://img.shields.io/pypi/pyversions/sendgrid-async)](https://pypi.org/project/sendgrid-async/)
30
+ [![PyPI Latest Release](https://img.shields.io/pypi/v/sendgrid-async.svg)](https://pypi.org/project/sendgrid-async/)
31
+ [![PyPI Downloads](https://img.shields.io/pypi/dm/sendgrid-async.svg?label=PyPI%20downloads)](https://pypi.org/project/sendgrid-async/)
32
+ [![License - MIT](https://img.shields.io/pypi/l/async_sendgrid.svg)](https://github.com/sensodevices/async_sendgrid/blob/main/LICENSE)
33
+
34
+ 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.
35
+
36
+ ## Features
37
+
38
+ - 🚀 Asynchronous email sending using `httpx`
39
+ - 🔒 Type-safe with comprehensive type hints
40
+ - ⚡️ Efficient connection pooling and session management
41
+ - 🛠️ Compatible with SendGrid's official Python library
42
+ - 📦 Easy installation and simple API
43
+
44
+ ## Requirements
45
+
46
+ - Python 3.10 or higher
47
+ - SendGrid API key
48
+
49
+ ## Installation
50
+
51
+ Install the package using pip:
52
+
53
+ ```bash
54
+ pip install sendgrid-async
55
+ ```
56
+
57
+ Or using Poetry:
58
+
59
+ ```bash
60
+ poetry add sendgrid-async
61
+ ```
62
+
63
+ ## Quick Start
64
+
65
+ Here's a simple example of how to send an email:
66
+
67
+ ```python
68
+ import os
69
+ from async_sendgrid import SendgridAPI
70
+ from sendgrid.helpers.mail import Content, Email, Mail, To
71
+
72
+ # Initialize the client
73
+ api_key = os.environ.get('SENDGRID_API_KEY')
74
+ sendgrid = SendgridAPI(api_key)
75
+
76
+ # Create email content
77
+ from_email = Email("sender@example.com")
78
+ to_email = To("recipient@example.com")
79
+ subject = "Hello from Async-Sendgrid!"
80
+ content = Content("text/plain", "This is a test email sent using Async-Sendgrid.")
81
+
82
+ # Create mail object
83
+ mail = Mail(from_email, to_email, subject, content)
84
+
85
+ # Send the email
86
+ async with sendgrid as client:
87
+ response = await client.send(mail)
88
+ ```
89
+
90
+ ## Advanced Usage
91
+
92
+ ### Custom Endpoint
93
+
94
+ For testing or development purposes, you can specify a custom endpoint:
95
+
96
+ ```python
97
+ sendgrid = SendgridAPI(
98
+ api_key="YOUR_API_KEY",
99
+ endpoint="https://localhost:3000/v3/mail/send"
100
+ )
101
+ ```
102
+
103
+ ### Error Handling
104
+
105
+ The library provides proper error handling for API responses:
106
+
107
+ ```python
108
+ from async_sendgrid.exceptions import SendgridError
109
+
110
+ try:
111
+ async with sendgrid as client:
112
+ response = await client.send(mail)
113
+ except SendgridError as e:
114
+ print(f"Failed to send email: {e}")
115
+ ```
116
+
117
+ ## Contributing
118
+
119
+ Contributions are welcome! Please feel free to submit a Pull Request.
120
+
121
+ 1. Fork the repository
122
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
123
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
124
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
125
+ 5. Open a Pull Request
126
+
127
+ ## License
128
+
129
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,103 @@
1
+ # Async-Sendgrid
2
+
3
+ [![Python](https://img.shields.io/pypi/pyversions/sendgrid-async)](https://pypi.org/project/sendgrid-async/)
4
+ [![PyPI Latest Release](https://img.shields.io/pypi/v/sendgrid-async.svg)](https://pypi.org/project/sendgrid-async/)
5
+ [![PyPI Downloads](https://img.shields.io/pypi/dm/sendgrid-async.svg?label=PyPI%20downloads)](https://pypi.org/project/sendgrid-async/)
6
+ [![License - MIT](https://img.shields.io/pypi/l/async_sendgrid.svg)](https://github.com/sensodevices/async_sendgrid/blob/main/LICENSE)
7
+
8
+ 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.
9
+
10
+ ## Features
11
+
12
+ - 🚀 Asynchronous email sending using `httpx`
13
+ - 🔒 Type-safe with comprehensive type hints
14
+ - ⚡️ Efficient connection pooling and session management
15
+ - 🛠️ Compatible with SendGrid's official Python library
16
+ - 📦 Easy installation and simple API
17
+
18
+ ## Requirements
19
+
20
+ - Python 3.10 or higher
21
+ - SendGrid API key
22
+
23
+ ## Installation
24
+
25
+ Install the package using pip:
26
+
27
+ ```bash
28
+ pip install sendgrid-async
29
+ ```
30
+
31
+ Or using Poetry:
32
+
33
+ ```bash
34
+ poetry add sendgrid-async
35
+ ```
36
+
37
+ ## Quick Start
38
+
39
+ Here's a simple example of how to send an email:
40
+
41
+ ```python
42
+ import os
43
+ from async_sendgrid import SendgridAPI
44
+ from sendgrid.helpers.mail import Content, Email, Mail, To
45
+
46
+ # Initialize the client
47
+ api_key = os.environ.get('SENDGRID_API_KEY')
48
+ sendgrid = SendgridAPI(api_key)
49
+
50
+ # Create email content
51
+ from_email = Email("sender@example.com")
52
+ to_email = To("recipient@example.com")
53
+ subject = "Hello from Async-Sendgrid!"
54
+ content = Content("text/plain", "This is a test email sent using Async-Sendgrid.")
55
+
56
+ # Create mail object
57
+ mail = Mail(from_email, to_email, subject, content)
58
+
59
+ # Send the email
60
+ async with sendgrid as client:
61
+ response = await client.send(mail)
62
+ ```
63
+
64
+ ## Advanced Usage
65
+
66
+ ### Custom Endpoint
67
+
68
+ For testing or development purposes, you can specify a custom endpoint:
69
+
70
+ ```python
71
+ sendgrid = SendgridAPI(
72
+ api_key="YOUR_API_KEY",
73
+ endpoint="https://localhost:3000/v3/mail/send"
74
+ )
75
+ ```
76
+
77
+ ### Error Handling
78
+
79
+ The library provides proper error handling for API responses:
80
+
81
+ ```python
82
+ from async_sendgrid.exceptions import SendgridError
83
+
84
+ try:
85
+ async with sendgrid as client:
86
+ response = await client.send(mail)
87
+ except SendgridError as e:
88
+ print(f"Failed to send email: {e}")
89
+ ```
90
+
91
+ ## Contributing
92
+
93
+ Contributions are welcome! Please feel free to submit a Pull Request.
94
+
95
+ 1. Fork the repository
96
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
97
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
98
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
99
+ 5. Open a Pull Request
100
+
101
+ ## License
102
+
103
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,8 @@
1
+ class SessionClosedException(Exception):
2
+ """
3
+ Exception raised when the session is closed unexpectedly.
4
+ """
5
+
6
+ def __init__(self, message: str):
7
+ self.message = message
8
+ super().__init__(self.message)
@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING
5
5
 
6
6
  from httpx import AsyncClient # type: ignore
7
7
 
8
+ from async_sendgrid.exception import SessionClosedException
8
9
  from async_sendgrid.utils import create_session
9
10
 
10
11
  if TYPE_CHECKING:
@@ -30,10 +31,23 @@ class BaseSendgridAPI(ABC):
30
31
  def headers(self) -> dict[Any, Any]:
31
32
  """Not implemented"""
32
33
 
34
+ @property
35
+ @abstractmethod
36
+ def session(self) -> AsyncClient | None:
37
+ """Not implemented"""
38
+
33
39
  @abstractmethod
34
40
  async def send(self, message: Mail) -> Response:
35
41
  """Not implemented"""
36
42
 
43
+ @abstractmethod
44
+ async def __aenter__(self) -> BaseSendgridAPI:
45
+ """Not implemented"""
46
+
47
+ @abstractmethod
48
+ async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None:
49
+ """Not implemented"""
50
+
37
51
 
38
52
  class SendgridAPI(BaseSendgridAPI):
39
53
  """
@@ -84,6 +98,10 @@ class SendgridAPI(BaseSendgridAPI):
84
98
  def headers(self) -> dict[Any, Any]:
85
99
  return self._headers
86
100
 
101
+ @property
102
+ def session(self) -> AsyncClient | None:
103
+ return self._session
104
+
87
105
  async def send(self, message: Mail) -> Response:
88
106
  """
89
107
  Make a Twilio SendGrid v3 API request with the request body generated
@@ -99,18 +117,27 @@ class SendgridAPI(BaseSendgridAPI):
99
117
  """
100
118
  assert self._session
101
119
 
120
+ if self._session.is_closed:
121
+ raise SessionClosedException(
122
+ "Session was closed, establishing new connection"
123
+ )
124
+
102
125
  json_message = message.get()
103
126
  response = await self._session.post(
104
127
  url=self._endpoint, json=json_message
105
128
  )
106
129
  return response
107
130
 
108
- async def __aenter__(self):
109
- if not self._session or self._session.is_closed:
110
- self._session = create_session(headers=self._headers)
131
+ async def __aenter__(self) -> SendgridAPI:
132
+ self._session = create_session(headers=self._headers)
111
133
  return self
112
134
 
113
- async def __aexit__(self, exc_type: Any, exc: Any, tb: Any):
135
+ async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None:
114
136
  assert self._session
115
-
116
137
  await self._session.aclose()
138
+
139
+ def __str__(self) -> str:
140
+ return f"SendGrid API Client\n • Endpoint: {self._endpoint}\n"
141
+
142
+ def __repr__(self) -> str:
143
+ return f"SendgridAPI(endpoint={self._endpoint})"
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "sendgrid-async"
3
- version = "2.0.2"
3
+ version = "2.0.3"
4
4
  description = "SendGrid using a client based on httpx."
5
5
  license = "MIT"
6
6
  authors = ["Elyes Mahjoubo <elyesmahjoubi@gmail.com>"]
@@ -13,12 +13,18 @@ classifiers = [
13
13
  "Programming Language :: Python :: 3.10",
14
14
  "Programming Language :: Python :: 3.11",
15
15
  "Programming Language :: Python :: 3.12",
16
+ "Programming Language :: Python :: 3.13",
16
17
  ]
17
18
 
18
19
  packages = [
19
20
  { include = "async_sendgrid" },
20
21
  ]
21
22
 
23
+ include = [
24
+ "LICENSE",
25
+ "README.md"
26
+ ]
27
+
22
28
  [tool.poetry.dependencies]
23
29
  python = "^3.10"
24
30
  sendgrid = "^6.7.0"
@@ -1,85 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: sendgrid-async
3
- Version: 2.0.2
4
- Summary: SendGrid using a client based on httpx.
5
- Home-page: https://github.com/EM51641/async-sendgrid/
6
- License: MIT
7
- Keywords: sendgrid,async,httpx
8
- Author: Elyes Mahjoubo
9
- Author-email: elyesmahjoubi@gmail.com
10
- Requires-Python: >=3.10,<4.0
11
- Classifier: License :: OSI Approved :: MIT License
12
- Classifier: Operating System :: OS Independent
13
- Classifier: Programming Language :: Python :: 3
14
- Classifier: Programming Language :: Python :: 3.10
15
- Classifier: Programming Language :: Python :: 3.11
16
- Classifier: Programming Language :: Python :: 3.12
17
- Requires-Dist: certifi (>=2024.6.2,<2025.0.0)
18
- Requires-Dist: httpx (>=0.24.1,<0.25.0)
19
- Requires-Dist: sendgrid (>=6.7.0,<7.0.0)
20
- Requires-Dist: toml (>=0.10.2,<0.11.0)
21
- Project-URL: Repository, https://github.com/EM51641/async-sendgrid/
22
- Description-Content-Type: text/markdown
23
-
24
- | | |
25
- | --- | --- |
26
- | Python| ![Python](https://img.shields.io/pypi/pyversions/sendgrid-async) |
27
- | Package | [![PyPI Latest Release](https://img.shields.io/pypi/v/sendgrid-async.svg)](https://pypi.org/project/sendgrid-async/) [![PyPI Downloads](https://img.shields.io/pypi/dm/sendgrid-async.svg?label=PyPI%20downloads)](https://pypi.org/project/sendgrid-async/) |
28
- | Meta | [![License - MIT](https://img.shields.io/pypi/l/async_sendgrid.svg)](https://github.com/sensodevices/async_sendgrid/blob/main/LICENSE)|
29
-
30
- # Async-Sendgrid
31
-
32
- Sendgrid-Async is a simple asynchronous client built upon the httpx library.
33
-
34
- # Installation
35
-
36
- You can install Sendgrid-Async using pip with the following command:
37
-
38
- ```shell
39
- pip install sendgrid-async
40
- ```
41
-
42
- # Usage
43
-
44
- Here is a brief script demonstrating how to send an email using Async-Sendgrid:
45
-
46
- First, import the `SendgridAPI` from the `sendgrid-async` package. Then, create a SendgridAPI object using your API key.
47
-
48
- ```python
49
- from async_sendgrid import SendgridAPI
50
- import os
51
-
52
- API_KEY = os.environ.get('SECRET_API_KEY')
53
- sendgrid = SendgridAPI(API_KEY)
54
- ```
55
-
56
- Next, we can create an email using the original `sendgrid` library as follows:
57
-
58
- ```python
59
- from sendgrid.helpers.mail import Content, Email, Mail, To
60
-
61
- from_email = Email("test@example.com")
62
- to_email = To("test@example.com")
63
- subject = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
64
- content = Content("text/plain", "Sed varius ligula ac urna vehicula ultrices. Nunc ut dolor sem.")
65
-
66
- mail = Mail(
67
- from_email=from_email,
68
- to_email=to_email,
69
- subject=subject,
70
- content=content
71
- )
72
- ```
73
-
74
- An email can be sent to sendgrid servers with the `send` API of the `SendgridAPI` instance:
75
-
76
- ```python
77
- async with sendgrid as client:
78
- response = await client.send(data)
79
- ```
80
-
81
- For testing purposes, you can modify the API endpoint as follows:
82
-
83
- ```python
84
- sendgrid = SendgridAPI(api_key="SECRET_API_KEY", endpoint="https://localhost:3000/v3/mail/send")
85
- ```
@@ -1,62 +0,0 @@
1
- | | |
2
- | --- | --- |
3
- | Python| ![Python](https://img.shields.io/pypi/pyversions/sendgrid-async) |
4
- | Package | [![PyPI Latest Release](https://img.shields.io/pypi/v/sendgrid-async.svg)](https://pypi.org/project/sendgrid-async/) [![PyPI Downloads](https://img.shields.io/pypi/dm/sendgrid-async.svg?label=PyPI%20downloads)](https://pypi.org/project/sendgrid-async/) |
5
- | Meta | [![License - MIT](https://img.shields.io/pypi/l/async_sendgrid.svg)](https://github.com/sensodevices/async_sendgrid/blob/main/LICENSE)|
6
-
7
- # Async-Sendgrid
8
-
9
- Sendgrid-Async is a simple asynchronous client built upon the httpx library.
10
-
11
- # Installation
12
-
13
- You can install Sendgrid-Async using pip with the following command:
14
-
15
- ```shell
16
- pip install sendgrid-async
17
- ```
18
-
19
- # Usage
20
-
21
- Here is a brief script demonstrating how to send an email using Async-Sendgrid:
22
-
23
- First, import the `SendgridAPI` from the `sendgrid-async` package. Then, create a SendgridAPI object using your API key.
24
-
25
- ```python
26
- from async_sendgrid import SendgridAPI
27
- import os
28
-
29
- API_KEY = os.environ.get('SECRET_API_KEY')
30
- sendgrid = SendgridAPI(API_KEY)
31
- ```
32
-
33
- Next, we can create an email using the original `sendgrid` library as follows:
34
-
35
- ```python
36
- from sendgrid.helpers.mail import Content, Email, Mail, To
37
-
38
- from_email = Email("test@example.com")
39
- to_email = To("test@example.com")
40
- subject = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
41
- content = Content("text/plain", "Sed varius ligula ac urna vehicula ultrices. Nunc ut dolor sem.")
42
-
43
- mail = Mail(
44
- from_email=from_email,
45
- to_email=to_email,
46
- subject=subject,
47
- content=content
48
- )
49
- ```
50
-
51
- An email can be sent to sendgrid servers with the `send` API of the `SendgridAPI` instance:
52
-
53
- ```python
54
- async with sendgrid as client:
55
- response = await client.send(data)
56
- ```
57
-
58
- For testing purposes, you can modify the API endpoint as follows:
59
-
60
- ```python
61
- sendgrid = SendgridAPI(api_key="SECRET_API_KEY", endpoint="https://localhost:3000/v3/mail/send")
62
- ```
File without changes