Here’s exactly what the above signal is doing:
Lines 1 – 9 import some Flask modules to create the remainder API answers, together with importing the db example through the config.py module. In addition, they imports the SQLAlchemy individual and Marshmallow PersonSchema sessions to get into anyone database table and serialize the results.
Line 11 begins this is of read_all() that reacts on REST API Address endpoint Purchase /api/people and comes back all the records into the person database table arranged in ascending purchase by finally name.
Lines 19 – 22 inform SQLAlchemy to query the individual database table for all the reports, sort all of them in rising order (the default sorting purchase), and return a listing of Person Python objects due to the fact variable folk .
Range 24 is where the Marshmallow PersonSchema class description becomes valuable. Your develop a case from the PersonSchema , passing it the parameter many=True . This says to PersonSchema to expect an interable to serialize, and that's what the folks adjustable try.
Range 25 makes use of the PersonSchema case adjustable ( person_schema ), phoning the dump() strategy aided by the folks listing. As a result, an object creating a data feature, an object containing a people number which can be transformed into JSON. It is returned and switched by Connexion to JSON since a reaction to the remainder API name.
Notice: individuals listing changeable developed online 24 above can not be came back right because Connexion won’t understand how to transform the timestamp area into JSON. Coming back the list of group without running they with Marshmallow brings about a long error traceback last but not least this different:
Here’s another an element of the person.py module which makes a request for a single individual from person databases. Here, read_one(person_id) purpose obtains a person_id from REMAINDER URL route, indicating an individual is seeking a certain people. Continue reading