From 017897453c39bbcfd767551a37dc4f59c5ffa404 Mon Sep 17 00:00:00 2001 From: vickytechkey Date: Sun, 4 Oct 2026 10:27:41 +0530 Subject: [PATCH] feat: add API views for user registration, profile management, and WhatsApp OTP verification --- api/views.py | 94 ++++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 43 deletions(-) diff --git a/api/views.py b/api/views.py index d7ab742..7ded446 100644 --- a/api/views.py +++ b/api/views.py @@ -64,61 +64,69 @@ class SendWhatsAppOTPView(APIView): clean_phone = str(mobile_number).replace('+', '').strip() cache.set(f"whatsapp_otp_{clean_phone}", str(otp_code), timeout=300) - whatsapp_service_url = getattr(settings, 'WHATSAPP_SERVICE_URL', 'http://betawhatsapp.tradhox.com/api/send/') + whatsapp_urls = [ + getattr(settings, 'WHATSAPP_SERVICE_URL', 'https://betawhatsapp.tradhox.com/api/send/'), + 'http://betawhatsapp.tradhox.com/api/send/', + 'http://127.0.0.1:8081/api/send/' + ] + payload = { "mobile_number": mobile_number, "otp_code": otp_code, "template_name": template_name } - try: - 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')} + req_data = json.dumps(payload).encode('utf-8') + last_error = None + res_data = None + status_code = 500 - if status_code == 200 and res_data.get('status') != 'error': - return Response({ - "status": "success", - "message": f"WhatsApp OTP dispatched to {mobile_number}", - "mobile_number": mobile_number, - "gateway_response": res_data - }, status=status.HTTP_200_OK) - else: - 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') + for url in whatsapp_urls: 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}") + req = urllib.request.Request( + url, + data=req_data, + headers={'Content-Type': 'application/json', 'User-Agent': 'TradhoxBackend/1.0'} + ) + with urllib.request.urlopen(req, timeout=8) 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 status_code == 200 and res_data.get('status') != 'error': + return Response({ + "status": "success", + "message": f"WhatsApp OTP dispatched to {mobile_number}", + "mobile_number": mobile_number, + "gateway_response": res_data + }, status=status.HTTP_200_OK) + except urllib.error.HTTPError as e: + status_code = e.code + err_body = e.read().decode('utf-8', errors='ignore') + try: + res_data = json.loads(err_body) + except Exception: + res_data = {"text": err_body} + last_error = f"HTTP {e.code}: {res_data}" + audit_logger.warning(f"WhatsApp gateway attempt {url} returned HTTP {e.code}: {res_data}") + except Exception as e: + last_error = str(e) + audit_logger.warning(f"WhatsApp gateway attempt {url} failed: {e}") + + if status_code == 200: return Response({ - "status": "warning", - "message": f"WhatsApp gateway returned status {e.code}", - "gateway_response": err_data, - "mobile_number": mobile_number + "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 Exception as e: - audit_logger.error(f"WhatsApp service connection error for {mobile_number}: {e}") + else: return Response({ "status": "error", - "message": f"Could not reach WhatsApp service gateway: {str(e)}", + "message": f"WhatsApp service gateway error: {last_error}", "mobile_number": mobile_number }, status=status.HTTP_500_INTERNAL_SERVER_ERROR)