-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
341 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
__pycache__ | ||
.coverage | ||
Makefile | ||
README.md | ||
.github/ | ||
LICENCE | ||
config/sample.env | ||
config/current.env |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.coverage | ||
__pycache__ | ||
config/current.env |
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 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,6 @@ | ||
SUGGESTION_MAILJET_REST_API_KEY=mailjet.com | ||
SUGGESTION_MAILJET_REST_API_SECRET=mailjet.com | ||
SUGGESTION_EMAIL_SENDER=[email protected] | ||
SUGGESTION_EMAIL_RECIPIENT=[email protected] | ||
SUGGESTION_NAME=ok | ||
SUGGESTION_MAILJET_CUSTOM_ID=AppCustomID |
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 |
---|---|---|
|
@@ -7,3 +7,4 @@ uvicorn==0.11.8 | |
psycopg2==2.8.5 | ||
SQLAlchemy==1.3.19 | ||
elasticsearch==7.9.1 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .model import Suggestion | ||
|
||
from .service import ( | ||
SuggestionService, | ||
MailjetSuggestionService, # only for test | ||
create_suggestion_service, | ||
) |
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,26 @@ | ||
class Suggestion: | ||
""" | ||
Object containing the data to suggest | ||
""" | ||
|
||
def __init__( | ||
self, title, email_address, name, content, | ||
): | ||
self.title = title | ||
self.email_address = email_address | ||
self.name = name | ||
self.content = content | ||
|
||
def __hash__(self): | ||
return hash((self.title, self.email_address, self.name, self.content,)) | ||
|
||
def __eq__(self, other): | ||
return ( | ||
self.title == other.title | ||
and self.email_address == other.email_address | ||
and self.name == other.name | ||
and self.content == other.content | ||
) | ||
|
||
def __repr__(self): | ||
return f"Suggestion({self.title}, {self.email_address}, {self.name}, {self.content})" |
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,87 @@ | ||
import abc | ||
import logging | ||
|
||
from mailjet_rest import Client | ||
|
||
from .model import Suggestion | ||
|
||
|
||
class SuggestionService(abc.ABC): | ||
""" | ||
Service to send a suggestion | ||
""" | ||
|
||
@abc.abstractmethod | ||
def add_suggestion(self, suggestion: Suggestion): | ||
""" | ||
Method to send a suggestion | ||
""" | ||
|
||
|
||
class MailjetSuggestionService(SuggestionService): | ||
def __init__( | ||
self, | ||
mailjet_client: Client, | ||
suggestion_email_sender: str, | ||
suggestion_email_recipient: str, | ||
suggestion_name: str, | ||
suggestion_mailjet_custom_id: str, | ||
): | ||
self.mailjet_client = mailjet_client | ||
self.suggestion_email_sender = suggestion_email_sender | ||
self.suggestion_email_recipient = suggestion_email_recipient | ||
self.suggestion_name = suggestion_name | ||
self.suggestion_mailjet_custom_id = suggestion_mailjet_custom_id | ||
self.logger = logging.getLogger(__name__) | ||
|
||
def add_suggestion(self, suggestion: Suggestion): | ||
data = { | ||
"Messages": [ | ||
{ | ||
"From": { | ||
"Email": self.suggestion_email_sender, | ||
"Name": suggestion.name, | ||
}, | ||
"To": [ | ||
{ | ||
"Email": self.suggestion_email_recipient, | ||
"Name": self.suggestion_name, | ||
} | ||
], | ||
"Subject": suggestion.title, | ||
"TextPart": f"From <{suggestion.email_address}>:\n\n{suggestion.content}", | ||
"CustomID": self.suggestion_mailjet_custom_id, | ||
} | ||
] | ||
} | ||
result = self.mailjet_client.send.create(data=data) | ||
|
||
self.logger.debug(f"Suggestion body response {result.json()}") | ||
if 200 <= result.status_code <= 299: | ||
self.logger.info(f"Suggestion created for {suggestion.email_address}") | ||
return True | ||
else: | ||
self.logger.error( | ||
f"Error on send email {suggestion.email_address}. Status code response: {result.status_code}" | ||
) | ||
return False | ||
|
||
|
||
def create_suggestion_service( | ||
suggestion_mailjet_rest_api_key: str, | ||
suggestion_mailjet_rest_api_secret: str, | ||
suggestion_email_sender: str, | ||
suggestion_email_recipient: str, | ||
suggestion_name: str, | ||
suggestion_mailjet_custom_id: str, | ||
) -> SuggestionService: | ||
return MailjetSuggestionService( | ||
mailjet_client=Client( | ||
auth=(suggestion_mailjet_rest_api_key, suggestion_mailjet_rest_api_secret), | ||
version="v3.1", | ||
), | ||
suggestion_email_sender=suggestion_email_sender, | ||
suggestion_email_recipient=suggestion_email_recipient, | ||
suggestion_name=suggestion_name, | ||
suggestion_mailjet_custom_id=suggestion_mailjet_custom_id, | ||
) |
Oops, something went wrong.