What's the most elegant way to convert requests' response to DRF response in Django?

Viewed 2795

Consider the following flow:

public client ----> DRF API on Service A ------> DRF API on Service B

Some of the DRF API on Service A merely proxying to Service B, so in the particular API on Service A looks like this:

class SomeServiceAPI(APIView):
    def get(request):
        resp = requests.get('http://service-b.com/api/...')
        return Response(resp.json())

While this works on normal status, but it has a few issues:

  1. It doesn't proxy the actual status code from service b.
  2. Unnecessary round-trip of json serialization within Response()
  3. If service b returns a non-json error, service does not return actual error from service b.

The question is, is there a better way to do it? I had a look at Django Rest Framework Proxy project, but I am not entirely sure if it actually suits my use case here.

2 Answers
Related