-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Add setting for configuring optional URL prefix for /api #14939
Add setting for configuring optional URL prefix for /api #14939
Conversation
029ae9c
to
69d5841
Compare
@TheRealHaoLiu what is the deal with the swagger tests all failing? Looking at your changes and the errors in swagger, it looks to me like something is off with the URL patterns you have changed and the associated tests? |
69d5841
to
86a196d
Compare
mmm, it seems some new issues have been uncovered |
86a196d
to
15ab639
Compare
fixed the swagger sanity test failure |
90f2bbe
to
8c7c0d3
Compare
8c7c0d3
to
8484e8a
Compare
8484e8a
to
c8e9776
Compare
c8e9776
to
624c458
Compare
awx/api/urls/urls.py
Outdated
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'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a better way.
624c458
to
4918fe4
Compare
The Djangoy way to solve this would be via But there are complications. Our url building and registration happens on import. So what would we dynamically set Let's say this below.
Becomes this:
Now the middleware can find But is this any better than this hack? It does become more testable. I tried testing this hack version and it sucks to test because the urls are "built" and registered on import and mocking/changing things on import sucks. I'd say this version is more discoverable. I'd say a middleware version is more hidden BUT I think other components could re-use this code if it were middleware. Ultimately, it's a judgement call as to if it's better to do this the Django way or via this hack. I'm of the opinion that this hack is OK. I'd love someone to do it "proper" so that it is more testable, but I'm ok with this version. |
My preferred idiomatic Django way would be for an upstream urls.py file to use this setting as its prefix for importing the awx/main urls. |
awx/api/versioning.py
Outdated
@@ -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/"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm kind of not sure what is intended here, but this almost certainly does not do whatever that is. The condition virtually guarantees that the replace will be a no-op.
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']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this doing? {var}
looks like string templating, but I see no f-string or .format()
in the changeset here.
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`
4918fe4
to
cebc917
Compare
Addressed @jbradberry's review in a collab session |
awx/api/versioning.py
Outdated
@@ -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}"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is request._request.path
really necessary, or does request.path
work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yah ur right request.path works
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
* Add setting for configuring optional URL prefix for /api 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`
* Add setting for configuring optional URL prefix for /api 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`
SUMMARY
Add OPTIONAL_API_URLPATTERN_PREFIX setting
examples:
''
(empty string) API pattern will be/api
/api
AND/api/controller
ISSUE TYPE
COMPONENT NAME
AWX VERSION
ADDITIONAL INFORMATION