From 2c300cb5af88e76bff305e32f8cb485dafcdf789 Mon Sep 17 00:00:00 2001 From: Mahmoud Abduljawad Date: Sun, 13 Oct 2019 20:47:13 +0400 Subject: [PATCH] [APIv5.8] Updated Timeout to Handle IP Quota, Introduced Gateways Workflow. - IP Quota Timeout is updated to 259sec. - Introduced gateways Config Attr. - Abstracted Gateway.send_email method unto function. - Refactored Gateway class to single static method send to select gateway method. --- app.py | 2 +- config.py | 6 +++-- gateway.py | 75 +++++++++++++++++++++++++----------------------------- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/app.py b/app.py index d66b4f7..5535a47 100644 --- a/app.py +++ b/app.py @@ -86,7 +86,7 @@ async def http_handler(request: aiohttp.web.Request): 'last_check':datetime.datetime.utcnow() } else: - if (datetime.datetime.utcnow() - ip_quota[str(request.remote)]['last_check']).seconds > 59: + if (datetime.datetime.utcnow() - ip_quota[str(request.remote)]['last_check']).seconds > 259: ip_quota[str(request.remote)]['last_check'] = datetime.datetime.utcnow() ip_quota[str(request.remote)]['counter'] = Config.quota_ip_min else: diff --git a/config.py b/config.py index acfc563..1521a2a 100644 --- a/config.py +++ b/config.py @@ -75,10 +75,12 @@ class Config: l10n: Dict[str, Dict[str, Any]] = {} - types: Dict[str, Callable] = {} - jobs: List[Dict[str, Any]] = [] + gateways: Dict[str, Callable] = {} + + types: Dict[str, Callable] = {} + @classmethod async def config_data(cls, modules: Dict[str, 'BaseModule']) -> None: from utils import DictObj diff --git a/gateway.py b/gateway.py index e51b2c9..0f0d473 100644 --- a/gateway.py +++ b/gateway.py @@ -1,51 +1,44 @@ from config import Config -from twilio.rest import Client - from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import COMMASPACE, formatdate + import smtplib +def email_gateway(subject, addr_to, content, content_format='html', files=[]): + if type(addr_to) == str: + addr_to = [addr_to] + addr_to = COMMASPACE.join(addr_to) + + msg = MIMEMultipart() + msg['From'] = Config.email_auth['username'] + msg['To'] = addr_to + msg['Date'] = formatdate(localtime=True) + msg['Subject'] = subject + + msg.attach(MIMEText(content, content_format)) + + for file in files: + part = MIMEApplication(file['content'], Name=file['name']) + part['Content-Disposition'] = 'attachment; filename="{}"'.format(file['name']) + msg.attach(part) + + email_server = Config.email_auth['server'] + email_username = Config.email_auth['username'] + email_password = Config.email_auth['password'] + + smtp = smtplib.SMTP_SSL(email_server) + smtp.login(email_username, email_password) + smtp.sendmail(Config.email_auth['username'], addr_to, msg.as_string()) + smtp.close() + class Gateway: - @classmethod - def send_sms(cls, to, body): - account_sid = Config.sms_auth['sid'] - auth_token = Config.sms_auth['token'] - client = Client(account_sid, auth_token) - message = client.messages.create( - body=body, - from_=Config.sms_auth['number'], - to=to - ) - return message - - @classmethod - def send_email(cls, subject, addr_to, content, content_format='html', files=[]): - if type(addr_to) == str: - addr_to = [addr_to] - addr_to = COMMASPACE.join(addr_to) - - msg = MIMEMultipart() - msg['From'] = Config.email_auth['username'] - msg['To'] = addr_to - msg['Date'] = formatdate(localtime=True) - msg['Subject'] = subject - - msg.attach(MIMEText(content, content_format)) - - for file in files: - part = MIMEApplication(file['content'], Name=file['name']) - part['Content-Disposition'] = 'attachment; filename="{}"'.format(file['name']) - msg.attach(part) - - email_server = Config.email_auth['server'] - email_username = Config.email_auth['username'] - email_password = Config.email_auth['password'] - - smtp = smtplib.SMTP_SSL(email_server) - smtp.login(email_username, email_password) - smtp.sendmail(Config.email_auth['username'], addr_to, msg.as_string()) - smtp.close() \ No newline at end of file + @staticmethod + def send(gateway, **kwargs): + if gateway == 'email': + return email_gateway(**kwargs) + else: + return Config.gateways[gateway](**kwargs) \ No newline at end of file