-
Notifications
You must be signed in to change notification settings - Fork 1
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
feature/BE-14 #245
Merged
Merged
feature/BE-14 #245
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Effort: Medium
Priority: High
This issue must be handled immediately
Priority: Medium
This issue should be handled, if there isn't any high priority issue
Status: Review Needed
A review is needed for this issue
Team: Backend
issues related to backend
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Please review this PR and perform the steps below to get familiar with the concept. There are lots of to learn but I'll try to list down the ones I found useful while documenting the existing APIs.
SWAGGER_SETTINGS
to thebase.py
settings file. This was necessary to test our APIs that require authorization with tokens.urls.py
within thebackend
folder (notapi
). After running the application, you can have a look at the Swagger via this url: http://localhost:8000/api/v1/swagger/schema.Each endpoint was displayed twice because of that and I thought it looked too ugly. Let me know if we should've kept that line.
Let me provide my understanding of this module and what features I've used.
First of all,
drf_yasg
attempts to document all endpoints within theurlpatterns
inapi/urls.py
. Therefor if you add your API there, it will automatically be displayed in Swagger.However, there are couple of problems. Firstly, it doesn't generate all the possible responses. Secondly, it doesn't provide any description or something. So, we have to provide them manually. To do so, we have to use a decorator called
@swagger_auto_schema
. This decorator must be called before the relevant function (post, get etc.). At this point, please examine its usage in the repository carefully.@swagger_auto_schema
takes lots of arguments. Here are some of them I used:operation_description
: Puts a description about the APIoperation_summary
: Put a summary about the API (this is the one we see at first look)tags
: This enables us to group our APIs based on tags (actually I didn't have to use this for now but anyways)request_body
: This sets the request body. This part is a little bit complicated and I strongly believe we will learn more about this as time goes by. What I understand though, is that it generates the request body based on the serializer we use. Otherwise, we have to give it manually. (I gave it manually but I didn't have to - I was just trying to get familiar with it)responses
: This helps us to list the response types we wait for an API to return. Here we can also provide example outputs - which is very informative for a documentation. So we had better use this frequently.Additionally, I used something called
decorated view
for our logout endpoint. This simply lets you to define all things I listed above not before the declaration of the function but while providing it as a view in theurls.py
. Why did I use it? Well, because logout endpoint is implemented by Knox and we have to provide some response types to it. So, I believe it was a nice use case fordecorated views
.Lastly, there is this button called
Authorize
on the right corner in Swagger page. We have to use it to test APIs that require authorization. For example, in order to test thelogout
API, first you have to log in to the application and have a token with you. Try thelogin
endpoint first, copy paste the token returned to you and click on theAuthorize
button. Input the token in the following format: "Token xxx". Replacexxx
with the token and don't forget to put a space between the first word and your token.That's all, let me know if you have any question in mind or you identify a problem with the current configuration. That's ok if you don't understand all of the above, I can explain in the meeting (if time permits) as well - but try to have a basic idea about what's going on here.