Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct requests to api.perma.cc that don't specify a route #3174

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions perma_web/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
from django.conf import settings
from django.conf.urls import url, include
from django.http import HttpResponse
from django.views.generic import RedirectView
from rest_framework.routers import APIRootView

from perma.urls import guid_pattern

from . import views


# helpers
class RedirectWithRootDomain(RedirectView):
def get_redirect_url(self, *args, **kwargs):
url = super(RedirectWithRootDomain, self).get_redirect_url(*args, **kwargs)
return self.request.scheme + '://' + settings.HOST + url

# list views that should appear in the HTML version of the API root
root_view = APIRootView.as_view(api_root_dict={
'folders': 'folders',
Expand Down Expand Up @@ -91,7 +83,7 @@ def get_redirect_url(self, *args, **kwargs):
])),

# redirect plain api.perma.cc/ and perma.cc/api/ to docs:
url(r'^$', RedirectWithRootDomain.as_view(url='/docs/developer'))
url(r'^$', views.DeveloperDocsView.as_view())
]

### error handlers ###
Expand Down
12 changes: 10 additions & 2 deletions perma_web/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
url_is_invalid_unicode
from .serializers import FolderSerializer, CaptureJobSerializer, LinkSerializer, AuthenticatedLinkSerializer, \
LinkUserSerializer, OrganizationSerializer, LinkBatchSerializer, DetailedLinkBatchSerializer

from django.conf import settings
from django.urls import reverse

### BASE VIEW ###

Expand Down Expand Up @@ -157,6 +158,14 @@ def get(self, request, pk, format=None):
return self.simple_get(request, pk)


### DEVELOPER DOCS VIEWS ###
class DeveloperDocsView(APIView):
def get(self, request, format=None):
""" reverse to Developer Docs to fetch correct url (view) named as 'dev_docs' """
absolute_url_to_redirect_to = f"{ self.request.scheme }://{ settings.HOST }{ reverse('dev_docs', urlconf='perma.urls') }"
""" Redirect to Dev Docs """
return HttpResponseRedirect(absolute_url_to_redirect_to)

### FOLDER VIEWS ###

# /folders
Expand Down Expand Up @@ -504,7 +513,6 @@ def post(self, request, format=None):
run_next_capture.delay()

return Response(serializer.data, status=status.HTTP_201_CREATED)

raise_invalid_capture_job(capture_job, serializer.errors)


Expand Down