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
Check it in Pypi.
Quick Start
- Add
django_simple_api_proxyto yourINSTALLED_APPSinsettings.pylike this:
INSTALLED_APPS = [
...
'django_simple_api_proxy',
]
- Add APP settings to your
settings.pylike this:
TARGET_API_URL = 'https://httpbin.org'
PROXY_ROUTE_PATH = 'my_test_route'
PROXY_TARGET_PATH = 'get'
- Include the
django_simple_api_proxyURL settings in your projecturls.pylike this:
from django.conf import settings
from django.urls import include
urlpatterns += [
path(settings.PROXY_ROUTE_PATH, include('django_simple_api_proxy.urls'))
]
- 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)
More
There is an example project you can check in ./example.
Metadata
Release files for django-simple-api-proxy 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-simple-api-proxy-1.0.2.tar.gz | 9.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_simple_api_proxy-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.8 kB
Release files / django-simple-api-proxy-1.0.2.tar.gz
| Download URL | django-simple-api-proxy-1.0.2.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7c757cf1731f6284f30f132398113faa247b9297b7f9e6ff5e34fcc8b90f9e34
|
|
BLAKE2b-256 checksum How to use checksums |
a5d092552c467bd089ec8ccc4dc779295e3a45c40bb6f677f2e41ca65a048c2d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.1
|
Release files / django_simple_api_proxy-1.0.2-py3-none-any.whl
| Download URL | django_simple_api_proxy-1.0.2-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ec9e935c2bc108dfb20d9eef4b713ae688be704725d60d43142dba4d46aa2cf
|
|
BLAKE2b-256 checksum How to use checksums |
44c24e17ed640e98f6a48e7a3cc10777e0bc09c31e65e603d32754990161d5a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.1
|