-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (58 loc) · 2.68 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
from typing import Optional
from datetime import datetime
from src.utils.utils import datetime_now
from src.emailer import Emailer
from src.events.events import EventProcessor
events_instance: EventProcessor = EventProcessor()
emailer_instance: Emailer = Emailer()
app = FastAPI()
events_instance.init_consume()
class EmailModel(BaseModel):
email: str
subject: str
text: str
html: str
o_tag: Optional[str]
class EmailSchedulerModel(EmailModel):
time_scheduled: datetime
job_name: str
@app.get("/")
async def root():
return dict(message='root says hello')
@app.post("/send-mail")
async def send_email(email_data: EmailModel) -> tuple:
"""
**send_email**
this end-points sends an email to a target recipients given only email, subject, text, html and o_tag
:param email_data: email data_type
:return:
"""
_response = await emailer_instance._send_with_mailgun_rest_api(to_list=[email_data.email], subject=email_data.subject,
text=email_data.text, html=email_data.html,
o_tag=email_data.o_tag)
data, status_code = _response
return dict(status=True, message='email was successfully sent', payload=data), status_code
@app.post("/schedule-mail")
async def schedule_mail(schedule_data: EmailSchedulerModel):
"""
**schedule_mail**
schedules an email to be sent at a later date & time
in order to check if the email was sent or not use the job_id and job_name to find out if the email was sent
:param schedule_data:
:return: job_id, job_name, time_scheduled of the scheduled job
"""
_kwargs: dict = dict(to_list=[schedule_data.email], subject=schedule_data.subject, text=schedule_data.text,
html=schedule_data.html, o_tag=schedule_data.o_tag)
_job_name: str = emailer_instance._create_job_name(header_name=schedule_data.job_name)
if datetime_now() > schedule_data.time_scheduled:
_message: str = 'time scheduled has already passed'
return dict(status=False, message=_message)
_payload: dict = await emailer_instance._base_email_scheduler(func=emailer_instance._send_with_mailgun_rest_api,
kwargs=_kwargs, job_name=_job_name,
time_scheduled=schedule_data.time_scheduled)
return dict(status=True, message='email scheduled', payload=_payload), 201
if __name__ == "__main__":
uvicorn.run('main:app', host='0.0.0.0', port=8000)