0

I am using flask-marshmallow to make an API for a front end client that is already made. In order to give the data off correctly, I need to give data as a list of dictionaries, but I can't figure out how to get APIFairy to stop wrapping the entire return inside of a dictionary.

I am using @miguel

Here is my schema:

def unPaginatedCollection(schema):
    class unPaginatedSchema(ma.Schema):
        class Meta:
            ordered = True
            ma.fields = ('data')

        data = ma.Nested(schema, many=True)

    unPaginatedSchema.__name__ = 'Paginated{}'.format(schema.__class__.__name__)
    return unPaginatedSchema

Here is my route

@types.route('/types', methods=['GET'])
@authenticate(token_auth)
@unpaginated_response(type_schema)
def all():
    """Retrieve all types"""
    return Type.select()

my unpaginated wrapper.

def unpaginated_response(schema, max_limit=25, order_by=None,
                       order_direction='asc'):
    def inner(f):
        @wraps(f)
        def unpaginate(*args, **kwargs):
            args = list(args)
            pagination = args.pop(-1)
            select_query = f(*args, **kwargs)
            if order_by is not None:
                o = order_by.desc() if order_direction == 'desc' else order_by
                select_query = select_query.order_by(o)

            count = db.session.scalar(sqla.select(
                sqla.func.count()).select_from(select_query))

            limit = pagination.get('limit', max_limit)
            offset = pagination.get('offset')
            after = pagination.get('after')
            if limit > max_limit:
                limit = max_limit
            if after is not None:
                if offset is not None or order_by is None:  # pragma: no cover
                    abort(400)
                if order_direction != 'desc':
                    order_condition = order_by > after
                    offset_condition = order_by <= after
                else:
                    order_condition = order_by < after
                    offset_condition = order_by >= after
                query = select_query.limit(limit).filter(order_condition)
                offset = db.session.scalar(sqla.select(
                    sqla.func.count()).select_from(select_query.filter(
                        offset_condition)))
            else:
                if offset is None:
                    offset = 0
                if offset < 0 or (count > 0 and offset >= count) or limit <= 0:
                    abort(400)

                query = select_query.limit(limit).offset(offset)

            data = db.session.scalars(query).all()
            return {'data': data}
        # wrap with APIFairy's arguments and response decorators
        return arguments(EmptySchema)(response(unPaginatedCollection(
            schema))(unpaginate))
    return inner

Here is how I am outputting now.

{#I want to kill this lead value and release only the list inside of data
  "data": [
    {
      "banners": [], 
      "created_at": "2022-10-03T00:20:16.691223", 
      "icon": "FruitsVegetable", 
      "id": 1, 
      "images": [], 
      "language": "en", 
      "name": "Grocery", 
      "promotional_sliders": [], 
      "settings": {
        "isHome": true, 
        "layoutType": "classic", 
        "productCard": "neon"
      }, 
      "slug": "grocery", 
      "translated_languages": [
        "es", 
        "fr", 
        "de"
      ], 
      "updated_at": "2022-10-03T00:20:16.691223"
    }, 
    {
      "banners": [], 
      "created_at": "2022-10-03T00:20:16.693224", 
      "icon": "Bakery", 
      "id": 2, 
      "images": [], 
      "name": "Bakery", 
      "promotional_sliders": [], 
      "settings": {
        "isHome": false, 
        "layoutType": "standard", 
        "productCard": "argon"
      }, 
      "slug": "bakery", 
      "translated_languages": [], 
      "updated_at": "2022-10-03T00:20:16.693224"
    }
  ]
}

This is my goal output.

  [
    {
      "banners": [], 
      "created_at": "2022-10-03T00:20:16.691223", 
      "icon": "FruitsVegetable", 
      "id": 1, 
      "images": [], 
      "language": "en", 
      "name": "Grocery", 
      "promotional_sliders": [], 
      "settings": {
        "isHome": true, 
        "layoutType": "classic", 
        "productCard": "neon"
      }, 
      "slug": "grocery", 
      "translated_languages": [
        "es", 
        "fr", 
        "de"
      ], 
      "updated_at": "2022-10-03T00:20:16.691223"
    }, 
    {
      "banners": [], 
      "created_at": "2022-10-03T00:20:16.693224", 
      "icon": "Bakery", 
      "id": 2, 
      "images": [], 
      "name": "Bakery", 
      "promotional_sliders": [], 
      "settings": {
        "isHome": false, 
        "layoutType": "standard", 
        "productCard": "argon"
      }, 
      "slug": "bakery", 
      "translated_languages": [], 
      "updated_at": "2022-10-03T00:20:16.693224"
    }
  ]

I started off with this code from @Miguel-Grinberg https://github.com/miguelgrinberg/microblog-api

rockets4all
  • 684
  • 3
  • 8
  • 32
  • `return inner.get("data")` ? – Jeremy Savage Oct 08 '22 at 19:14
  • I tried that, but no worky. Here is the dict of inner ['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] – rockets4all Oct 08 '22 at 19:51

0 Answers0