-
-
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.
Adds a new endpoint to allow the front end to send suggestions to the project.
- Loading branch information
1 parent
b9da14c
commit 00782d9
Showing
14 changed files
with
551 additions
and
40 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,7 @@ | ||
QUERIDO_DIARIO_SUGGESTION_MAILJET_REST_API_KEY=mailjet.com | ||
QUERIDO_DIARIO_SUGGESTION_MAILJET_REST_API_SECRET=mailjet.com | ||
QUERIDO_DIARIO_SUGGESTION_SENDER_NAME=Sender Name | ||
QUERIDO_DIARIO_SUGGESTION_SENDER_EMAIL=[email protected] | ||
QUERIDO_DIARIO_SUGGESTION_RECIPIENT_NAME=Recipient Name | ||
QUERIDO_DIARIO_SUGGESTION_RECIPIENT_EMAIL=[email protected] | ||
QUERIDO_DIARIO_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,10 @@ | ||
from .model import ( | ||
Suggestion, | ||
SuggestionSent, | ||
) | ||
|
||
from .service import ( | ||
SuggestionServiceInterface, | ||
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,45 @@ | ||
class Suggestion: | ||
""" | ||
Object containing the data to suggest | ||
""" | ||
|
||
def __init__( | ||
self, email_address, name, content, | ||
): | ||
self.email_address = email_address | ||
self.name = name | ||
self.content = content | ||
|
||
def __hash__(self): | ||
return hash((self.email_address, self.name, self.content,)) | ||
|
||
def __eq__(self, other): | ||
return ( | ||
self.email_address == other.email_address | ||
and self.name == other.name | ||
and self.content == other.content | ||
) | ||
|
||
def __repr__(self): | ||
return f"Suggestion({self.email_address}, {self.name}, {self.content})" | ||
|
||
|
||
class SuggestionSent: | ||
""" | ||
Object containing the suggestion sent | ||
""" | ||
|
||
def __init__( | ||
self, success, status, | ||
): | ||
self.success = success | ||
self.status = status | ||
|
||
def __hash__(self): | ||
return hash((self.success, self.status,)) | ||
|
||
def __eq__(self, other): | ||
return self.success == other.success and self.status == other.status | ||
|
||
def __repr__(self): | ||
return f"SuggestionSent({self.success}, {self.status})" |
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,108 @@ | ||
import abc | ||
import logging | ||
|
||
from mailjet_rest import Client | ||
|
||
from .model import ( | ||
Suggestion, | ||
SuggestionSent, | ||
) | ||
|
||
|
||
class SuggestionServiceInterface(abc.ABC): | ||
""" | ||
Service to send a suggestion | ||
""" | ||
|
||
@abc.abstractmethod | ||
def add_suggestion(self, suggestion: Suggestion): | ||
""" | ||
Method to send a suggestion | ||
""" | ||
|
||
|
||
class MailjetSuggestionService(SuggestionServiceInterface): | ||
def __init__( | ||
self, | ||
mailjet_client: Client, | ||
suggestion_sender_name: str, | ||
suggestion_sender_email: str, | ||
suggestion_recipient_name: str, | ||
suggestion_recipient_email: str, | ||
suggestion_mailjet_custom_id: str, | ||
): | ||
self.mailjet_client = mailjet_client | ||
self.suggestion_sender_name = suggestion_sender_name | ||
self.suggestion_sender_email = suggestion_sender_email | ||
self.suggestion_recipient_name = suggestion_recipient_name | ||
self.suggestion_recipient_email = suggestion_recipient_email | ||
self.suggestion_mailjet_custom_id = suggestion_mailjet_custom_id | ||
self.logger = logging.getLogger(__name__) | ||
|
||
def add_suggestion(self, suggestion: Suggestion): | ||
data = { | ||
"Messages": [ | ||
{ | ||
"From": { | ||
"Name": self.suggestion_sender_name, | ||
"Email": self.suggestion_sender_email, | ||
}, | ||
"To": [ | ||
{ | ||
"Name": self.suggestion_recipient_name, | ||
"Email": self.suggestion_recipient_email, | ||
} | ||
], | ||
"Subject": "Querido Diário, hoje recebi uma sugestão", | ||
"TextPart": f"From {suggestion.name} <{suggestion.email_address}>:\n\n{suggestion.content}", | ||
"CustomID": self.suggestion_mailjet_custom_id, | ||
} | ||
] | ||
} | ||
result = self.mailjet_client.send.create(data=data) | ||
result_json = result.json() | ||
|
||
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 SuggestionSent(success=True, status="Sent") | ||
else: | ||
status = "unknown error" | ||
try: | ||
errors = [] | ||
for message in result_json["Messages"]: | ||
for error in message["Errors"]: | ||
errors.append(error["ErrorMessage"]) | ||
if errors: | ||
status = ", ".join(errors) | ||
except KeyError: | ||
pass | ||
|
||
self.logger.error( | ||
f"Could not sent message to <{suggestion.email_address}>. Status code response: {result.status_code} - {status}" | ||
) | ||
return SuggestionSent( | ||
success=False, status=f"Could not sent message: {status}" | ||
) | ||
|
||
|
||
def create_suggestion_service( | ||
suggestion_mailjet_rest_api_key: str, | ||
suggestion_mailjet_rest_api_secret: str, | ||
suggestion_sender_name: str, | ||
suggestion_sender_email: str, | ||
suggestion_recipient_name: str, | ||
suggestion_recipient_email: str, | ||
suggestion_mailjet_custom_id: str, | ||
) -> SuggestionServiceInterface: | ||
return MailjetSuggestionService( | ||
mailjet_client=Client( | ||
auth=(suggestion_mailjet_rest_api_key, suggestion_mailjet_rest_api_secret), | ||
version="v3.1", | ||
), | ||
suggestion_sender_name=suggestion_sender_name, | ||
suggestion_sender_email=suggestion_sender_email, | ||
suggestion_recipient_name=suggestion_recipient_name, | ||
suggestion_recipient_email=suggestion_recipient_email, | ||
suggestion_mailjet_custom_id=suggestion_mailjet_custom_id, | ||
) |
Oops, something went wrong.