|
| 1 | +# Django Proxy API |
| 2 | + |
| 3 | +This is a tool for proxying any APIs on Django server. |
| 4 | + |
| 5 | +You can use it as middleware to make a layer for user authorization or something. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install djangoapiproxy |
| 11 | +``` |
| 12 | + |
| 13 | +## Quick Start |
| 14 | + |
| 15 | +1. Add `django_api_proxy` to your `INSTALLED_APPS` in `settings.py` like this: |
| 16 | + |
| 17 | +```py |
| 18 | +INSTALLED_APPS = [ |
| 19 | +... |
| 20 | +'django_api_proxy', |
| 21 | +] |
| 22 | +``` |
| 23 | + |
| 24 | +2. Add APP settings to your `settings.py` like this: |
| 25 | + |
| 26 | +```py |
| 27 | +TARGET_API_URL = 'https://httpbin.org' |
| 28 | +PROXY_ROUTE_PATH = 'my_test_route' |
| 29 | +PROXY_TARGET_PATH = '' |
| 30 | +``` |
| 31 | + |
| 32 | +3. Include the `django_api_proxy` URL settings in your project `urls.py` like this: |
| 33 | + |
| 34 | +```py |
| 35 | +from django.conf import settings |
| 36 | +from django.urls import include |
| 37 | +urlpatterns += [ |
| 38 | + path(settings.PROXY_ROUTE_PATH, include('django_api_proxy.urls')) |
| 39 | +] |
| 40 | +``` |
| 41 | + |
| 42 | +4. Test on your server. |
| 43 | + |
| 44 | +```bash |
| 45 | +python manage.py runserver |
| 46 | +``` |
| 47 | + |
| 48 | +Here's an example you success proxy an API by visit the following URLs. |
| 49 | + |
| 50 | +- http://127.0.0.1:8000/my_test_route/ |
| 51 | +- http://127.0.0.1:8000/my_test_route |
| 52 | + |
| 53 | +And the result will be as below. |
| 54 | + |
| 55 | +```log |
| 56 | +[06/Sep/2022 01:26:04] "GET /my_test_route/ HTTP/1.1" 200 314 |
| 57 | +2022-09-06 01:26:06.338 | DEBUG | django_api_proxy.views:get:73 - ----- Proxy GET |
| 58 | +2022-09-06 01:26:06.339 | DEBUG | django_api_proxy.views:get_proxy_path:37 - URL: /get |
| 59 | +2022-09-06 01:26:06.340 | DEBUG | django_api_proxy.views:update_payload:49 - Username: #anonymous |
| 60 | +``` |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | + "args": { "username": "#anonymous" }, |
| 65 | + "headers": { |
| 66 | + "Accept": "*/*", |
| 67 | + "Accept-Encoding": "gzip, deflate, br", |
| 68 | + "Host": "httpbin.org", |
| 69 | + "User-Agent": "python-requests/2.27.1", |
| 70 | + "X-Amzn-Trace-Id": "Root=1-6316360c-2308560261599de4071127ac" |
| 71 | + }, |
| 72 | + "origin": "xxx.xxx.xxx.xxx", |
| 73 | + "url": "https://httpbin.org/get?username=%23anonymous" |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +But when you visit `http://127.0.0.1:8000/my_test_route/123`, you'll get error. |
| 78 | + |
| 79 | +Cause this URL is not found on target API server. |
| 80 | + |
| 81 | +So, this proxy server will return this for you. |
| 82 | + |
| 83 | +```json |
| 84 | +{ "status": "error" } |
| 85 | +``` |
| 86 | + |
| 87 | +## Usage |
| 88 | + |
| 89 | +After the quick start, you may want to change some methods with your API server like making an authorization. |
| 90 | + |
| 91 | +You can do it with inheriting the `APIProxy` class. |
| 92 | + |
| 93 | +Here is an example: |
| 94 | + |
| 95 | +```py |
| 96 | + |
| 97 | +import requests |
| 98 | + |
| 99 | +from rest_framework.authentication import SessionAuthentication |
| 100 | +from rest_framework_simplejwt.authentication import JWTAuthentication |
| 101 | +from rest_framework.permissions import IsAuthenticated |
| 102 | + |
| 103 | +from django_api_proxy.views import APIProxy |
| 104 | + |
| 105 | +class MyAPIProxy(APIProxy): |
| 106 | + # give custom authentication |
| 107 | + authentication_classes = [SessionAuthentication, JWTAuthentication] |
| 108 | + |
| 109 | + # give custom permission |
| 110 | + permission_classes = [IsAuthenticated] |
| 111 | + |
| 112 | + def get(self, request, *args, **kwargs): |
| 113 | + """Get.""" |
| 114 | + # your new `GET` logic here |
| 115 | + |
| 116 | + logger.debug("----- Proxy GET Heyyaya") |
| 117 | + response = {"status": "default error!!"} |
| 118 | + try: |
| 119 | + params = dict(request.GET) |
| 120 | + path = self.get_proxy_path(request) |
| 121 | + params = self.update_payload(request, params) |
| 122 | + middle_resp_ = self.send_request("GET", path, params=params) |
| 123 | + response = middle_resp_.json() |
| 124 | + except Exception as e: |
| 125 | + print('yooooo error occurs!!') |
| 126 | + print(e) |
| 127 | + return self.response(response) |
| 128 | + |
| 129 | +``` |
0 commit comments