Skip to content

driveate/ws-django-rest-framework-proxy

 
 

Repository files navigation

Django Rest Framework 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
  • Django 4.2 LTS support
  • Python 3.9+ support

Compatibility

Version Django Support Python Support Status
2.x 3.2, 4.0, 4.1, 4.2 LTS 3.9, 3.10, 3.11, 3.12 ✅ Active
1.x 2.2, 3.0, 3.1, 3.2 3.8+ 🔒 Maintenance only

Installation

poetry add ws-django-rest-framework-proxy

or

pip install ws-django-rest-framework-proxy

Quick Start

Add to your INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'rest_framework',
    'rest_framework_proxy',
    # ...
]

Configure proxy settings:

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

Usage

Simple Proxy View

# urls.py
from django.urls import path
from rest_framework_proxy.views import ProxyView

urlpatterns = [
    # Basic proxy
    path('api/items/', ProxyView.as_view(source='items/'), name='item-list'),
    
    # With URL parameters
    path('api/items/<int:pk>/', ProxyView.as_view(source='items/%(pk)s/'), name='item-detail'),
]

Custom Proxy View

# views.py
from rest_framework_proxy.views import ProxyView

class ItemListProxy(ProxyView):
    """
    Proxy for item list API
    """
    source = 'items/'
    
class ItemDetailProxy(ProxyView):
    """
    Proxy for item detail API
    """
    source = 'items/%(pk)s/'

Development

Setup

git clone https://github.com/driveate/ws-django-rest-framework-proxy.git
cd ws-django-rest-framework-proxy
poetry install

Running Tests

# Run all tests
poetry run python -m django test tests --settings=tests.settings

# Run with coverage
poetry run pytest --cov=rest_framework_proxy

# Run linting
poetry run flake8 rest_framework_proxy/
poetry run black --check rest_framework_proxy/
poetry run mypy rest_framework_proxy/

Testing Against Multiple Django Versions

poetry run tox

Migration from 1.x to 2.x

Version 2.0 introduces breaking changes for Django 4.2 compatibility:

Breaking Changes

  1. Python 3.9+ Required: Dropped support for Python 3.8
  2. Django 3.2+ Required: Dropped support for Django 2.x and 3.0/3.1
  3. Removed six dependency: No longer needed for Python 3.9+

Migration Steps

  1. Update your Python version to 3.9 or higher
  2. Update Django to 3.2 or higher
  3. Update the package: poetry add ws-django-rest-framework-proxy@^2.0.0
  4. Test your proxy views thoroughly

No code changes should be required in most cases, as the API remains the same.

License

Copyright (c) 2014, Tomi Pajunen All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

About

Django Rest Framework Proxy views

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%