Adding pagination features

Open the api/views.py file and replace the code for the MessageListResource.get method with the highlighted lines in the next listing. In addition, make sure that you add the import statement. The code file for the sample is included in the restful_python_chapter_07_01 folder:

from helpers import PaginationHelper 
 
 
class MessageListResource(Resource): 
    def get(self): 
        pagination_helper = PaginationHelper( 
            request, 
            query=Message.query, 
            resource_for_url='api.messagelistresource', 
            key_name='results', 
            schema=message_schema) 
        result = pagination_helper.paginate_query() 
        return result

The new code for the get method creates an instance of the previously explained PaginationHelper class named pagination_helper with the request object as the first argument. The named arguments specify the query, resource_for_url, key_name, and schema that the PaginationHelper instance has to use to provide a paginated query result.

The next line calls the pagination_helper.paginate_query method that will return the results of the paginated query with the page number specified in the request. Finally, the method returns the results of the call to this method that include the previously explained dictionary. In this case, the paginated resultset with the messages will be rendered as a value of the 'results' key, specified in the key_name argument.

Now, we will compose and send an HTTP request to retrieve all the messages, specifically an HTTP GET method to /api/messages/.

http :5000/api/messages/

The following is the equivalent curl command:

curl -iX GET :5000/api/messages/

The new code for the MessageListResource.get method will work with pagination and the result will provide us the first 5 messages (results key), the total number of messages for the query (count key) and a link to the next (next key) and previous (previous key) pages. In this case, the resultset is the first page, and therefore, the link to the previous page (previous key) is null. We will receive a 200 OK status code in the response header and the 5 messages in the results array:

HTTP/1.0 200 OK
Content-Length: 2521
Content-Type: application/json
Date: Wed, 10 Aug 2016 18:26:44 GMT
Server: Werkzeug/0.11.10 Python/3.5.1
{
    "count": 12, 
    "results": [
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-08T12:27:30.124511+00:00", 
            "duration": 8, 
            "id": 2, 
            "message": "Checking light sensor", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/2"
        }, 
        {
            "category": {
                "id": 3, 
                "name": "Error", 
                "url": "http://localhost:5000/api/categories/3"
            }, 
            "creation_date": "2016-08-08T14:20:22.103752+00:00", 
            "duration": 10, 
            "id": 3, 
            "message": "Temperature sensor error", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/3"
        }, 
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-08T12:18:43.260474+00:00", 
            "duration": 5, 
            "id": 1, 
            "message": "Checking temperature sensor", 
            "printed_once": true, 
            "printed_times": 1, 
            "url": "http://localhost:5000/api/messages/1"
        }, 
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-09T20:18:26.648071+00:00", 
            "duration": 25, 
            "id": 4, 
            "message": "Initializing light controller", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/4"
        }, 
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-09T20:19:16.174807+00:00", 
            "duration": 20, 
            "id": 5, 
            "message": "Initializing light sensor", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/5"
        }
    ], 
    "next": "http://localhost:5000/api/messages/?page=2", 
    "previous": null
}

In the previous HTTP request, we didn't specify any value for the page parameter, and therefore the paginate_query method in the PaginationHelper class requests the first page to the paginated query. If we compose and send the following HTTP request to retrieve the first page of all the messages by specifying 1 for the page value, the API will provide the same results shown before:

http ':5000/api/messages/?page=1'

The following is the equivalent curl command:

curl -iX GET ':5000/api/messages/?page=1'

Tip

The code in the PaginationHelper class considers that first page is page number 1. Thus, we don't work with zero-based numbering for pages.

Now, we will compose and send an HTTP request to retrieve the next page, that is, the second page for the messages, specifically an HTTP GET method to /api/messages/ with the page value set to 2. Remember that the value for the next key returned in the JSON body of the previous result provides us with the full URL to the next page:

http ':5000/api/messages/?page=2'

The following is the equivalent curl command:

curl -iX GET ':5000/api/messages/?page=2'

The result will provide us the second set of the five message resource (results key), the total number of messages for the query (count key), a link to the next (next key), and previous (previous key) pages. In this case, the resultset is the second page, and therefore, the link to the previous page (previous key) is http://localhost:5000/api/messages/?page=1. We will receive a 200 OK status code in the response header and the 5 messages in the results array.

HTTP/1.0 200 OK
Content-Length: 2557
Content-Type: application/json
Date: Wed, 10 Aug 2016 19:51:50 GMT
Server: Werkzeug/0.11.10 Python/3.5.1
{
    "count": 12, 
    "next": "http://localhost:5000/api/messages/?page=3", 
    "previous": "http://localhost:5000/api/messages/?page=1", 
    "results": [
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-09T20:19:22.335600+00:00", 
            "duration": 18, 
            "id": 6, 
            "message": "Checking pressure sensor", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/6"
        }, 
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-09T20:19:26.189009+00:00", 
            "duration": 14, 
            "id": 7, 
            "message": "Checking gas sensor", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/7"
        }, 
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-09T20:19:29.854576+00:00", 
            "duration": 22, 
            "id": 8, 
            "message": "Setting ADC resolution", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/8"
        }, 
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-09T20:19:33.838977+00:00", 
            "duration": 15, 
            "id": 9, 
            "message": "Setting sample rate", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/9"
        }, 
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-09T20:19:37.830843+00:00", 
            "duration": 18, 
            "id": 10, 
            "message": "Initializing pressure sensor", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/10"
        }
    ]
}

Finally, we will compose and send an HTTP request to retrieve the last page, that is, the third page for the messages, specifically an HTTP GET method to /api/messages/ with the page value set to 3. Remember that the value for the next key returned in the JSON body of the previous result provides us with the URL to the next page:

http ':5000/api/messages/?page=3'

The following is the equivalent curl command:

curl -iX GET ':5000/api/messages/?page=3'

The result will provide us the last set with two message resources (results key), the total number of messages for the query (count key), a link to the next (next key), and previous (previous key) pages. In this case, the resultset is the last page, and therefore, the link to the next page (next key) is null. We will receive a 200 OK status code in the response header and the 2 messages in the results array:

HTTP/1.0 200 OK
Content-Length: 1090
Content-Type: application/json
Date: Wed, 10 Aug 2016 20:02:00 GMT
Server: Werkzeug/0.11.10 Python/3.5.1
{
    "count": 12, 
    "next": null, 
    "previous": "http://localhost:5000/api/messages/?page=2", 
    "results": [
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-09T20:19:41.645628+00:00", 
            "duration": 16, 
            "id": 11, 
            "message": "Initializing gas sensor", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/11"
        }, 
        {
            "category": {
                "id": 1, 
                "name": "Information", 
                "url": "http://localhost:5000/api/categories/1"
            }, 
            "creation_date": "2016-08-09T20:19:45.304391+00:00", 
            "duration": 5, 
            "id": 12, 
            "message": "Initializing proximity sensor", 
            "printed_once": false, 
            "printed_times": 0, 
            "url": "http://localhost:5000/api/messages/12"
        }
    ]
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.74.66