-
Notifications
You must be signed in to change notification settings - Fork 178
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
Should be able to specify serializer/deserializer on RestAPI.register #131
Comments
The serializer/deserializer objects may need more information, like some stored inside Example: let's say we have a model {
"message": "the answer is 42",
"postedOn": "2014-04-05T23:22:48Z",
"sender": "/api/user/1",
"receiver": "/api/user/2"
} |
A little change: if the method run is |
|
Good idea, I think offering the ability to register serializers/deserializers would go a long way. |
I've started the branch feature/flexible-content-types on my fork to work on this specification - it will need a big rewrite in many things! |
A good RESTful API can receive and answer requests in many formats (content types) and it would be nice if flask-peewee have this flexibility. With this implemented we can easily extend the API to support other formats (example: outputting JSON-LD instead of plain JSON, or XML, or even CSV etc.). I made a little specification of this new feature below for discussion:
Flexible Content-Types
Introduction
flask_peewee.rest.RestAPI.register
method should support aformats
parameter: a dictionary where keys are accepted content-types and values are serializer/deserializer objects that can handle it.The code would be something like this:
flask_peewee.rest.RestAPI.__init__
should also accept aformats
parameter that will default to all registered models in that object.A parameter
default_format
should also be accepted by these two methods, representing the default serializer/deserializer to use if user sendAccept: */*
(forGET
) or noContent-Type
(forPOST/PUT/DELETE
). Note that415 Unsupported Media Type
should be answered if the specified content type is not available.Serializer/Deserializer
The values of
formats
dict
(json_serdes
andxml_serdes
in the example) should provide the following methods:serialize_object
Method used serialize, for example, the result of a
GET /api/<model>/<pk>/
(one object).str
* object representing the object serialized (with possibliy some extra metadata) that will be returned to the HTTP client.serialize_collection
Method used to serialize, for example, the result of a
GET /api/<model>/
(many objects and possibly metadata such as pagination information).str
* representing the objects serialized with metadata information (like pagination) that will be returned to the HTTP client.deserialize_object
Method used to deserialize, for example, data coming from a
POST /api/<model>/
(one object).str
* object representing the object serialized and model class;dict
representing the object (flask_peewee.rest.RestResource
will then create the object from thedict
and do what is needed with it --obj.save
etc.).deserialize_collection
Method used to deserialize, for example, data coming from a
POST /api/<model>/?bulk=true
(many objects).str
* object representing objects serialized and model class;list
ofdict
s -- eachdict
represent one object data.Supporting JSON
By now, the default value for
formats
would be adict
with only the keyapplication/json
set (pointing to an object that have insideflask_peewee.serializer.Serializer
andflask_peewee.serializer.Deserializer
), like this:The default value of
default_format
should beapplication/json
.The text was updated successfully, but these errors were encountered: