Package: django-apistar
This project is aDjangoApp that switches between Django andAPI
Star’s routes and views. That way, we have API Star as the API
frontend, while leaving Django’s toolset available for devs to
work with.Both API Star’s docs and Django Admin work as intended.
You can see a Django + API Star implementation example at thedjango_apistar_exampleor test it onheroku.Suppports:django>=1.8apistar>=0.5.40python==3.6Installationpipinstalldjango_apistarAfter installing, we need to adddjango_apistarto yourINSTALLED_APPSin your project’ssettings.py:INSTALLED_APPS=(...'django_apistar','your_api_star_app',...)Then, we need to set up three settings: a base API Star route module (APISTAR_ROUTE_CONF), API Star’s own settings and to change the default WSGI application:APISTAR_SETTINGS={'ALLOWED_DJANGO_ROUTES':('/admin/','/static/'),}APISTAR_ROUTE_CONF='your_api_star_app.routes'# remember that the following setting already exists in a default django settings file!WSGI_APPLICATION='django_apistar.wsgi.application'TheALLOWED_DJANGO_ROUTESkey describes which routes you want API Star to ignore. Only'/static/'is required, since we want Django to keep managing static files for us.Now, if you want to run the dev server, runpython manage.py runserverand hack away!AuthenticationWe support Basic and Token/Bearer authentication.To use them, configure yourAPISTAR_SETTINGSas you would configure your API Star app:fromdjango_apistar.authentication.componentsimportDjangoBasicAuthentication,DjangoTokenAuthentication...APISTAR_SETTINGS['COMPONENTS']=[DjangoBasicAuthentication(),DjangoTokenAuthentication()],If you want to use authentication by token, you need to add thedjango_apistar.authenticationto yourINSTALLED_APPS, then migrate your database.INSTALLED_APPS=(...'django_apistar','django_apistar.authentication','your_api_star_app',...)Token authentication viewsWe provide two helper views for token authentication. To set them up, add the routes into your rootroutes.pyfile:fromdjango_apistar.authenticationimportroutesroutes=[...,Include('/auth',routes),]The views will be added to your/docs/, as usual.How it worksThis Django app contains a custom WSGI application that smartly changes between API Star’s and Django’s response handlers. By default, all requests will be responded by API Star, unless theALLOWED_DJANGO_ROUTESsettings key contains that route.This way, we are able to bypass Django completely when responding API requests, while keeping Django ready to respond to more complicated requests, like Django Admin and complex template/form views.Another big advantage is that this app enables both Django AdminandAPI Star automatic API docs.Implementing viewsThere is no need to think about corner cases when writing views. We only need to keep in mind that we won’t be able to use thedjango_ormbackend baked into API Star, so we must access models directly to deal with CRUD operations.For example, let’s create a view that persists aProduct:fromcoreimportschemasfromcoreimportmodelsdefcreate_product(product:schemas.Product)->schemas.Product:db_product=models.Product(**product)db_product.save()returnhttp.JSONResponse(schemas.Product(db_product.__dict__),status_code=201)As intended, all the data validation is at the schemas, and everything is handled gracefully by API Star.Implementing testsTo test your API Star views, we provide a hybridTestClientthat is API Star aware and a custom TestCase, leveraging Django’s ownTestCaseby including thereverse_urlmethod from API Star’s router:fromdjango_apistar.testimportTestCase#  our custom TestCasefrommodel_mommyimportmommyfromcoreimportmodels,schemasclassTestListProducts(TestCase):deftest_list_products(self):'''
        The reverse_url method behaves exactly like Django's reverse,
        but uses the view's defined name as namespace.
        The builtin client is based on the API Star Test Client,
        so it's preferred to use this test case only to test API Star's views.
        '''url=self.reverse_url('list_products')db_product=mommy.make(models.Product,rating=5,size='large')response=self.client.get(url)content=response.json()expected_product=schemas.Product(db_product.__dict__)self.assertEqual(1,len(content))self.assertEqual(expected_product,content[0])PerformanceSince we capture the request at the WSGI level, you should expect no drops in performance whatsoever.I’ve made a few (and completely arbitrary) benchmarks. I’ve used Siege and set up two views, one Django view, one API Star view, both only responding a json response with{"message": "Hello,World!"}. These were all run in my computer, so don’t expect true results - this is only for you to have an idea.apistardjango2django2-no middlewaresdjango_apistartransactions1368868401050713899transactions/sec1482.99716.231085.431440.31longest transaction0.08 sec3.063.240.08ContributingThere are still a lot of ways we can improve and add more features to this app. If you find any bugs or have significant suggestions, just open an issue or contact me atluciano@ratamero.com. Pull requests will be received with all care and attention as well :)Changelog0.5.40__0updates API Star version and code references;refactor classes into components;adds sample event hook for authentication.0.3.9__0starts using matching versions with API Star to simplify things (0.3.9__x will always work and depend on apistar==0.3.9);locks maximum API Star version to 0.3.9.0.3.2adds authentication app;adds views, models, schemas and authenticators for token authentication.0.3.1fixes defaultDJANGO_SETTINGS_MODULE;sets up Django before starting the WSGI application, enabling use with Heroku.0.3.0removes the middleware implementation in favor of a custom WSGI app;removes templates folder andapps.py, since they won’t be necessary anymore;adds custom TestClient and TestCase to thetestsmodule;improves performance by ~100% by bypassing Django when answering API Star’s requests.0.2.3coupled API Star to Django via middlewares;hijacks Django’s WSGI process to respond using API Star’s views.