Skip to content
CRAG666 edited this page Jun 26, 2020 · 13 revisions

Create Routes

The route files must be saved in the routes folder

img

To create a new group of routes it is necessary to first create a blueprint in the _init_.py file

img

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)

img

From create_routesStandars.py we import create_routes, this function receives as parameters a model, a schema, blueprint and a noun

create routes

From the noun, create_routes will create basic api endpoints. Uses the model for CRUD processes and the schema for data validation and serialization.

The created endpoints are as follows

  • 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

Route registration in the context of the application

Register the blueprint in server.py

register

Import the blueprint from the file where I create the endpoints

impors

register the blueprint(read about it)

register

Security

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.

login

The function creates an extra point.

  • POST localhost:5000/<noun>/login -> get token

Change fields to login

To change the default login fields, open the file api_template.py. Find the method called post.

The method looks like this

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.

img

img

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.

img

If you don't need token security just remove or comment out the decorator in api_template.py methods.

Create custom routes

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).

Example

img

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

img

Clone this wiki locally