-
Notifications
You must be signed in to change notification settings - Fork 0
Rutes
The route files must be saved in the routes folder
To create a new group of routes it is necessary to first create a blueprint in the _init_.py file
so that everything is ordered we create a .py file where the routes will be created
In the file we just created, we import the blueprint, a model and a schema (the imported model and schema will be used to manage your data)
From create_routesStandars.py we import create_routes, this function receives as parameters a model, a schema, blueprint and a noun
From the noun, create_routes will create basic api endpoints. Uses the model for CRUD processes and the schema for data validation and serialization.
- GET localhost:5000/<noun>/ -> returns all records
- GET localhost:5000/<noun>/<id> -> returns a record passed by id
- GET localhost:5000/<noun>/?page=<num_page>&size=<records_per_page> -> page all records
- POST localhost:5000/<noun>/ -> create new record
- PUT localhost:5000/<noun>/<id> -> update record for id
- DELETE localhost:5000/<noun>/<id> -> delete record for id
Note: <noun> is replaced by the string passed to create_routes
Register the blueprint in server.py
Import the blueprint from the file where I create the endpoints
register the blueprint(read about it)
By default, security is enabled for all endpoints created with create_routes. The security is given by jwt, if you want to create a login to get the token, just pass as last true parameter in the create_routes function, please note that the model passed to the function must include name and password as fields.
The function creates an extra point.
- POST localhost:5000/<noun>/login -> get token
To change the default login fields, open the file api_template.py. Find the method called post.
Change all the lines where it says name and password and replace with the fields of your choice.
In this line the password hash match is validated the compare_pasword method is defined in the model.
Modify what you think is convenient to obtain the token.
If you pay attention to api_template.py, you will notice that some methods are decorated by @token_required, this is to allow access only to requests that pass the correct token in the header with the x-access-token key. If you want to see how this is done go to the helpers folder and open the file validations.py, the token_required function is the decorator, please modify it as your need.
If you don't need token security just remove or comment out the decorator in api_template.py methods.
The routes are created the same as always, but additionally you can use the decorators that are in the project (token_required and validate_json).
At this point it is not necessary to explain token_required, but if you validate_json. validate_json receives an instance of a marshmallow schema as a parameter, to validate customer data entry (read Models and schemas)
If you want to see or modify the operation of validate_json, you can find it in the helpers folder in the file validations.py