-
Notifications
You must be signed in to change notification settings - Fork 72
SAFRSBase
Bases: flask_sqlalchemy.model.Model
This SQLAlchemy mixin implements Json Serialization for SAFRS SQLalchemy Persistent Objects
Serialization itself is performed by the to_dict
method
Initialization and instantiation are quite complex because we rely on the DB schema
The jsonapi id is generated from the primary keys of the columns
This class is mostly used as a sqla model mixin therefore the object attributes should not match column names or sqla attribute names, this is why most of the methods & properties have (or should have, hindsight is great :/) the distinguishing s prefix
jsonapi spec doesn’t allow “type” as an attribute nmae, but this is a pretty common column name we rename type to Type so we can support it. A bit hacky but better than not supporting “type” at all This may cause other errors too, for ex when sorting :return: renamed type
Object initialization:
- set the named attributes and add the object to the database
- create relationships
-
Parameters
-
url_prefix – URL prefix used by the app
-
type – endpoint type, e.g. “instance”
-
-
Returns
the API endpoint
-
Return type
str
-
Parameters
-
item – instance id or dict { “id” : .. “type” : ..}
-
failsafe – indicates whether we want an exception to be raised in case the id is not found
-
-
Returns
Instance or None. An error is raised if an invalid id is used
-
Parameters
http_method – the http method for which to retrieve the documentation
-
Returns
swagger body and response dictionaries
-
Return type
tuple
Create a swagger api model based on the sqlalchemy schema.
-
Returns
the object’s id type
-
Returns
json:api id
-
Return type
str
if the table/object has a single primary key “id”, it will return this id. In the other cases, the jsonapi “id” will be generated by the cls.id_type (typically by combining the PKs)
ClassPropertyDescriptor
Create a dictionary with all the instance “attributes” this method will be called by SAFRSJSONEncoder to serialize objects
-
Parameters
fields – if set, fields to include in the result
-
Returns
dictionary object
The optional fields attribute is used to implement jsonapi “Sparse Fieldsets” https://jsonapi.org/format/#fetching-sparse-fieldsets:
client MAY request that an endpoint return only specific fields in the response on a per-type basis by including a fields[TYPE] parameter. The value of the fields parameter MUST be a comma-separated (U+002C COMMA, “,”) list that refers to the name(s) of the fields to be returned. If a client requests a restricted set of fields for a given resource type, an endpoint MUST NOT include additional fields in resource objects of that type in its response.
Bases: object
Debug class