Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Added the ability to use custom renderers for the JWT endpoints. #121

Merged
merged 1 commit into from
May 30, 2015

Conversation

Jwpe
Copy link
Contributor

@Jwpe Jwpe commented May 19, 2015

Ran into a use-case today where I wanted to modify the format of the error response from a failed login attempt. I've already written a DRF custom renderer to neatly wrap my responses and errors, and I thought it would be cool if I could apply this renderer to the JWT endpoints as well.

As far as I can tell it's an extremely simple change! Would definitely appreciate thoughts and feedback though.

@Jwpe
Copy link
Contributor Author

Jwpe commented May 21, 2015

@jpadilla does this seem like something you'd want to add?

@jpadilla
Copy link
Owner

@Jwpe wouldn't just setting DEFAULT_RENDERER_CLASSES work? You can also do something like:

urlpatterns = [
    url(r'^api-token-auth/', ObtainJSONWebToken.as_view(renderer_classes=[CustomRenderer]),
]

@Jwpe
Copy link
Contributor Author

Jwpe commented May 22, 2015

@jpadilla the first suggestion wouldn't work, unfortunately, because the render_classes on the base view is hard-coded as rest_framework.renderers.JSONRenderer. It could be made to work if that was changed just to use DRF's DEFAULT_RENDERER_CLASSES - might be preferable to a specific setting just for this.

The second way does work, but in my mind it's less elegant than permitting a global setting. It requires importing the view and the custom renderer into your urls.py.

Maybe a good middle ground would just be changing the base class to have:

from rest_framework.settings import api_settings as drf_api_settings
...

class JSONWebTokenAPIView(APIView):
...
renderer_classes =  drf_api_settings.DEFAULT_RENDERER_CLASSES

@jpadilla
Copy link
Owner

@Jwpe I'm thinking parser_classes and renderer_classes are better off removed. It'll just default to whatever you set on your DRF settings. Thoughts?

@Jwpe
Copy link
Contributor Author

Jwpe commented May 28, 2015

@jpadilla that totally works. It would be a lot simpler. If you like I can change the PR to reflect that?

@jpadilla
Copy link
Owner

@Jwpe that'd be great, yeah!

@Jwpe Jwpe force-pushed the custom-renderers branch from 62b92eb to 0357c36 Compare May 29, 2015 14:27
@Jwpe
Copy link
Contributor Author

Jwpe commented May 29, 2015

@jpadilla modified this and rebased to take out the irrelevant changes I made before.

@jpadilla
Copy link
Owner

@Jwpe thanks!

jpadilla added a commit that referenced this pull request May 30, 2015
Added the ability to use custom renderers for the JWT endpoints.
@jpadilla jpadilla merged commit 6c5e918 into jpadilla:master May 30, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants