Skip to content

Commit

Permalink
fixes for handling the request data as per content type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachinbisht27 committed Jan 11, 2024
1 parent d730a1e commit 6f8e869
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
10 changes: 10 additions & 0 deletions api/models/call_log_event.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from api.mixins import TimestampMixin
from api import db
from flask_sqlalchemy.query import Query as BaseQuery


class CallLogEventQuery(BaseQuery):
def call_sid_exist(self, form_data):
return (
self.filter(CallLogEvent.call_sid == form_data["CallSid"]).first()
is not None
)


class CallLogEvent(TimestampMixin, db.Model):
query_class = CallLogEventQuery
__tablename__ = "call_log_event"
id = db.Column(db.Integer, primary_key=True)
call_sid = db.Column(db.String(255))
Expand Down
12 changes: 12 additions & 0 deletions api/models/system_phone.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
from api.mixins import TimestampMixin
from api import db
from flask_sqlalchemy.query import Query as BaseQuery


class SystemPhoneQuery(BaseQuery):
def system_phone_exists(self, url_decoded_system_phone):
return (
self.filter(
SystemPhone.phone.contains(url_decoded_system_phone[-10:])
).first()
is not None
)


class SystemPhone(TimestampMixin, db.Model):
query_class = SystemPhoneQuery
__tablename__ = "system_phone"
id = db.Column(db.Integer, primary_key=True)
phone = db.Column(db.String(50))
Expand Down
3 changes: 1 addition & 2 deletions api/services/call_log_event_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# This file is treated as service layer
from flask import request
from api import helpers, models, db
from api import helpers, models
from datetime import datetime
from utils.loggingutils import logger
import traceback
Expand Down
13 changes: 5 additions & 8 deletions api/services/handle_event_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests
from flask import request
from api import models, db, app
from api import models, app
from . import user_service as user
from . import registration_service as registration
from . import call_log_event_service as call_log_event
Expand All @@ -10,18 +9,16 @@

class HandleEventService:
def handle_event_service(self, form_data):
call_sid_exist = db.session.query(
db.exists().where(models.CallLogEvent.call_sid == form_data["CallSid"])
).scalar()
call_sid_exist = models.CallLogEvent.query.call_sid_exist(form_data)

if call_sid_exist:
return

url_decoded_system_phone = requests.utils.unquote(form_data["To"])

system_phone_exists = db.session.query(
db.exists().where(models.SystemPhone.phone == url_decoded_system_phone)
).scalar()
system_phone_exists = models.SystemPhone.query.system_phone_exists(
url_decoded_system_phone
)

if not system_phone_exists:
return
Expand Down
2 changes: 1 addition & 1 deletion api/services/registration_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RegistrationService:
# Create a registartion
def create_registration(self, data):
system_phone_data = models.SystemPhone.query.filter_by(
phone=data["to_number"]
phone=data["to_number"][-10:]
).first()

if not system_phone_data:
Expand Down
20 changes: 13 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# from pprint import pprint
from api import models, app, db, services
from flask import jsonify, request
from api import app, services, helpers
from flask import jsonify
import json
from utils.loggingutils import logger


def callback(request):
with app.app_context():
json_data = None
form_data = None

if request.method == "POST":
json_data = request.get_json()
form_data = request.form
content_type = request.headers.get("Content-Type")

if content_type == "application/json":
json_data = request.get_json()
else:
form_data = request.form

transaction_log_service = services.TransactionLogService()

try:
Expand Down Expand Up @@ -50,8 +57,7 @@ def retry_failed_webhook(transaction_log_service):
payload["log_created_on"] = log.created_on
log.processed = process_form_data(payload)
log.attempts += 1
db.session.add(log)
db.session.commit()
helpers.save(log)


def process_form_data(form_data):
Expand Down

0 comments on commit 6f8e869

Please sign in to comment.