Skip to main content

Django Rest Framework proxy API

Project description

Django API Proxy

PyPI version

Provides views to redirect incoming request to another API server.

Features:

  • Masquerade paths
  • HTTP Basic Auth (between your API and backend API)
  • Token Auth
  • Supported methods: GET/POST/PUT/PATCH/DELETE
  • File uploads

TODO:

  • Pass auth information from original client to backend API

Installation

$ pip install django-api-proxy

Usage

There are couple of ways to use proxies. You can either use provided views as is or subclass them.

Settings

# settings.py
API_PROXY = {
    'HOST': 'https://api.example.com',
    'AUTH': {
        'user': 'myuser',
        'password': 'mypassword',
        # Or alternatively:
        'token': 'Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b',
    },
}

Simple way

# urls.py
from django_api_proxy.views import ProxyView

# Basic
url(r'^item/$', ProxyView.as_view(source='items/'), name='item-list'),

# With captured URL parameters
url(r'^item/(?P<pk>[0-9]+)$', ProxyView.as_view(source='items/%(pk)s'), name='item-detail'),

Complex way

# views.py
from django_api_proxy.views import ProxyView

class ItemListProxy(ProxyView):
  """
  List of items
  """
  source = 'items/'

class ItemDetailProxy(ProxyView):
  """
  Item detail
  """
  source = 'items/%(pk)s'
# urls.py
from views import ProxyListView, ProxyDetailView

url(r'^item/$', ProxyListView.as_view(), name='item-list'),
url(r'^item/(?P<pk>[0-9]+)$', ProxyDetailView.as_view(), name='item-detail'),

Settings

Setting Default Comment
HOST None Proxy 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.
TIMEOUT None Timeout 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 Verification

By default, django-api-proxy will 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 the VERIFY_SSL value in the REST_PROXY settings.

Additionally, one may set the verify_proxy settings on their proxy class:

# views.py
from django_api_proxy.views import ProxyView

class ItemListProxy(ProxyView):
  """
  List of items
  """
  source = 'items/'
  verify_ssl = False

Finally, if there is complex business logic needed to determine if one should verify SSL, then you can override the get_verify_ssl() method on your proxy view class:

# views.py
from django_api_proxy.views import ProxyView

class ItemListProxy(ProxyView):
  """
  List of items
  """
  source = 'items/'

  def get_verify_ssl(self, request):
    host = self.get_proxy_host(request)
    if host.startswith('intranet.'):
      return True
    return False

Permissions

You can limit access by using Permission classes and custom Views. See http://django-rest-framework.org/api-guide/permissions.html for more information

# permissions.py
from rest_framework.permissions import BasePermission, SAFE_METHODS

class AdminOrReadOnly(BasePermission):
    """
    Read permission for everyone. Only admins can modify content.
    """
    def has_permission(self, request, view, obj=None):
        if (request.method in SAFE_METHODS or
            request.user and request.user.is_staff):
            return True
        return False
# views.py
from django_api_proxy.views import ProxyView
from permissions import AdminOrReadOnly

class ItemListProxy(ProxyView):
    permission_classes = (AdminOrReadOnly,)

License

django_api_proxy is offered under the Simplified BSD License

Credits

django_api_proxy is a fork of django-rest-framework-proxy (https://github.com/eofs/django-rest-framework-proxy) created by Tomi Pajunen

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-api-proxy-0.1.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

django_api_proxy-0.1.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file django-api-proxy-0.1.1.tar.gz.

File metadata

  • Download URL: django-api-proxy-0.1.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.27.1 setuptools/59.6.0 requests-toolbelt/0.9.1 tqdm/4.55.2 CPython/3.9.9

File hashes

Hashes for django-api-proxy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7a8b8e657957fa65948f4214a226cb2a59541cfb51a91e8a6d275c57905436fb
MD5 71015d510d83176d522788eb6bbe9e1e
BLAKE2b-256 7c0392eb45dd191caf732ef62fe0eeb22571e4a6ccbb22963df87eaaad2991a6

See more details on using hashes here.

File details

Details for the file django_api_proxy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_api_proxy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.27.1 setuptools/59.6.0 requests-toolbelt/0.9.1 tqdm/4.55.2 CPython/3.9.9

File hashes

Hashes for django_api_proxy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ce389265af6ceb10e6a3be9acaa01fd51709b9e302d7b60c0e99d0dd07d90f8d
MD5 82ec8224bfb2c8c51554d1889bcf1f6f
BLAKE2b-256 b7a1379243d5a5ad48a41582755a200b244d8b9c118ff0be233a102dc45155d0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page