如何通过 API (最好使用 flask-restplus )编组腌制对象?
How can I marshal a pickled object through an API ( preferably using flask-restplus )?
我有一个 API 完全记录并完成的,使用 flask-restplus 在 python 3.5/flask 中构建。我想添加一大块功能 - 返回腌制对象作为我的其中一个响应的一部分。
欢迎使用非特定于 flask-restplus 的一般解决方案,但由于我的 API 已完全记录并完成(除了这一点),我宁愿坚持下去而不是从根本上改变框架我在用。
我的模型架构如下所示(简化):
get_fields = api.model('get-myresource', {
'id': fields.Integer(description='Id of the resource.'),
'other_field': fields.Integer(description='some other fields too.'),
'pickled_obj': fields.Raw(description='Marshalling fields.Raw fails, as the serialised binary cant be expressed as json'),
})
还有一个示例 class(待腌制)和我想要形成 api 响应的模型:
class MyClass(object):
# An instance of this class will be serialised with pickle
a_prop = None
def a_method(self):
return 'some_result'
obj_instance = MyClass()
class MyDataModel(object):
# It's actually a SQLAlchemy model, where one of the columns is PickleType, but for the sake of a canonical example...
id = 1
other_field = 2
pickled_obj = pickle.dumps(obj_instance)
并且 api 端点方法声明为:
import pickle
from flask import request
from flask_restplus import Resource, Namespace
from .my_schema_file import get_fields
api = Namespace('myresource', description='Whatever')
@api.route('')
class MyResource(Resource):
@api.response(200, 'Resource(s) returned successfully', get_fields)
@api.response(400, 'Bad Request')
@api.response(404, 'Not Found')
@api.response(500, 'Application Error')
@api.marshal_with(get_fields, code=200, description='Resource(s) returned successfully')
def get(self):
# Argument parsing and fetch from the database
data = MyDataModel()
return data, 200
在我给出的这个例子中,使用 fields.Raw() 作为腌制对象的编组器不起作用(没有 json 表示)。那么我应该怎么做才能最大限度地减少 API 框架的重构?
[编辑:修复原始 Q 中的语法错误]
最终答案:
最后,我们不再使用 pickle,以避免在更新我们的 classes 时出现版本控制问题,然后尝试从旧版本中 unpickle。
我们最终使用 this SO answer, which was to use the jsonpickle 库中的建议序列化任意 class 对象并将其从 API.
中吐出
我有一个 API 完全记录并完成的,使用 flask-restplus 在 python 3.5/flask 中构建。我想添加一大块功能 - 返回腌制对象作为我的其中一个响应的一部分。
欢迎使用非特定于 flask-restplus 的一般解决方案,但由于我的 API 已完全记录并完成(除了这一点),我宁愿坚持下去而不是从根本上改变框架我在用。
我的模型架构如下所示(简化):
get_fields = api.model('get-myresource', {
'id': fields.Integer(description='Id of the resource.'),
'other_field': fields.Integer(description='some other fields too.'),
'pickled_obj': fields.Raw(description='Marshalling fields.Raw fails, as the serialised binary cant be expressed as json'),
})
还有一个示例 class(待腌制)和我想要形成 api 响应的模型:
class MyClass(object):
# An instance of this class will be serialised with pickle
a_prop = None
def a_method(self):
return 'some_result'
obj_instance = MyClass()
class MyDataModel(object):
# It's actually a SQLAlchemy model, where one of the columns is PickleType, but for the sake of a canonical example...
id = 1
other_field = 2
pickled_obj = pickle.dumps(obj_instance)
并且 api 端点方法声明为:
import pickle
from flask import request
from flask_restplus import Resource, Namespace
from .my_schema_file import get_fields
api = Namespace('myresource', description='Whatever')
@api.route('')
class MyResource(Resource):
@api.response(200, 'Resource(s) returned successfully', get_fields)
@api.response(400, 'Bad Request')
@api.response(404, 'Not Found')
@api.response(500, 'Application Error')
@api.marshal_with(get_fields, code=200, description='Resource(s) returned successfully')
def get(self):
# Argument parsing and fetch from the database
data = MyDataModel()
return data, 200
在我给出的这个例子中,使用 fields.Raw() 作为腌制对象的编组器不起作用(没有 json 表示)。那么我应该怎么做才能最大限度地减少 API 框架的重构?
[编辑:修复原始 Q 中的语法错误]
最终答案:
最后,我们不再使用 pickle,以避免在更新我们的 classes 时出现版本控制问题,然后尝试从旧版本中 unpickle。
我们最终使用 this SO answer, which was to use the jsonpickle 库中的建议序列化任意 class 对象并将其从 API.
中吐出