Reputation: 439
I want to chain the call of API's, what I have is:
from django.views import View
from rest_framework.views import APIView
from rest_framework.response import Response
class ChildAPI(APIView)
def get(self, request):
store_id = request.GET.get('store_id')
prodcuts = ProductsModel.objects\
.filter(store_id=store_id)\
.annotate(SUM('price'))... etc
x = products['price__sum']
y = products['tax__sum']
Now rather then returning an response from the ChildAPI
I rather would like to pass the x
and y
parameters via post
to ParentAPI
which will then return the response as:
class ParentAPI(APIView):
def post(self, request):
store_id = request.POST.get('store_id')
x = request.POST.get('x')
y = request.POST.get('y')
AwesomeModel.objects.filter(...).update(x=x,y=y)
return Response({"code": 1, "message": "Updated"})
I was reading Calling a REST API from Django view,
As the parameters aren't passed via post
and url
is hited via requests
, can't I do it without domainname.com
i.e. as we do via namespace
from Django templates
as:
<form method="post" action="{% url 'product:update-product' %}">
<input type="hidden" value="{{ x }}" name="x">
<input type="hidden" value="{{ y }}" name="y">
<input type="submit" value="Update">
</form>
Note: I have the ParentAPI url
pattern in another Django App urls
file,
The way we call one function from another can I call one API from another passing parameters wrapped via post
Here the ParentAPI
is called independently as well, so I want to pass parameters wrapped into request
via post
only. Can't pass them to function as ParentAPI.post(request, x=x)
If ParentAPI
was hit independently, then rather I would have created a function with variable argument parameter **kwarg
and called the function.
If I do so I will have:
class ParentAPI(APIView):
def post(self, request, *args, **kwargs):
x = request.POST.get('x')
if not x:
x = kwargs['x']
Basically I want to send x,y
wrapped into request
. So as it can be accessed by ParentAPI
as request.POST.get('x')
or reques.POST['x']
Upvotes: 0
Views: 1316
Reputation: 88569
Do something like this,
from rest_framework.views import APIView
class Parent(APIView):
def post(self, request, *args, **kwargs):
return Response(data={"store_id": kwargs['store_id']})
class ChildAPI(APIView):
def get(self, request, *args, **kwargs):
store_id = request.GET.get('store_id')
parent = Parent()
return parent.post(request, store_id=store_id)
Access the child api , /child/?store_id=12323
and you will get response from Parent
API
Upvotes: 2