shippingservice/delhivery/views.py
vignesh 6e3bbee5c8
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 31s
feat: add Delhivery shipping service integration and API documentation
2026-10-10 19:39:18 +05:30

92 lines
4 KiB
Python

import json
from django.http import JsonResponse, HttpResponse
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from .client import DelhiveryAPIClient
from .models import DelhiveryActivityLog, DelhiveryCredential, DelhiveryShipment
@method_decorator(csrf_exempt, name='dispatch')
class DelhiveryRouterView(View):
"""
REST API Gateway Router for Delhivery B2C Logistics.
Handles serviceability, order booking, tracking, pricing estimate, labels,
pickup requests, cancellations, and NDR actions.
"""
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def post(self, request, action_name):
return self._handle_action(request, action_name)
def get(self, request, action_name):
return self._handle_action(request, action_name)
def _handle_action(self, request, action_name):
try:
data = json.loads(request.body.decode('utf-8')) if request.body else {}
except Exception:
data = {}
client = DelhiveryAPIClient()
try:
if action_name == 'serviceability':
pincode = request.GET.get('pincode') or data.get('pincode') or '560077'
res = client.check_serviceability(pincode)
return JsonResponse(res, status=200)
elif action_name in ['create-shipment', 'create_shipment']:
res = client.create_shipment(data.get('payload', data))
return JsonResponse(res, status=200)
elif action_name == 'track':
waybill = request.GET.get('waybill') or data.get('waybill') or '1234567890123'
res = client.track_shipment(waybill)
return JsonResponse(res, status=200)
elif action_name == 'estimate':
res = client.get_rate_estimate(
origin_pin=data.get('origin_pin', '560077'),
dest_pin=data.get('dest_pin', '110009'),
weight=data.get('weight', 500),
mode=data.get('mode', 'S'),
payment_type=data.get('payment_type', 'Prepaid')
)
return JsonResponse(res, status=200)
elif action_name == 'label':
waybills = data.get('waybills', ['1234567890123'])
pdf = data.get('pdf', False)
res = client.download_label(waybills, pdf=pdf)
if pdf:
return HttpResponse(res, content_type='application/pdf')
return JsonResponse(res, status=200)
elif action_name in ['pickup-request', 'pickup_request']:
res = client.create_pickup_request(
pickup_time=data.get('pickup_time', '14:00:00'),
pickup_date=data.get('pickup_date', '2026-10-11'),
pickup_location=data.get('pickup_location', 'Default Warehouse'),
expected_package_count=data.get('expected_package_count', 1)
)
return JsonResponse(res, status=200)
elif action_name in ['cancel-shipment', 'cancel_shipment']:
waybill = data.get('waybill') or request.GET.get('waybill')
res = client.cancel_shipment(waybill)
return JsonResponse(res, status=200)
elif action_name in ['ndr-action', 'ndr_action']:
res = client.take_ndr_action(
waybill=data.get('waybill'),
action=data.get('action', 'RE-ATTEMPT'),
deferred_date=data.get('deferred_date'),
comments=data.get('comments')
)
return JsonResponse(res, status=200)
return JsonResponse({'error': f'Delhivery action {action_name} not supported'}, status=400)
except Exception as e:
return JsonResponse({'status': False, 'error': str(e)}, status=500)