feat: implement user registration, profile management, and WhatsApp OTP authentication views
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 24s
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 24s
This commit is contained in:
parent
653f0a219e
commit
dc60cf95b2
2 changed files with 33 additions and 8 deletions
39
api/views.py
39
api/views.py
|
|
@ -1,5 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import json
|
||||||
|
import urllib.request
|
||||||
|
import urllib.error
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from rest_framework import status, permissions
|
from rest_framework import status, permissions
|
||||||
|
|
@ -70,13 +72,21 @@ class SendWhatsAppOTPView(APIView):
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = requests.post(whatsapp_service_url, json=payload, timeout=10)
|
req_data = json.dumps(payload).encode('utf-8')
|
||||||
try:
|
req = urllib.request.Request(
|
||||||
res_data = res.json()
|
whatsapp_service_url,
|
||||||
except Exception:
|
data=req_data,
|
||||||
res_data = {"text": res.text}
|
headers={'Content-Type': 'application/json', 'User-Agent': 'TradhoxBackend/1.0'}
|
||||||
|
)
|
||||||
|
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||||
|
status_code = resp.status
|
||||||
|
body_bytes = resp.read()
|
||||||
|
try:
|
||||||
|
res_data = json.loads(body_bytes.decode('utf-8'))
|
||||||
|
except Exception:
|
||||||
|
res_data = {"text": body_bytes.decode('utf-8', errors='ignore')}
|
||||||
|
|
||||||
if res.status_code == 200 and res_data.get('status') != 'error':
|
if status_code == 200 and res_data.get('status') != 'error':
|
||||||
return Response({
|
return Response({
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"message": f"WhatsApp OTP dispatched to {mobile_number}",
|
"message": f"WhatsApp OTP dispatched to {mobile_number}",
|
||||||
|
|
@ -84,13 +94,26 @@ class SendWhatsAppOTPView(APIView):
|
||||||
"gateway_response": res_data
|
"gateway_response": res_data
|
||||||
}, status=status.HTTP_200_OK)
|
}, status=status.HTTP_200_OK)
|
||||||
else:
|
else:
|
||||||
audit_logger.warning(f"WhatsApp gateway response for {mobile_number}: {res.status_code} {res_data}")
|
audit_logger.warning(f"WhatsApp gateway response for {mobile_number}: {status_code} {res_data}")
|
||||||
return Response({
|
return Response({
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"message": f"WhatsApp OTP request submitted for {mobile_number}",
|
"message": f"WhatsApp OTP request submitted for {mobile_number}",
|
||||||
"mobile_number": mobile_number,
|
"mobile_number": mobile_number,
|
||||||
"gateway_response": res_data
|
"gateway_response": res_data
|
||||||
}, status=status.HTTP_200_OK)
|
}, status=status.HTTP_200_OK)
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
err_body = e.read().decode('utf-8', errors='ignore')
|
||||||
|
try:
|
||||||
|
err_data = json.loads(err_body)
|
||||||
|
except Exception:
|
||||||
|
err_data = {"text": err_body}
|
||||||
|
audit_logger.warning(f"WhatsApp gateway HTTPError {e.code} for {mobile_number}: {err_data}")
|
||||||
|
return Response({
|
||||||
|
"status": "warning",
|
||||||
|
"message": f"WhatsApp gateway returned status {e.code}",
|
||||||
|
"gateway_response": err_data,
|
||||||
|
"mobile_number": mobile_number
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
audit_logger.error(f"WhatsApp service connection error for {mobile_number}: {e}")
|
audit_logger.error(f"WhatsApp service connection error for {mobile_number}: {e}")
|
||||||
return Response({
|
return Response({
|
||||||
|
|
|
||||||
|
|
@ -4,3 +4,5 @@ djangorestframework-simplejwt
|
||||||
django-cors-headers
|
django-cors-headers
|
||||||
gunicorn
|
gunicorn
|
||||||
psycopg2-binary
|
psycopg2-binary
|
||||||
|
requests
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue