-
-
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.
reset some files fix api fixes set required fields fix prefix properties name rename change subject and title fix test fix test rename from SuggestionService to SuggestionServiceInterface test create_suggestion_service implements api tests
- Loading branch information
1 parent
b9da14c
commit 5fd1370
Showing
14 changed files
with
467 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,7 @@ | ||
from .model import Suggestion | ||
|
||
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,24 @@ | ||
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})" |
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,91 @@ | ||
import abc | ||
import logging | ||
|
||
from mailjet_rest import Client | ||
|
||
from .model import Suggestion | ||
|
||
|
||
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) | ||
|
||
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_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.