From 4ab7cc8526378cf87aa6d1a7268838b97e26540f Mon Sep 17 00:00:00 2001 From: Muhammad Faraz Maqsood Date: Thu, 1 Aug 2024 19:44:38 +0500 Subject: [PATCH] fix: add params parameter to the requests - add missing params to the requests call - simplify all requests call into a single request helper --- forum/utils.py | 32 ++++++++++++++ forum/views.py | 110 +++---------------------------------------------- 2 files changed, 37 insertions(+), 105 deletions(-) create mode 100644 forum/utils.py diff --git a/forum/utils.py b/forum/utils.py new file mode 100644 index 00000000..f01cea85 --- /dev/null +++ b/forum/utils.py @@ -0,0 +1,32 @@ +"""Forum Utils.""" + +import logging + +import requests +from django.conf import settings + +logger = logging.getLogger(__name__) + + +def handle_proxy_requests(request, suffix, method): + """ + Catches all requests and sends it to forum/cs_comments_service urls. + """ + comments_service_url = f"http://forum:{settings.FORUM_PORT}" + url = comments_service_url + suffix + request_headers = { + "X-Edx-Api-Key": request.headers.get("X-Edx-Api-Key"), + "Accept-Language": request.headers.get("Accept-Language"), + } + request_data = request.POST.dict() or request.data + request_params = request.GET.dict() + + logger.info(f"{method} request to cs_comments_service url: {url}") + return requests.request( + method, + url, + data=request_data, + params=request_params, + headers=request_headers, + timeout=5.0, + ) diff --git a/forum/views.py b/forum/views.py index bb0453ff..11e4db58 100644 --- a/forum/views.py +++ b/forum/views.py @@ -1,14 +1,10 @@ """Forum Views.""" -import logging - -import requests -from django.conf import settings from rest_framework.permissions import AllowAny from rest_framework.response import Response from rest_framework.views import APIView -logger = logging.getLogger(__name__) +from forum.utils import handle_proxy_requests class ForumProxyAPIView(APIView): @@ -17,127 +13,31 @@ class ForumProxyAPIView(APIView): """ permission_classes = (AllowAny,) - COMMENTS_SERVICE_URL = f"http://forum:{settings.FORUM_PORT}" def post(self, request, suffix): """ Catches post requests and sends it to forum/cs_comments_service post urls. """ - request_headers = { - "X-Edx-Api-Key": request.headers.get("X-Edx-Api-Key"), - "Accept-Language": request.headers.get("Accept-Language"), - } - request_data = request.data - url = self.COMMENTS_SERVICE_URL + suffix - - """ - Will be removed once start migrating the endpoints. - login on postman before using this curl request. - sample curl request to /:commentable_id/threads POST: - curl --location 'http://local.edly.io:8000/forum/forum_proxy/api/v1/course123/threads/' \ - --header 'X-Csrftoken: kjGJPW6nPDGpHd23bBtlPhlQYooctsDDuH9SycovPI7vdWODBAstmbT1HaGWgX7Z' \ - --header 'X-Edx-Api-Key: forumapikey' \ - --header 'Accept-Language: en' \ - --header 'Content-Type: application/json' \ - --data '{ - "body": "

test post request 1

", - "anonymous": false, - "anonymous_to_peers": false, - "course_id": "course-v1:Arbisoft+SE002+2024_S2", - "commentable_id": "course", - "thread_type": "discussion", - "title": "Test" - }' - Uncomment below lines of code if want to test above curl request, - when called from edx-platform suffix should have required params - """ - # user_id = request.user.id - # url = url + (f"&user_id={user_id}" if "?" in url else f"?user_id={user_id}") - - logger.info(f"Post Request to cs_comments_service url: {url}") - response = requests.post(url, data=request_data, headers=request_headers) + response = handle_proxy_requests(request, suffix, "post") return Response(data=response.json(), status=response.status_code) def get(self, request, suffix): """ Catches get requests and sends it to forum/cs_comments_service get urls. """ - request_headers = { - "X-Edx-Api-Key": request.headers.get("X-Edx-Api-Key"), - "Accept-Language": request.headers.get("Accept-Language"), - } - request_data = request.data - url = self.COMMENTS_SERVICE_URL + suffix - - """ - Will be removed once start migrating the endpoints. - sample curl request to /:commentable_id/threads GET: - curl --location --request GET 'http://local.edly.io:8000/forum/forum_proxy/api/v1/course123/threads/' \ - --header 'X-Edx-Api-Key: forumapikey' \ - --header 'Accept-Language: en' \ - --header 'Content-Type: application/json' \ - --data '{ - "course_id": "course-v1:Arbisoft+SE002+2024_S2", - "commentable_id": "course123" - }' - Uncomment below lines of code if want to test above curl request, - when called from edx-platform suffix should have required params - """ - # course_id = request_data.get("course_id") - # url = url + (f"&course_id={course_id}" if "?" in url else f"?course_id={course_id}") - - logger.info(f"Get Request to cs_comments_service url: {url}") - response = requests.get(url, data=request_data, headers=request_headers) + response = handle_proxy_requests(request, suffix, "get") return Response(data=response.json(), status=response.status_code) def delete(self, request, suffix): """ Catches delete requests and sends it to forum/cs_comments_service delete urls. """ - request_headers = { - "X-Edx-Api-Key": request.headers.get("X-Edx-Api-Key"), - "Accept-Language": request.headers.get("Accept-Language"), - } - request_data = request.data - url = self.COMMENTS_SERVICE_URL + suffix - - """ - Will be removed once start migrating the endpoints. - sample curl request to /:commentable_id/threads DELETE: - curl --location --request DELETE 'http://local.edly.io:8000/forum/forum_proxy/api/v1/course123/threads/' \ - --header 'X-Edx-Api-Key: forumapikey' \ - --header 'Accept-Language: en' \ - --data '' - """ - - logger.info(f"Get Request to cs_comments_service url: {url}") - response = requests.delete(url, data=request_data, headers=request_headers) + response = handle_proxy_requests(request, suffix, "delete") return Response(data=response.json(), status=response.status_code) def put(self, request, suffix): """ Catches post requests and sends it to forum/cs_comments_service post urls. """ - request_headers = { - "X-Edx-Api-Key": request.headers.get("X-Edx-Api-Key"), - "Accept-Language": request.headers.get("Accept-Language"), - } - request_data = request.data - url = self.COMMENTS_SERVICE_URL + suffix - - """ - Will be removed once start migrating the endpoints. - sample curl request to /threads/:thread_id PUT: - curl --location --request PUT - 'http://local.edly.io:8000/forum/forum_proxy/api/v1/threads/66a9d1cca99edf001d4c5f77/' \ - --header 'X-Edx-Api-Key: forumapikey' \ - --header 'Accept-Language: en' \ - --header 'Content-Type: application/json' \ - --data '{ - "body": "

test post request 11

" - }' - """ - - logger.info(f"Put Request to cs_comments_service url: {url}") - response = requests.put(url, data=request_data, headers=request_headers) + response = handle_proxy_requests(request, suffix, "put") return Response(data=response.json(), status=response.status_code)