Quickly build API services to expose functionality in Python. kwikapi.django
was built by using the functionality of KwikAPI and Django web server.
$ pip3 install kwikapi[django]
(Ref: https://docs.djangoproject.com/en/1.11/intro/tutorial01/)
$ django-admin startproject django_kwikapi
$ python3 manage.py startapp polls
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
/django_kwikapi/django_kwikapi/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('polls.urls'))
]
/django_kwikapi/polls/urls.py
from django.conf.urls import url, include
from . import views
from kwikapi.django import RequestHandler
urlpatterns = [
url(r'api/', RequestHandler(views.api).handle_request),
]
/django_kwikapi/polls/views.py
from django.http import HttpResponse
from kwikapi import API
from logging import Logger
class BaseCalc():
def add(self, a: int, b: int) -> int:
return a + b
def subtract(self, a: int, b: int) -> int:
return a - b
class StandardCalc():
def multiply(self, a: int, b: int) -> int:
return a * b
def divide(self, a: int, b: int) -> float:
return a / b
api = API(log=Logger, default_version='v1')
api.register(BaseCalc(), 'v1')
api.register(StandardCalc(), "v2")
$ python3 manage.py makemigrations
$ python3 manage.py migrate
$ python3 manage.py runserver 8888
$ curl "http://localhost:8888/api/v1/add?a=10&b=10"
To know how to use all features, please refer KwikAPI documentation https://github.com/deep-compute/kwikapi/blob/master/README.md