sendgrid-async 1.0.0__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,7 @@
1
+ Copyright (c) 2023, Elyes Mahjoubi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ Metadata-Version: 2.1
2
+ Name: sendgrid-async
3
+ Version: 1.0.0
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: httpx (>=0.24.1,<0.25.0)
18
+ Requires-Dist: sendgrid (>=6.7.0,<7.0.0)
19
+ Project-URL: Repository, https://github.com/EM51641/async-sendgrid/
20
+ Description-Content-Type: text/markdown
21
+
22
+ | | |
23
+ | --- | --- |
24
+ | Python| ![Python](https://img.shields.io/pypi/pyversions/async-sendgrid) |
25
+ | Package | [![PyPI Latest Release](https://img.shields.io/pypi/v/pandas.svg)](https://pypi.org/project/async-sendgrid/) [![PyPI Downloads](https://img.shields.io/pypi/dm/async-sendgrid.svg?label=PyPI%20downloads)](https://pypi.org/project/async-sendgrid/) |
26
+ | Meta | [![License - MIT](https://img.shields.io/pypi/l/async_sendgrid.svg)](https://github.com/sensodevices/async_sendgrid/blob/main/LICENSE)|
27
+
28
+ # Async-Sendgrid
29
+
30
+ Sendgrid simple asynchronous client based on the httpx libarary.
31
+
32
+ # Installation
33
+
34
+ It is possible to install async-sendgrid with pip:
35
+
36
+ ```bash
37
+ pip install async-sendgrid
38
+ ```
39
+
40
+ # Usage
41
+ This is a small script showing how to send an email with async-sendgrid:
42
+
43
+ First, you need to import the ```SendgridAPI``` from the ```async_sendgrid``` package. Then, you need to create a ```SendgridAPI``` object with your API key.
44
+
45
+ ```python
46
+ from async_sendgrid import SendgridAPI
47
+ import os
48
+
49
+ API_KEY = os.environ.get['API_KEY']
50
+ sendgrid = SendgridAPI(API_KEY)
51
+ ```
52
+
53
+ Thereafter, you can create an email with the original ```sendgrid``` package such:
54
+
55
+ ```python
56
+ from sendgrid.helpers.mail import Content, Email, Mail, To
57
+
58
+ from_email = Email("test@example.com")
59
+ to_email = To("test@example.com")
60
+ subject = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
61
+ content = Content("text/plain", "Sed varius ligula ac urna vehicula ultrices. Nunc ut dolor sem.")
62
+
63
+ mail = Mail(
64
+ from_email=from_email,
65
+ to_email=to_email,
66
+ subject=subject,
67
+ content=content
68
+ )
69
+
70
+ ```
71
+
72
+ Finally you can send the email with the ```send``` method of the ```SendgridAPI``` instance:
73
+
74
+ ```python
75
+ async with sendgrid as client:
76
+ response = await client.send(data)
77
+ ```
78
+
@@ -0,0 +1,56 @@
1
+ | | |
2
+ | --- | --- |
3
+ | Python| ![Python](https://img.shields.io/pypi/pyversions/async-sendgrid) |
4
+ | Package | [![PyPI Latest Release](https://img.shields.io/pypi/v/pandas.svg)](https://pypi.org/project/async-sendgrid/) [![PyPI Downloads](https://img.shields.io/pypi/dm/async-sendgrid.svg?label=PyPI%20downloads)](https://pypi.org/project/async-sendgrid/) |
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 simple asynchronous client based on the httpx libarary.
10
+
11
+ # Installation
12
+
13
+ It is possible to install async-sendgrid with pip:
14
+
15
+ ```bash
16
+ pip install async-sendgrid
17
+ ```
18
+
19
+ # Usage
20
+ This is a small script showing how to send an email with async-sendgrid:
21
+
22
+ First, you need to import the ```SendgridAPI``` from the ```async_sendgrid``` package. Then, you need to create a ```SendgridAPI``` object with your API key.
23
+
24
+ ```python
25
+ from async_sendgrid import SendgridAPI
26
+ import os
27
+
28
+ API_KEY = os.environ.get['API_KEY']
29
+ sendgrid = SendgridAPI(API_KEY)
30
+ ```
31
+
32
+ Thereafter, you can create an email with the original ```sendgrid``` package such:
33
+
34
+ ```python
35
+ from sendgrid.helpers.mail import Content, Email, Mail, To
36
+
37
+ from_email = Email("test@example.com")
38
+ to_email = To("test@example.com")
39
+ subject = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
40
+ content = Content("text/plain", "Sed varius ligula ac urna vehicula ultrices. Nunc ut dolor sem.")
41
+
42
+ mail = Mail(
43
+ from_email=from_email,
44
+ to_email=to_email,
45
+ subject=subject,
46
+ content=content
47
+ )
48
+
49
+ ```
50
+
51
+ Finally you can send the email with the ```send``` method of the ```SendgridAPI``` instance:
52
+
53
+ ```python
54
+ async with sendgrid as client:
55
+ response = await client.send(data)
56
+ ```
@@ -0,0 +1,3 @@
1
+ from .sendgrid import SendgridAPI # noqa
2
+
3
+ __version__ = "0.0.2"
@@ -0,0 +1,118 @@
1
+ from __future__ import annotations
2
+
3
+ from abc import ABC, abstractmethod
4
+ from typing import TYPE_CHECKING
5
+
6
+ import async_sendgrid
7
+ from async_sendgrid.utils import create_session
8
+
9
+ if TYPE_CHECKING:
10
+ from typing import Any, Optional
11
+
12
+ from httpx import Response # type: ignore
13
+ from sendgrid.helpers.mail import Mail # type: ignore
14
+
15
+
16
+ class BaseSendgridAPI(ABC):
17
+ @property
18
+ @abstractmethod
19
+ def api_key(self) -> str:
20
+ """Not implemented"""
21
+
22
+ @property
23
+ @abstractmethod
24
+ def endpoint(self) -> str:
25
+ """Not implemented"""
26
+
27
+ @property
28
+ @abstractmethod
29
+ def version(self) -> str:
30
+ """Not implemented"""
31
+
32
+ @property
33
+ @abstractmethod
34
+ def headers(self) -> dict[Any, Any]:
35
+ """Not implemented"""
36
+
37
+ @abstractmethod
38
+ async def send(self, message: dict[Any, Any] | Mail) -> Response:
39
+ """Not implemented"""
40
+
41
+
42
+ class SendgridAPI(BaseSendgridAPI):
43
+ """
44
+ Construct the Twilio SendGrid v3 API object.
45
+ Note that the underlying client is being Setup during initialization,
46
+ therefore changing attributes in runtime will not affect HTTP client
47
+ behaviour.
48
+
49
+ :param api_key: The api key issued by Sendgrid.
50
+ :param impersonate_subuser: the subuser to impersonate. Will be passed
51
+ by "On-Behalf-Of" header by underlying client.
52
+ See https://sendgrid.com/docs/User_Guide/Settings/subusers.html
53
+ for more details.
54
+ """
55
+
56
+ def __init__(
57
+ self,
58
+ api_key: str,
59
+ impersonate_subuser: Optional[str] = None,
60
+ ):
61
+ self._api_key = api_key
62
+ self._endpoint = "https://api.sendgrid.com/v3/mail/send"
63
+ self._version = async_sendgrid.__version__
64
+
65
+ self._headers = {
66
+ "Authorization": f"Bearer {self._api_key}",
67
+ "User-Agent": f"async_sendgrid/{self._version};python",
68
+ "Accept": "*/*",
69
+ "Content-Type": "application/json",
70
+ }
71
+
72
+ if impersonate_subuser:
73
+ self._headers["On-Behalf-Of"] = impersonate_subuser
74
+
75
+ self._session = create_session(self._headers)
76
+
77
+ @property
78
+ def api_key(self) -> str:
79
+ return self._api_key
80
+
81
+ @property
82
+ def endpoint(self) -> str:
83
+ return self._endpoint
84
+
85
+ @property
86
+ def version(self) -> str:
87
+ return self._version
88
+
89
+ @property
90
+ def headers(self) -> dict[Any, Any]:
91
+ return self._headers
92
+
93
+ async def send(self, message: Mail) -> Response:
94
+ """
95
+ Make a Twilio SendGrid v3 API request with the request body generated
96
+ by the Mail object
97
+
98
+ Parameters:
99
+ ----
100
+ :param message: The Twilio SendGrid v3 API request body generated
101
+ by the Mail object or dict
102
+ Returns:
103
+ ----
104
+ :return: The Twilio SendGrid v3 API response
105
+ """
106
+ json_message = message.get()
107
+ response = await self._session.post(
108
+ url=self._endpoint, json=json_message
109
+ )
110
+ return response
111
+
112
+ async def __aenter__(self):
113
+ if self._session.is_closed:
114
+ self._session = create_session(headers=self._headers)
115
+ return self
116
+
117
+ async def __aexit__(self, exc_type: Any, exc: Any, tb: Any):
118
+ await self._session.aclose()
@@ -0,0 +1,24 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from httpx import AsyncClient # type: ignore
6
+
7
+ if TYPE_CHECKING:
8
+ from typing import Any
9
+
10
+
11
+ def create_session(headers: dict[Any, Any]) -> AsyncClient:
12
+ """
13
+ create an httpx session.
14
+
15
+ Parameters
16
+ ----------
17
+ headers : dict[Any, Any]
18
+
19
+ Returns
20
+ -------
21
+ AsyncClient
22
+ """
23
+ session = AsyncClient(headers=headers)
24
+ return session
@@ -0,0 +1,47 @@
1
+ [tool.poetry]
2
+ name = "sendgrid-async"
3
+ version = "1.0.0"
4
+ description = "SendGrid using a client based on httpx."
5
+ license = "MIT"
6
+ authors = ["Elyes Mahjoubo <elyesmahjoubi@gmail.com>"]
7
+ readme = "README.md"
8
+ repository = "https://github.com/EM51641/async-sendgrid/"
9
+ keywords = ["sendgrid", "async", "httpx"]
10
+ classifiers = [
11
+ "Operating System :: OS Independent",
12
+ "License :: OSI Approved :: MIT License",
13
+ "Programming Language :: Python :: 3.10",
14
+ "Programming Language :: Python :: 3.11",
15
+ "Programming Language :: Python :: 3.12",
16
+ ]
17
+
18
+ packages = [
19
+ { include = "async_sendgrid" },
20
+ ]
21
+
22
+ [tool.poetry.dependencies]
23
+ python = "^3.10"
24
+ sendgrid = "^6.7.0"
25
+ httpx = "^0.24.1"
26
+
27
+ [tool.poetry.dev-dependencies]
28
+ pytest = "^7.4.2"
29
+ pytest-asyncio = "^0.15.1"
30
+ pytest-cov = "^4.0.0"
31
+ pytest-httpx="^0.24.0"
32
+ mypy = "^1.0.0"
33
+ flake8 = "^6.0.0"
34
+ black = "^23.9.0"
35
+ tox = "^4.11.1"
36
+
37
+ [tool.black]
38
+ line-length = 79
39
+
40
+ [tool.coverage.report]
41
+ exclude_also = [
42
+ "if TYPE_CHECKING:",
43
+ ]
44
+
45
+ [build-system]
46
+ requires = ["poetry-core"]
47
+ build-backend = "poetry.core.masonry.api"