Skip to content

Commit

Permalink
Add setting for configuring optional URL prefix for /api
Browse files Browse the repository at this point in the history
Add OPTIONAL_API_URLPATTERN_PREFIX setting

examples:
- if set to `''` (empty string) API pattern will be `/api`
- if set to 'controller' API pattern will be `/api` AND `/api/controller`
  • Loading branch information
TheRealHaoLiu committed Mar 18, 2024
1 parent 065bd3a commit 4918fe4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
19 changes: 13 additions & 6 deletions awx/api/urls/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import absolute_import, unicode_literals
from django.urls import include, re_path
from django.conf import settings

from awx import MODE
from awx.api.generics import LoggedLoginView, LoggedLogoutView
Expand Down Expand Up @@ -159,15 +160,21 @@
re_path(r'^receptor_addresses/', include(receptor_address_urls)),
]


app_name = 'api'
urlpatterns = [
re_path(r'^$', ApiRootView.as_view(), name='api_root_view'),
re_path(r'^(?P<version>(v2))/', include(v2_urls)),
re_path(r'^login/$', LoggedLoginView.as_view(template_name='rest_framework/login.html', extra_context={'inside_login_context': True}), name='login'),
re_path(r'^logout/$', LoggedLogoutView.as_view(next_page='/api/', redirect_field_name='next'), name='logout'),
re_path(r'^o/', include(oauth2_root_urls)),
re_path(r'^(' + settings.OPTIONAL_API_URLPATTERN_PREFIX + '/)?$', ApiRootView.as_view(), name='api_root_view'),
re_path(r'^(' + settings.OPTIONAL_API_URLPATTERN_PREFIX + '/)?(?P<version>(v2))/', include(v2_urls)),
re_path(
r'^(' + settings.OPTIONAL_API_URLPATTERN_PREFIX + '/)?login/$',
LoggedLoginView.as_view(template_name='rest_framework/login.html', extra_context={'inside_login_context': True}),
name='login',
),
re_path(
r'^(' + settings.OPTIONAL_API_URLPATTERN_PREFIX + '/)?logout/$', LoggedLogoutView.as_view(next_page='/api/', redirect_field_name='next'), name='logout'
),
re_path(r'^(' + settings.OPTIONAL_API_URLPATTERN_PREFIX + '/)?o/', include(oauth2_root_urls)),
]

if MODE == 'development':
# Only include these if we are in the development environment
from awx.api.swagger import schema_view
Expand Down
4 changes: 4 additions & 0 deletions awx/api/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def drf_reverse(viewname, args=None, kwargs=None, request=None, format=None, **e
else:
url = _reverse(viewname, args, kwargs, request, format, **extra)

if settings.OPTIONAL_API_URLPATTERN_PREFIX and request:
if request._request.path.startswith(f"/api/{settings.OPTIONAL_API_URLPATTERN_PREFIX}/v2/"):
url = url.replace('/api/v2/', f"/api/{settings.OPTIONAL_API_URLPATTERN_PREFIX}/v2/")

return url


Expand Down
12 changes: 6 additions & 6 deletions awx/main/tests/docs/test_swagger_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ def test_sanity(self, release, request):
# for a reasonable number here; if this test starts failing, raise/lower the bounds
paths = JSON['paths']
assert 250 < len(paths) < 375
assert set(list(paths['/api/'].keys())) == set(['get', 'parameters'])
assert set(list(paths['/api/v2/'].keys())) == set(['get', 'parameters'])
assert set(list(sorted(paths['/api/v2/credentials/'].keys()))) == set(['get', 'post', 'parameters'])
assert set(list(sorted(paths['/api/v2/credentials/{id}/'].keys()))) == set(['delete', 'get', 'patch', 'put', 'parameters'])
assert set(list(paths['/api/v2/settings/'].keys())) == set(['get', 'parameters'])
assert set(list(paths['/api/v2/settings/{category_slug}/'].keys())) == set(['get', 'put', 'patch', 'delete', 'parameters'])
assert set(list(paths['/api/{var}'].keys())) == set(['get', 'parameters'])
assert set(list(paths['/api/{var}v2/'].keys())) == set(['get', 'parameters'])
assert set(list(sorted(paths['/api/{var}v2/credentials/'].keys()))) == set(['get', 'post', 'parameters'])
assert set(list(sorted(paths['/api/{var}v2/credentials/{id}/'].keys()))) == set(['delete', 'get', 'patch', 'put', 'parameters'])
assert set(list(paths['/api/{var}v2/settings/'].keys())) == set(['get', 'parameters'])
assert set(list(paths['/api/{var}v2/settings/{category_slug}/'].keys())) == set(['get', 'put', 'patch', 'delete', 'parameters'])

@pytest.mark.parametrize(
'path',
Expand Down
5 changes: 5 additions & 0 deletions awx/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,3 +1126,8 @@

settings_file = os.path.join(os.path.dirname(dynamic_config.__file__), 'dynamic_settings.py')
include(settings_file)

# Add a postfix to the API URL patterns
# example if set to '' API pattern will be /api
# example if set to 'controller' API pattern will be /api AND /api/controller
OPTIONAL_API_URLPATTERN_PREFIX = ''

0 comments on commit 4918fe4

Please sign in to comment.