0

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

UPDATED:

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']

Aashish Gahlawat
  • 409
  • 1
  • 7
  • 25

1 Answers1

2

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

JPG
  • 82,442
  • 19
  • 127
  • 206
  • won't this change the way `ParentAPI` accept the parameters? Now It is taking them via `kwargs['x']`, whereas earlier it was getting them as `request.POST.get('x')` – Aashish Gahlawat Aug 19 '18 at 14:44
  • 1
    You can't alter the `request.POST` attribute, because `request` object is ***immutable*** – JPG Aug 19 '18 at 15:33
  • 1
    You can't ***mimic*** the `HTTP POST` request *directly*. What you can try is , mock the request object.. [see the realted post](https://stackoverflow.com/questions/49306177/post-document-with-django-requestfactory-instead-of-form-data) – JPG Aug 19 '18 at 15:39