4

[Using Python, Flask, flask_restplus, Swagger]

I'm trying to display a schema model as the picture below using flask_restplus. Prototype schema in yml and not python:

enter image description here

I created the schema_model but I'm not sure how to input it into the code so that it pairs to the GET call. How do I display the schema_model?


import requests
from flask import Flask, request, json, jsonify, Blueprint
from flask_restplus import Resource, Api, reqparse, fields, SchemaModel

app = Flask(__name__)
api = Api(app, title='Function Test', doc='/FT')

rbt = api.namespace('RBT', description='Accessible by API')

address = api.schema_model('Address', {
    'properties': {
        'road': {
            'type': 'string'
        },
    },
    'type': 'object'
})

@rbt.route('/<string:resource>/<string:responder>/<string:tag>')
class RBT(Resource):

    @rbt.doc(responses={
        200: 'Success',
        400: 'Validation Error',
        500: 'Internal Server Error'
    })

    #@rbt.marshal_with(address)
    def get(self, resource, responder, tag, **kwargs):
        '''TC#1 Definition'''
        url2 = 'http://' + host +  port + '/' + resource + '?' +responder
        print(url2)

        url = 'http://httpbin.org/get'

        parameters = {'resource': resource, 'responder':responder, 'tag': tag}
        r = requests.get(url)
        data = r.text

        return data
andilabs
  • 22,159
  • 14
  • 114
  • 151
zeusking123
  • 189
  • 1
  • 10

1 Answers1

0

Few remarks about general concepts of serializing (aka marshaling) and deserializing in flask rest_plus:

  • for out-putting (serializing) use @api.marshall_with(some_serializer) - this will be used for displaying the returned object in GET but also resulting created object if your return object in response in POST or some other method.
  • for in-putting (deserializing) user @api.expect(some_deserializer, validate=True) - this will be used to convert incoming JSON into python objects and making validation that the shape of data is correct.

regarding JSON schema based serializers - the sad, but the straightforward answer is:

the functionality of api.schema_model() (JSON schema-based de/serializer) IS NOT WORKING properly in rest_plus see the github issues:

andilabs
  • 22,159
  • 14
  • 114
  • 151