Skip to content

Instantly share code, notes, and snippets.

@kayode-adechinan
Created June 24, 2019 00:21
Show Gist options
  • Save kayode-adechinan/c0fd550258d8f40b8d836ef88b16c5ec to your computer and use it in GitHub Desktop.
Save kayode-adechinan/c0fd550258d8f40b8d836ef88b16c5ec to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import requests
from django.shortcuts import render
from django.http import HttpResponse
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView
from .models import Api
class gateway(APIView):
authentication_classes = ()
def operation(self, request):
path = request.path_info.split('/')
if len(path) < 2:
return Response('bad request', status=status.HTTP_400_BAD_REQUEST)
apimodel = Api.objects.filter(name=path[2])
if apimodel.count() != 1:
return Response('bad request', status=status.HTTP_400_BAD_REQUEST)
valid, msg = apimodel[0].check_plugin(request)
if not valid:
return Response(msg, status=status.HTTP_400_BAD_REQUEST)
res = apimodel[0].send_request(request)
if res.headers.get('Content-Type', '').lower() == 'application/json':
data = res.json()
else:
data = res.content
return Response(data=data, status=res.status_code)
def get(self, request):
return self.operation(request)
def post(self, request):
return self.operation(request)
def put(self, request):
return self.operation(request)
def patch(self, request):
return self.operation(request)
def delete(self, request):
return self.operation(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment