Adding pagination features

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

from helpers import PaginationHelper 
 
 
class NotificationListResource(Resource): 
    def get(self):
pagination_helper = PaginationHelper(
request,
query=Notification.query,
resource_for_url='service.notificationlistresource',

key_name='results',
schema=notification_schema)
pagination_result = pagination_helper.paginate_query()
return pagination_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 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 result set 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 notifications, specifically, an HTTP GET method to /service/notifications/. Notice that we don't specify any desired page number. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd311.txt file:

    http ":5000/service/notifications/"

The following is the equivalent curl command. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd312.txt file:

    curl -iX GET "localhost:5000/service/notifications/"

The new code for the NotificationListResource.get method will work with pagination and the result will provide us with the first four notifications in the results key, the total number of messages for the query in the count key and a link to the next and previous pages in the next and previous keys. In this case, the result set is the first page, and therefore, the link to the previous page in the previous key is null. We will receive a 200 OK status code in the response header and the four notifications in the results array. The next lines show the result of the previous command:

    HTTP/1.0 200 OK
    Content-Length: 2205
    Content-Type: application/json
    Date: Fri, 19 Oct 2018 16:11:58 GMT
    Server: Werkzeug/0.14.1 Python/3.7.1
    
    {
        "count": 12,
        "next": "http://localhost:5000/service/notifications/?page=2",
        "previous": null,
        "results": [
            {
                "creation_date": "2018-10-16T21:57:15.867853+00:00",
                "displayed_once": false,
                "displayed_times": 0,
                "id": 1,
                "message": "eSports competition finishes in 10 
minutes",
"notification_category": { "id": 1, "name": "Information", "url":
"http://localhost:5000/service/notification_categories/1"
}, "ttl": 30, "url": "http://localhost:5000/service/notifications/1" }, { "creation_date": "2018-10-16T23:07:27.372778+00:00", "displayed_once": false, "displayed_times": 0, "id": 3, "message": "Score calculation error", "notification_category": { "id": 3, "name": "Error", "url":
"http://localhost:5000/service/notification_categories/3"
}, "ttl": 30, "url": "http://localhost:5000/service/notifications/3" }, { "creation_date": "2018-10-16T21:58:43.737812+00:00", "displayed_once": true, "displayed_times": 1, "id": 2, "message": "No winners yet", "notification_category": { "id": 2, "name": "Warning", "url":
"http://localhost:5000/service/notification_categories/2"
}, "ttl": 15, "url": "http://localhost:5000/service/notifications/2" }, { "creation_date": "2018-10-19T12:40:09.726102+00:00", "displayed_once": false, "displayed_times": 0, "id": 5, "message": "Clash Royale has a new winner", "notification_category": { "id": 1, "name": "Information", "url": "http://localhost:5000/service
/notification_categories/1"
}, "ttl": 25, "url": "http://localhost:5000/service/notifications/5" } ] }

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. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd313.txt file:

    http ":5000/service/notifications/?page=1"

The following is the equivalent curl command. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd314.txt file:

    curl -iX GET "localhost:5000/service/notifications/?page=1"
Notice that the code in the PaginationHelper class considers that the 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 /service/notifications/ 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. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd315.txt file:

    http ":5000/service/notifications/?page=2"

The following is the equivalent curl command. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd316.txt file:

    curl -iX GET "localhost:5000/service/notifications/?page=2"

The result will provide us with the second set of four notification resources in the results key, the total number of messages for the query in the count key and a link to the next and previous pages in the next and previous keys. In this case, the result set is the second page, and therefore, the link to the previous page in the previous key is http://localhost:5000/service/notifications/?page=1. We will receive a 200 OK status code in the response header and the four messages in the results array:

    HTTP/1.0 200 OK
    Content-Length: 2299
    Content-Type: application/json
    Date: Fri, 19 Oct 2018 17:08:54 GMT
    Server: Werkzeug/0.14.1 Python/3.7.1
    
    {
        "count": 12,
        "next": "http://localhost:5000/service/notifications/?page=3",
        "previous":
"http://localhost:5000/service/notifications/?page=1",
"results": [ { "creation_date": "2018-10-19T12:40:10.241868+00:00", "displayed_once": false, "displayed_times": 0, "id": 6, "message": "Uncharted 4 has a new 2nd position score", "notification_category": { "id": 1, "name": "Information", "url":
"http://localhost:5000/service/notification_categories/1"
}, "ttl": 20, "url": "http://localhost:5000/service/notifications/6" }, { "creation_date": "2018-10-19T12:40:10.738386+00:00", "displayed_once": false, "displayed_times": 0, "id": 7, "message": "Fortnite has a new 4th position score", "notification_category": { "id": 1, "name": "Information", "url":
"http://localhost:5000/service/notification_categories/1"
}, "ttl": 18, "url": "http://localhost:5000/service/notifications/7" }, { "creation_date": "2018-10-19T12:40:11.237577+00:00", "displayed_once": false, "displayed_times": 0, "id": 8, "message": "Injustice 2 has a new winner", "notification_category": { "id": 1, "name": "Information", "url":
"http://localhost:5000/service/notification_categories/1"
}, "ttl": 14, "url": "http://localhost:5000/service/notifications/8" }, { "creation_date": "2018-10-19T12:40:11.737706+00:00", "displayed_once": false, "displayed_times": 0, "id": 9, "message": "PvZ Garden Warfare 2 has a new winner", "notification_category": { "id": 1, "name": "Information", "url":
"http://localhost:5000/service/notification_categories/1"
}, "ttl": 22, "url": "http://localhost:5000/service/notifications/9" } ] }

Finally, we will compose and send an HTTP request to retrieve the last page, that is, the third page for the notifications, specifically an HTTP GET method to /service/notifications/ 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. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd317.txt file:

    http ":5000/service/notifications/?page=3"

The following is the equivalent curl command. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd318.txt file.

    curl -iX GET "localhost:5000/service/notifications/?page=3"

The result will provide us with the last set with four notification resources in the results key), the total number of messages for the query in the count key and a link to the next and previous pages in the next and previous keys. In this case, the result set 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 four notifications in the results array:

    HTTP/1.0 200 OK
    Content-Length: 2251
    Content-Type: application/json
    Date: Fri, 19 Oct 2018 17:19:32 GMT
    Server: Werkzeug/0.14.1 Python/3.7.1
    
    {
        "count": 12,
        "next": null,
        "previous": 
"http://localhost:5000/service/notifications/?page=2",
"results": [ { "creation_date": "2018-10-19T12:40:12.246582+00:00", "displayed_once": false, "displayed_times": 0, "id": 10, "message": "Madden NFL 19 has a new 3rd position
score",
"notification_category": { "id": 1, "name": "Information", "url":
"http://localhost:5000/service/notification_categories/1"
}, "ttl": 15, "url": "http://localhost:5000/service/notifications/10" }, { "creation_date": "2018-10-19T12:40:12.748875+00:00", "displayed_once": false, "displayed_times": 0, "id": 11, "message": "Madden NFL 19 has a new winner", "notification_category": { "id": 1, "name": "Information", "url":
"http://localhost:5000/service/notification_categories/1"
}, "ttl": 18, "url": "http://localhost:5000/service/notifications/11" }, { "creation_date": "2018-10-19T12:40:13.253900+00:00", "displayed_once": false, "displayed_times": 0, "id": 12, "message": "FIFA 19 has a new 3rd position score", "notification_category": { "id": 1, "name": "Information", "url":
"http://localhost:5000/service/notification_categories/1"
}, "ttl": 16, "url": "http://localhost:5000/service/notifications/12" }, { "creation_date": "2018-10-19T12:40:13.750718+00:00", "displayed_once": false, "displayed_times": 0, "id": 13, "message": "NBA Live 19 has a new winner", "notification_category": { "id": 1, "name": "Information", "url":
"http://localhost:5000/service/notification_categories/1"
}, "ttl": 5, "url": "http://localhost:5000/service/notifications/13" } ] }
..................Content has been hidden....................

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