2

I have an API fully documented and finished, built in python 3.5/flask using flask-restplus. I'd like to add one chunk of functionality - returning a pickled object as part of one of my responses.

General solutions not specific to flask-restplus are welcome, but as my API is fully documented and finished (other than this little bit), I'd rather hang this on rather than fundamentally altering the framework I'm using.

My model schema looks like this (simplified):

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'),
})

And an example class (to be pickled) and model that I want to form the api response:

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)

And the api endpoint method declared as:

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

In this example I've given, using fields.Raw() as a marshaller for a pickled object doesn't work (there's no json representation). So what should I do instead to minimise restructuring of my API framework?

[EDIT: to fix syntax error in original Q]

4

1 回答 1

0

最终答案:

最后,我们不再使用 pickle,以避免在更新类时出现版本控制问题,然后尝试从旧版本中解压。

我们最终使用了这个 SO 答案中的建议,即使用jsonpickle库来序列化任意类对象并将其从 API 中吐出。

于 2018-02-28T14:02:28.120 回答