From 8d98bbd9f67ca8faf859db25141320203ccaaf61 Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 17 Sep 2020 17:36:15 +0200 Subject: [PATCH] Simplify Twilio integration --- app/{twilio.py => external_services.py} | 19 ++++++------------- app/routes.py | 2 +- 2 files changed, 7 insertions(+), 14 deletions(-) rename app/{twilio.py => external_services.py} (56%) diff --git a/app/twilio.py b/app/external_services.py similarity index 56% rename from app/twilio.py rename to app/external_services.py index 3ef20f6..fde34ff 100644 --- a/app/twilio.py +++ b/app/external_services.py @@ -1,25 +1,18 @@ -from twilio.rest import Client from secrets import randbits -from constants import ACCOUNT_ID, TOKEN, SMS_SENDER +from twilio.rest import Client + +from constants import ACCOUNT_ID, SMS_SENDER, TOKEN from database.crud import save_otp -def connect_api(): - account_sid = ACCOUNT_ID - auth_token = TOKEN +def create_twilio_client(account_sid, auth_token): client = Client(account_sid, auth_token) return client -def generate_code(): - bits = 16 - code = randbits(bits) - return code - - def send_otp(receiver): - client = connect_api() - code = generate_code() + client = create_twilio_client(account_sid=ACCOUNT_ID, auth_token=TOKEN) + code = randbits(k=16) message = "Your OTP code is {0}".format(code) client.messages.create(to=receiver, from_=SMS_SENDER, body=message) save_otp(receiver, code) diff --git a/app/routes.py b/app/routes.py index c1667ee..09025e5 100644 --- a/app/routes.py +++ b/app/routes.py @@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException, Response from sqlalchemy.orm import Session from app.schemas import * -from app.twilio import send_otp +from app.external_services import send_otp from database.crud import get_db, insert_data, verify_otp router = APIRouter()