I implemented a Django REST API using Django Rest Framework.
For this API I have to call 3 http web services and I merge the data results like response of my API.
So, I have:
- Request WS 1
- Request WS 2
- Request WS 3
--> RESULT = Merge Results 1, 2, 3
----> Response: RESULT
Is it possible to have Request WS 1, 2,3 as asynchronous requests?
Is it a good idea?
Its not a good idea to use threads to do that, threading adds more complexity and could be hard to debug. In that case of situation you could use a task scheduler (Celery, Django-RQ, etc). You have to choose one based on your system. Celery is well-known but could be too much for your system.
I recommend to approach this using HTTP statuses and some basic payload. The idea is to get the request, send the "get to third party sites task" to the task scheduler and return a "polling URL" with status 202. By "Polling URL" I mean an API endpoint, where the user could check the status of the task. HTTP Status 202 tells the user that the request has been accepted but the processing has not finished (its async).
Then the callee of the API could poll the "polling URL" every X times, to fetch the task's result. Example:
def api_process_request(request):
data = process_data(request)
polling_url = send_task_to_scheduler(data)
return JsonResponse({'url': polling_url}, status=202)
def api_polling_url(request, task_id):
done, result = task_scheduler_get_result(task_id)
return JsonResponse({'result': result, 'status': done}, status=200)
Hope it helps you! Good luck :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With