Skip to content
This repository has been archived by the owner on Aug 2, 2020. It is now read-only.

Commit

Permalink
[APIv5.8] Updated Timeout to Handle IP Quota, Introduced Gateways Wor…
Browse files Browse the repository at this point in the history
…kflow.

- 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.
  • Loading branch information
mahmoudajawad committed Oct 13, 2019
1 parent c2bb993 commit 2c300cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 34 additions & 41 deletions gateway.py
Original file line number Diff line number Diff line change
@@ -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()
@staticmethod
def send(gateway, **kwargs):
if gateway == 'email':
return email_gateway(**kwargs)
else:
return Config.gateways[gateway](**kwargs)

0 comments on commit 2c300cb

Please sign in to comment.