Package: django-api-proxy
Django API ProxyProvides views to redirect incoming request to another API server.Features:Masquerade pathsHTTP Basic Auth (between your API and backend API)Token AuthSupported methods: GET/POST/PUT/PATCH/DELETEFile uploadsTODO:Pass auth information from original client to backend APIInstallation$pipinstalldjango-api-proxyUsageThere are couple of ways to use proxies. You can either use provided views as is or subclass them.Settings# settings.pyAPI_PROXY={'HOST':'https://api.example.com','AUTH':{'user':'myuser','password':'mypassword',# Or alternatively:'token':'Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b',},}Simple way# urls.pyfromdjango_api_proxy.viewsimportProxyView# Basicurl(r'^item/$',ProxyView.as_view(source='items/'),name='item-list'),# With captured URL parametersurl(r'^item/(?P<pk>[0-9]+)$',ProxyView.as_view(source='items/%(pk)s'),name='item-detail'),Complex way# views.pyfromdjango_api_proxy.viewsimportProxyViewclassItemListProxy(ProxyView):"""List of items"""source='items/'classItemDetailProxy(ProxyView):"""Item detail"""source='items/%(pk)s'# urls.pyfromviewsimportProxyListView,ProxyDetailViewurl(r'^item/$',ProxyListView.as_view(),name='item-list'),url(r'^item/(?P<pk>[0-9]+)$',ProxyDetailView.as_view(),name='item-detail'),SettingsSettingDefaultCommentHOSTNoneProxy request to this host (e.g.https://example.com/api/)AUTH{'user': None, 'password': None, 'token': None}Proxy requests using HTTP Basic or Token Authentication.Token is only used if user & password are not provided.TIMEOUTNoneTimeout value for proxy requests.ACCEPT_MAPS{'text/html': 'application/json'}Modify Accept-headers before proxying them. You can use this to disallow certain types. By default text/html is translated to return JSON data.DISALLOWED_PARAMS('format',)Remove defined query parameters from proxy request.SSL VerificationBy default,django-api-proxywill verify the SSL certificates when proxying requests, defaulting
to security. In some cases, it may be desirable to not verify SSL certificates. This setting can be modified
by overriding theVERIFY_SSLvalue in theREST_PROXYsettings.Additionally, one may set theverify_proxysettings on their proxy class:# views.pyfromdjango_api_proxy.viewsimportProxyViewclassItemListProxy(ProxyView):"""List of items"""source='items/'verify_ssl=FalseFinally, if there is complex business logic needed to determine if one should verify SSL, then
you can override theget_verify_ssl()method on your proxy view class:# views.pyfromdjango_api_proxy.viewsimportProxyViewclassItemListProxy(ProxyView):"""List of items"""source='items/'defget_verify_ssl(self,request):host=self.get_proxy_host(request)ifhost.startswith('intranet.'):returnTruereturnFalsePermissionsYou can limit access by using Permission classes and custom Views.
Seehttp://django-rest-framework.org/api-guide/permissions.htmlfor more information# permissions.pyfromrest_framework.permissionsimportBasePermission,SAFE_METHODSclassAdminOrReadOnly(BasePermission):"""Read permission for everyone. Only admins can modify content."""defhas_permission(self,request,view,obj=None):if(request.methodinSAFE_METHODSorrequest.userandrequest.user.is_staff):returnTruereturnFalse# views.pyfromdjango_api_proxy.viewsimportProxyViewfrompermissionsimportAdminOrReadOnlyclassItemListProxy(ProxyView):permission_classes=(AdminOrReadOnly,)Licensedjango_api_proxyis offered under the Simplified BSD LicenseCreditsdjango_api_proxyis a fork of django-rest-framework-proxy (https://github.com/eofs/django-rest-framework-proxy) created by Tomi Pajunen