From 4e59812b6b3f9cde1558f1ac75cd35b7218017fa Mon Sep 17 00:00:00 2001 From: Tim Glaser Date: Thu, 13 Feb 2020 22:50:44 -0800 Subject: [PATCH] Add API documentation --- posthog/settings.py | 5 ++++- posthog/urls.py | 22 ++++++++++++++++++++++ preview.Dockerfile | 2 +- production.Dockerfile | 2 +- requirements.txt | 1 + 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/posthog/settings.py b/posthog/settings.py index 4561d980008..d3752197bf0 100644 --- a/posthog/settings.py +++ b/posthog/settings.py @@ -164,4 +164,7 @@ REST_FRAMEWORK = { } # You can pass a comma deliminated list of domains with which users can sign up to this service -RESTRICT_SIGNUPS = os.environ.get('RESTRICT_SIGNUPS', False) \ No newline at end of file +RESTRICT_SIGNUPS = os.environ.get('RESTRICT_SIGNUPS', False) + +if os.environ.get('INCLUDE_DOCS', False): + INSTALLED_APPS.push('drf_yasg') \ No newline at end of file diff --git a/posthog/urls.py b/posthog/urls.py index 3179917d65a..65af97ce135 100644 --- a/posthog/urls.py +++ b/posthog/urls.py @@ -12,6 +12,23 @@ from .models import Team, User import json import posthoganalytics +from rest_framework import permissions +from drf_yasg.views import get_schema_view +from drf_yasg import openapi + +schema_view = get_schema_view( + openapi.Info( + title="PostHog API", + default_version='v1', + description="PostHog's API allows you to do anything you can do in the PostHog frontend.", + contact=openapi.Contact(email="hey@posthog.com"), + license=openapi.License(name="MIT License"), + ), + public=True, + permission_classes=(permissions.AllowAny,), +) + + def render_template(template_name: str, request, context=None) -> HttpResponse: template = get_template(template_name) html = template.render(context, request=request) @@ -101,6 +118,11 @@ urlpatterns = [ path('login', login_view, name='login'), path('signup', signup_view, name='signup'), path('setup_admin', setup_admin, name='setup_admin'), + re_path(r'^swagger(?P\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'), + re_path(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), + re_path(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), + + # react frontend re_path(r'^.*', decorators.login_required(home)), diff --git a/preview.Dockerfile b/preview.Dockerfile index 284fcbec2bd..df35ec090b0 100644 --- a/preview.Dockerfile +++ b/preview.Dockerfile @@ -25,7 +25,7 @@ USER root COPY requirements.txt /code/ # install dependencies but ignore any we don't need for dev environment -RUN pip install $(grep -ivE "psycopg2|ipdb|mypy|ipython|ipdb|pip|djangorestframework-stubs|django-stubs|ipython-genutils|mypy-extensions|Pygments|typed-ast|jedi" requirements.txt) --no-cache-dir --compile\ +RUN pip install $(grep -ivE "drf-yasg|spsycopg2|ipdb|mypy|ipython|ipdb|pip|djangorestframework-stubs|django-stubs|ipython-genutils|mypy-extensions|Pygments|typed-ast|jedi" requirements.txt) --no-cache-dir --compile\ && pip install psycopg2-binary --no-cache-dir --compile\ && pip uninstall ipython-genutils pip -y \ && rm -rf /usr/local/lib/python3.8/site-packages/numpy/core/tests \ diff --git a/production.Dockerfile b/production.Dockerfile index 8213404359e..10b909c37c6 100644 --- a/production.Dockerfile +++ b/production.Dockerfile @@ -5,7 +5,7 @@ WORKDIR /code COPY requirements.txt /code/ # install dependencies but ignore any we don't need for dev environment -RUN pip install $(grep -ivE "psycopg2|ipdb|mypy|ipython|ipdb|pip|djangorestframework-stubs|django-stubs|ipython-genutils|mypy-extensions|Pygments|typed-ast|jedi" requirements.txt) --no-cache-dir --compile\ +RUN pip install $(grep -ivE "drf-yasg|psycopg2|ipdb|mypy|ipython|ipdb|pip|djangorestframework-stubs|django-stubs|ipython-genutils|mypy-extensions|Pygments|typed-ast|jedi" requirements.txt) --no-cache-dir --compile\ && pip install psycopg2-binary --no-cache-dir --compile\ && pip uninstall ipython-genutils pip -y COPY frontend/ /code/frontend diff --git a/requirements.txt b/requirements.txt index f31a4dfb0a5..83ac2f6f61c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,7 @@ django-loginas==0.3.8 django-stubs==1.4.0 djangorestframework==3.11.0 djangorestframework-stubs==1.1.0 +drf-yasg==1.17.0 freezegun==0.3.14 gunicorn==20.0.4 idna==2.8