# Django Simple API Proxy
[![Test](https://github.com/NatLee/Django-Simple-API-Proxy/actions/workflows/test.yml/badge.svg)](https://github.com/NatLee/Django-Simple-API-Proxy/actions/workflows/test.yml)[![Release](https://github.com/NatLee/Django-Simple-API-Proxy/actions/workflows/release.yml/badge.svg)](https://github.com/NatLee/Django-Simple-API-Proxy/actions/workflows/release.yml)
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
```bash
pip install django-simple-api-proxy
```
Check it in [Pypi](https://pypi.org/project/django-simple-api-proxy/).
## Quick Start
1. Add `django_simple_api_proxy` to your `INSTALLED_APPS` in `settings.py` like this:
```py
INSTALLED_APPS = [
...
'django_simple_api_proxy',
]
```
2. Add APP settings to your `settings.py` like this:
```py
TARGET_API_URL = 'https://httpbin.org'
PROXY_ROUTE_PATH = 'my_test_route'
PROXY_TARGET_PATH = 'get'
```
3. Include the `django_simple_api_proxy` URL settings in your project `urls.py` like this:
```py
from django.conf import settings
from django.urls import include
urlpatterns += [
path(settings.PROXY_ROUTE_PATH, include('django_simple_api_proxy.urls'))
]
```
4. Test on your server.
```bash
python manage.py runserver
```
Here's an example you success proxy an API by visit the following URLs.
- http://127.0.0.1:8000/my_test_route/
- http://127.0.0.1:8000/my_test_route
And the result will be as below.
```log
[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
```
```json
{
"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.
```json
{ "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:
```py
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](https://github.com/NatLee/Django-Simple-API-Proxy/tree/main/example/api_proxy_example).
Raw data
{
"_id": null,
"home_page": "https://github.com/natlee/Django-Simple-API-Proxy",
"name": "django-simple-api-proxy",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "django,api,proxy,proxy pass",
"author": "Nat Lee",
"author_email": "natlee.work@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/a5/d0/92552c467bd089ec8ccc4dc779295e3a45c40bb6f677f2e41ca65a048c2d/django-simple-api-proxy-1.0.2.tar.gz",
"platform": null,
"description": "# Django Simple API Proxy\n\n[![Test](https://github.com/NatLee/Django-Simple-API-Proxy/actions/workflows/test.yml/badge.svg)](https://github.com/NatLee/Django-Simple-API-Proxy/actions/workflows/test.yml)[![Release](https://github.com/NatLee/Django-Simple-API-Proxy/actions/workflows/release.yml/badge.svg)](https://github.com/NatLee/Django-Simple-API-Proxy/actions/workflows/release.yml)\n\nThis is a simple tool for proxying any APIs easily on your Django server.\n\nYou can use it as middleware to make a layer for user authorization or something.\n\n## Installation\n\n```bash\npip install django-simple-api-proxy\n```\n\nCheck it in [Pypi](https://pypi.org/project/django-simple-api-proxy/).\n\n## Quick Start\n\n1. Add `django_simple_api_proxy` to your `INSTALLED_APPS` in `settings.py` like this:\n\n```py\nINSTALLED_APPS = [\n...\n'django_simple_api_proxy',\n]\n```\n\n2. Add APP settings to your `settings.py` like this:\n\n```py\nTARGET_API_URL = 'https://httpbin.org'\nPROXY_ROUTE_PATH = 'my_test_route'\nPROXY_TARGET_PATH = 'get'\n```\n\n3. Include the `django_simple_api_proxy` URL settings in your project `urls.py` like this:\n\n```py\nfrom django.conf import settings\nfrom django.urls import include\nurlpatterns += [\n path(settings.PROXY_ROUTE_PATH, include('django_simple_api_proxy.urls'))\n]\n```\n\n4. Test on your server.\n\n```bash\npython manage.py runserver\n```\n\nHere's an example you success proxy an API by visit the following URLs.\n\n- http://127.0.0.1:8000/my_test_route/\n- http://127.0.0.1:8000/my_test_route\n\nAnd the result will be as below.\n\n```log\n[06/Sep/2022 01:26:04] \"GET /my_test_route/ HTTP/1.1\" 200 314\n2022-09-06 01:26:06.338 | DEBUG | django_simple_api_proxy.views:get:73 - ----- Proxy GET\n2022-09-06 01:26:06.339 | DEBUG | django_simple_api_proxy.views:get_proxy_path:37 - URL: /get\n2022-09-06 01:26:06.340 | DEBUG | django_simple_api_proxy.views:update_payload:49 - Username: #anonymous\n```\n\n```json\n{\n \"args\": { \"username\": \"#anonymous\" },\n \"headers\": {\n \"Accept\": \"*/*\",\n \"Accept-Encoding\": \"gzip, deflate, br\",\n \"Host\": \"httpbin.org\",\n \"User-Agent\": \"python-requests/2.27.1\",\n \"X-Amzn-Trace-Id\": \"Root=1-6316360c-2308560261599de4071127ac\"\n },\n \"origin\": \"xxx.xxx.xxx.xxx\",\n \"url\": \"https://httpbin.org/get?username=%23anonymous\"\n}\n```\n\nBut when you visit `http://127.0.0.1:8000/my_test_route/123`, you'll get error.\n\nCause this URL is not found on target API server.\n\nSo, this proxy server will return this for you.\n\n```json\n{ \"status\": \"error\" }\n```\n\n## Usage\n\nAfter the quick start, you may want to change some methods with your API server like making an authorization.\n\nYou can do it with inheriting the `APIProxy` class.\n\nHere is an example:\n\n```py\n\nimport requests\n\nfrom rest_framework.authentication import SessionAuthentication\nfrom rest_framework_simplejwt.authentication import JWTAuthentication\nfrom rest_framework.permissions import IsAuthenticated\n\nfrom django_simple_api_proxy.views import APIProxy\n\nclass MyAPIProxy(APIProxy):\n # give custom authentication\n authentication_classes = [SessionAuthentication, JWTAuthentication]\n\n # give custom permission\n permission_classes = [IsAuthenticated]\n\n def get(self, request, *args, **kwargs):\n \"\"\"Get.\"\"\"\n # your new `GET` logic here\n\n logger.debug(\"----- Proxy GET Heyyaya\")\n response = {\"status\": \"default error!!\"}\n try:\n params = dict(request.GET)\n path = self.get_proxy_path(request)\n params = self.update_payload(request, params)\n middle_resp_ = self.send_request(\"GET\", path, params=params)\n response = middle_resp_.json()\n except Exception as e:\n print('yooooo error occurs!!')\n print(e)\n return self.response(response)\n\n```\n\n## More\n\nThere is an example project you can check in [./example](https://github.com/NatLee/Django-Simple-API-Proxy/tree/main/example/api_proxy_example).\n",
"bugtrack_url": null,
"license": "",
"summary": "Easy to proxy any APIs you want.",
"version": "1.0.2",
"split_keywords": [
"django",
"api",
"proxy",
"proxy pass"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "67e0957813f9eaf2eeb5f9045e6bfdbe",
"sha256": "2ec9e935c2bc108dfb20d9eef4b713ae688be704725d60d43142dba4d46aa2cf"
},
"downloads": -1,
"filename": "django_simple_api_proxy-1.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "67e0957813f9eaf2eeb5f9045e6bfdbe",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 6967,
"upload_time": "2022-12-14T15:49:59",
"upload_time_iso_8601": "2022-12-14T15:49:59.676290Z",
"url": "https://files.pythonhosted.org/packages/44/c2/4e17ed640e98f6a48e7a3cc10777e0bc09c31e65e603d32754990161d5a0/django_simple_api_proxy-1.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "84e5a387f5e1463d0e71edae25f39306",
"sha256": "7c757cf1731f6284f30f132398113faa247b9297b7f9e6ff5e34fcc8b90f9e34"
},
"downloads": -1,
"filename": "django-simple-api-proxy-1.0.2.tar.gz",
"has_sig": false,
"md5_digest": "84e5a387f5e1463d0e71edae25f39306",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 9840,
"upload_time": "2022-12-14T15:50:01",
"upload_time_iso_8601": "2022-12-14T15:50:01.827813Z",
"url": "https://files.pythonhosted.org/packages/a5/d0/92552c467bd089ec8ccc4dc779295e3a45c40bb6f677f2e41ca65a048c2d/django-simple-api-proxy-1.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-14 15:50:01",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "natlee",
"github_project": "Django-Simple-API-Proxy",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "django-simple-api-proxy"
}