-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
165 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ tomli==2.0.1 | |
urllib3==1.26.18 | ||
webencodings==0.5.1 | ||
tomli-w==1.0.0 | ||
mailjet-rest==1.3.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import logging | ||
|
||
from mailjet_rest import Client | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class MailjetEmailClient: | ||
def __init__(self, to_email, api_key, api_secret, _sender_email): | ||
self._to_email = to_email | ||
self._mailjet = Client(auth=(api_key, api_secret), version="v3.1") | ||
self._sender_email = _sender_email | ||
|
||
def send_email(self, subject, body): | ||
data = { | ||
"Messages": [ | ||
{ | ||
"From": {"Email": self._sender_email, "Name": "동행복권 API"}, | ||
"To": [{"Email": self._to_email, "Name": self._to_email}], | ||
"Subject": subject, | ||
"TextPart": body, | ||
"HTMLPart": "", | ||
} | ||
] | ||
} | ||
|
||
result = self._mailjet.send.create(data=data) | ||
logging.debug(result.json()) | ||
|
||
if result.status_code == 200: | ||
logger.info("메일 전송에 성공했습니다.") | ||
else: | ||
logger.error(f"메일 전송에 실패했습니다. (result: {result.json()})") | ||
raise RuntimeError(f"메일 전송에 실패했습니다. (status_code: {result.status_code})") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import sys | ||
from unittest import TestCase | ||
from unittest.mock import patch | ||
|
||
from dhapi.router.arg_parser import ArgParser | ||
|
||
|
||
class TestArgParser(TestCase): | ||
@patch("dhapi.router.arg_parser.get_credentials", return_value={"username": "test_user", "password": "test_pw"}) | ||
@patch.object(sys, 'argv', ["dhapi", "buy_lotto645", "-e", "[email protected]"]) | ||
def test_given_email_flag_is_set_and_api_credentials_not_set_when_arg_parser_init_then_raise_runtime_error(self, get_credentials_mock): | ||
self.assertRaises(RuntimeError, ArgParser) | ||
|
||
@patch("dhapi.router.arg_parser.get_credentials", return_value={"mailjet_api_key": "test_key", "mailjet_api_secret": "test_secret", "mailjet_sender_email": "[email protected]"}) | ||
@patch.object(sys, 'argv', ["dhapi", "buy_lotto645", "-e", "[email protected]"]) | ||
def test_given_email_flag_is_set_and_api_credentials_set_when_arg_parser_init_then_raise_runtime_error(self, get_credentials_mock): | ||
ArgParser() | ||
# no error means success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import unittest | ||
|
||
from dhapi.client.mailjet_email_client import MailjetEmailClient | ||
|
||
|
||
class TestMailjetEmailClient(unittest.TestCase): | ||
|
||
@unittest.skip("sending real email is not needed after checking success") | ||
def test_send_email(self): | ||
sut = MailjetEmailClient("EMAIL", "API_KEY", "SECRET_KEY") | ||
|
||
sut.send_email("hello", "world") |