Skip to main content

Easy to proxy any APIs you want.

Project description

Django Simple API Proxy

This is a simple tool for proxying any APIs easily on your Django server.

You can use it as middleware to make a layer for user authorization or something.

Installation

pip install django-simple-api-proxy

Quick Start

  1. Add django_simple_api_proxy to your INSTALLED_APPS in settings.py like this:
INSTALLED_APPS = [
...
'django_simple_api_proxy',
]
  1. Add APP settings to your settings.py like this:
TARGET_API_URL = 'https://httpbin.org'
PROXY_ROUTE_PATH = 'my_test_route'
PROXY_TARGET_PATH = ''
  1. Include the django_simple_api_proxy URL settings in your project urls.py like this:
from django.conf import settings
from django.urls import include
urlpatterns += [
    path(settings.PROXY_ROUTE_PATH, include('django_simple_api_proxy.urls'))
]
  1. Test on your server.
python manage.py runserver

Here's an example you success proxy an API by visit the following URLs.

And the result will be as below.

[06/Sep/2022 01:26:04] "GET /my_test_route/ HTTP/1.1" 200 314
2022-09-06 01:26:06.338 | DEBUG    | django_simple_api_proxy.views:get:73 - ----- Proxy GET
2022-09-06 01:26:06.339 | DEBUG    | django_simple_api_proxy.views:get_proxy_path:37 - URL: /get
2022-09-06 01:26:06.340 | DEBUG    | django_simple_api_proxy.views:update_payload:49 - Username: #anonymous
{
  "args": { "username": "#anonymous" },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.27.1",
    "X-Amzn-Trace-Id": "Root=1-6316360c-2308560261599de4071127ac"
  },
  "origin": "xxx.xxx.xxx.xxx",
  "url": "https://httpbin.org/get?username=%23anonymous"
}

But when you visit http://127.0.0.1:8000/my_test_route/123, you'll get error.

Cause this URL is not found on target API server.

So, this proxy server will return this for you.

{ "status": "error" }

Usage

After the quick start, you may want to change some methods with your API server like making an authorization.

You can do it with inheriting the APIProxy class.

Here is an example:

import requests

from rest_framework.authentication import SessionAuthentication
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.permissions import IsAuthenticated

from django_simple_api_proxy.views import APIProxy

class MyAPIProxy(APIProxy):
    # give custom authentication
    authentication_classes = [SessionAuthentication, JWTAuthentication]

    # give custom permission
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        """Get."""
        # your new `GET` logic here

        logger.debug("----- Proxy GET Heyyaya")
        response = {"status": "default error!!"}
        try:
            params = dict(request.GET)
            path = self.get_proxy_path(request)
            params = self.update_payload(request, params)
            middle_resp_ = self.send_request("GET", path, params=params)
            response = middle_resp_.json()
        except Exception as e:
            print('yooooo error occurs!!')
            print(e)
        return self.response(response)

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-simple-api-proxy-1.0.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_simple_api_proxy-1.0.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file django-simple-api-proxy-1.0.0.tar.gz.

File metadata

  • Download URL: django-simple-api-proxy-1.0.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for django-simple-api-proxy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0c93b09bcd7ffc46554722a8154053832589ab4be311062813094bf06984a7f8
MD5 010028fb26d48a47a524968531731cc7
BLAKE2b-256 510161fa37ce1543c1342d22a7b77ec13364ebccd9a7e08fba7712c5099d7998

See more details on using hashes here.

File details

Details for the file django_simple_api_proxy-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_simple_api_proxy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c966d90a6bd21eff7acae2a293402b9e477acd2422d3bfb5ba26e47c14ea2f0
MD5 7bc3884fd11b465df131ce07295fb71e
BLAKE2b-256 d70c43ca4c64e9bd5f72f201708dd9b8fd6672a240539b63abcb4542fb220283

See more details on using hashes here.

Supported by

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