├── .gitignore ├── apiv1 ├── __init__.py ├── admin.py ├── apps.py ├── migrations │ └── __init__.py ├── models.py ├── serializers.py ├── tests.py └── views.py ├── django_jsonapi ├── __init__.py ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py └── manage.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /apiv1/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-free89/simple_django_rest_api/adc04fc56ac4c9853172d4f95fa25010220ae8e9/apiv1/__init__.py -------------------------------------------------------------------------------- /apiv1/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /apiv1/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class Apiv1Config(AppConfig): 5 | name = 'apiv1' 6 | -------------------------------------------------------------------------------- /apiv1/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-free89/simple_django_rest_api/adc04fc56ac4c9853172d4f95fa25010220ae8e9/apiv1/migrations/__init__.py -------------------------------------------------------------------------------- /apiv1/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /apiv1/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | 3 | class DataSerializer(serializers.Serializer): 4 | # name = serializers.CharField(max_length=200) 5 | # job = serializers.CharField(max_length=200) 6 | 7 | class Meta: 8 | fields = '__all__' -------------------------------------------------------------------------------- /apiv1/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /apiv1/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from rest_framework.response import Response 3 | from rest_framework import status, views 4 | from .serializers import DataSerializer 5 | import requests 6 | import json 7 | 8 | dummy_url = "https://dummy.restapiexample.com/api/v1/employees" 9 | service2_url = "https://api.mocki.io/v1/b043df5a" 10 | order_url = "https://dummy.restapiexample.com/api/v1/create" 11 | 12 | class data(views.APIView): 13 | 14 | # Get all data 15 | def get(self, request): 16 | print("backend processing ...") 17 | headers = { 18 | 'Content-Type': 'application/json', 19 | 'Accept': 'text/plain', 20 | 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0' 21 | } 22 | result = requests.get(dummy_url, headers=headers) 23 | print("backend processing 1 ...", result.text) 24 | 25 | results = json.loads(result.text) 26 | 27 | # results = DataSerializer(result.content, many=True).data 28 | print("backend processing 2 ...", results) 29 | 30 | return Response(results) 31 | 32 | class Orders(views.APIView): 33 | 34 | # Get all data 35 | def post(self, request): 36 | print("backend processing ...") 37 | data = { 38 | "name": "test", 39 | "salary": "123", 40 | "age": "23" 41 | } 42 | headers = { 43 | 'Content-Type': 'application/json', 44 | 'Accept': 'text/plain', 45 | 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0' 46 | } 47 | response = requests.request("POST", order_url, headers=headers, data=data) 48 | print("backend processing 1 ...", response.text) 49 | 50 | results = json.loads(response.text) 51 | 52 | # # # results = DataSerializer(result.content, many=True).data 53 | # print("backend processing 2 ...", results) 54 | 55 | return Response(results) 56 | 57 | class Service2(views.APIView): 58 | 59 | # Get all data 60 | def get(self, request): 61 | print("backend processing ...") 62 | headers = { 63 | 'Content-Type': 'application/json', 64 | 'Accept': 'text/plain', 65 | 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0' 66 | } 67 | result = requests.get(service2_url, headers=headers) 68 | print("backend processing 1 ...", result.text) 69 | 70 | results = json.loads(result.text) 71 | 72 | # results = DataSerializer(result.content, many=True).data 73 | print("backend processing 2 ...", results) 74 | 75 | return Response(results) -------------------------------------------------------------------------------- /django_jsonapi/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-free89/simple_django_rest_api/adc04fc56ac4c9853172d4f95fa25010220ae8e9/django_jsonapi/__init__.py -------------------------------------------------------------------------------- /django_jsonapi/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for django_jsonapi project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_jsonapi.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /django_jsonapi/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for django_jsonapi project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.1.7. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 's4c@7odzj^!w6c5tvfkmi#acldr(gp%#0z+k2#c2)p$2ug91#v' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'apiv1.apps.Apiv1Config', 41 | 'rest_framework' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'django_jsonapi.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'django_jsonapi.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/3.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/3.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_L10N = True 115 | 116 | USE_TZ = True 117 | 118 | 119 | # Static files (CSS, JavaScript, Images) 120 | # https://docs.djangoproject.com/en/3.1/howto/static-files/ 121 | 122 | STATIC_URL = '/static/' 123 | 124 | 125 | REST_FRAMEWORK = { 126 | 'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler', 127 | 'DEFAULT_PAGINATION_CLASS': 128 | 'rest_framework_json_api.pagination.JsonApiPageNumberPagination', 129 | 'DEFAULT_PARSER_CLASSES': ( 130 | 'rest_framework_json_api.parsers.JSONParser', 131 | 'rest_framework.parsers.FormParser', 132 | 'rest_framework.parsers.MultiPartParser' 133 | ), 134 | 'DEFAULT_RENDERER_CLASSES': ( 135 | 'rest_framework_json_api.renderers.JSONRenderer', 136 | # If you're performance testing, you will want to use the browseable API 137 | # without forms, as the forms can generate their own queries. 138 | # If performance testing, enable: 139 | # 'example.utils.BrowsableAPIRendererWithoutForms', 140 | # Otherwise, to play around with the browseable API, enable: 141 | 'rest_framework.renderers.BrowsableAPIRenderer' 142 | ), 143 | 'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata', 144 | 'DEFAULT_FILTER_BACKENDS': ( 145 | 'rest_framework_json_api.filters.QueryParameterValidationFilter', 146 | 'rest_framework_json_api.filters.OrderingFilter', 147 | 'rest_framework_json_api.django_filters.DjangoFilterBackend', 148 | 'rest_framework.filters.SearchFilter', 149 | ), 150 | 'SEARCH_PARAM': 'filter[search]', 151 | 'TEST_REQUEST_RENDERER_CLASSES': ( 152 | 'rest_framework_json_api.renderers.JSONRenderer', 153 | ), 154 | 'TEST_REQUEST_DEFAULT_FORMAT': 'vnd.api+json' 155 | } -------------------------------------------------------------------------------- /django_jsonapi/urls.py: -------------------------------------------------------------------------------- 1 | """django_jsonapi URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from django.urls import include, path, re_path 19 | from apiv1 import views as apiv1_views 20 | 21 | urlpatterns = [ 22 | path('admin/', admin.site.urls), 23 | path('api/Warehouse/', apiv1_views.data.as_view()), 24 | path('api/Orders/', apiv1_views.Orders.as_view()), 25 | path('api/Service2/', apiv1_views.Service2.as_view()) 26 | ] 27 | -------------------------------------------------------------------------------- /django_jsonapi/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for django_jsonapi project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_jsonapi.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_jsonapi.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | --------------------------------------------------------------------------------