RESTful API: Pagination

75 views
Skip to first unread message

Lucas Alvares Gomes

unread,
Jul 24, 2013, 2:06:02 PM7/24/13
to pytho...@googlegroups.com
Hi,

I need some helps with ideas on how to do pagination in a RESTful api implemented using wsme/pecan.

Basically I've something like:

class Items(wsme.types.Base):
    uuid = wsme.types.text


class ItemsController(rest.RestController):

    @wsme_pecan.wsexpose([Items], int)
    def get_all(self, limit=None):
        ...
        return items_list


What I want to implement is something that will work like:

1. GET ../items

[
    {"uuid": ...,
     ...
    },
    {
     "uuid":...,
     ...
    }
]


2. GET ../items?limit=10

{
    "items": [ {...}, {...}, ... ],
    "links": [ {"next": ... }, {"previous": ...} ]
}

The "case 1." is a GET without "limit" parameter, so it should  return a list of all items.
The "case 2." is a GET where the user specify "limit" parameter, so it should return a list of items with a limited size + links to the next or/and previous page.

Questions:
1. How do you guys would implement something similar using WSME?
2. I tried to create a complex type that could hold different types of values (something analogous to the C/C++ unions), but I couldn't find any obvious way of doing that, any thoughts on that?

The only workaround I see here is to set the return value to be "unicode" and then I could just return something which is JSON serializable, but by doing that I don't see any reason in using WSME in my API.

Cheers,
Lucas

Doug Hellmann

unread,
Jul 24, 2013, 2:36:56 PM7/24/13
to pytho...@googlegroups.com
It would be better to have the API always return the same type of object. The list of links could be empty in the first case, and populated with useful values in the second.

Doug



--
You received this message because you are subscribed to the Google Groups "python-wsme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-wsme...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Christophe de Vienne

unread,
Jul 24, 2013, 2:38:00 PM7/24/13
to pytho...@googlegroups.com
Hello Lucas,

Changing the shape of your returned values depending on the parameters
is, in my opinion, not a good idea.

I see two options in your case :

1. Use a complex type to wrap the result and meta data, and use it
whatever the parameters are :

class ItemListResult(Base):
result = Item
next = text
previous = text
total = int

Note that it you do not affect a value to the metadata fields, they will
not be returned at all and the returned value will look like :

{
"result": [
{"uuid": ""},
...
]
}

2. Skip the metadata information altogether.

It will not prevent the client to use limit/offset, but the total count
will not be available.


Option 1 gives you more control, which is better if you have a lot of
item. Moverover it allows you to limit by default the returned number of
items even if no pagination parameters are given, which is almost
mandatory if you have huge number of items.


Christophe

Le 24/07/2013 20:06, Lucas Alvares Gomes a �crit :
Reply all
Reply to author
Forward
0 new messages