From dc60cf95b2b517ffff3419a83640d037b6def5ab Mon Sep 17 00:00:00 2001 From: vickytechkey Date: Sun, 4 Oct 2026 10:23:42 +0530 Subject: [PATCH] feat: implement user registration, profile management, and WhatsApp OTP authentication views --- api/views.py | 39 +++++++++++++++++++++++++++++++-------- requirements.txt | 2 ++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/api/views.py b/api/views.py index da2b5f6..d7ab742 100644 --- a/api/views.py +++ b/api/views.py @@ -1,5 +1,7 @@ import logging -import requests +import json +import urllib.request +import urllib.error from django.conf import settings from django.core.cache import cache from rest_framework import status, permissions @@ -70,13 +72,21 @@ class SendWhatsAppOTPView(APIView): } try: - res = requests.post(whatsapp_service_url, json=payload, timeout=10) - try: - res_data = res.json() - except Exception: - res_data = {"text": res.text} + req_data = json.dumps(payload).encode('utf-8') + req = urllib.request.Request( + whatsapp_service_url, + data=req_data, + 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({ "status": "success", "message": f"WhatsApp OTP dispatched to {mobile_number}", @@ -84,13 +94,26 @@ class SendWhatsAppOTPView(APIView): "gateway_response": res_data }, status=status.HTTP_200_OK) 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({ "status": "success", "message": f"WhatsApp OTP request submitted for {mobile_number}", "mobile_number": mobile_number, "gateway_response": res_data }, 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: audit_logger.error(f"WhatsApp service connection error for {mobile_number}: {e}") return Response({ diff --git a/requirements.txt b/requirements.txt index fb300c6..c639549 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ djangorestframework-simplejwt django-cors-headers gunicorn psycopg2-binary +requests +