Book Image

Hands-On RESTful Python Web Services - Second Edition

By : Gaston C. Hillar
1 (1)
Book Image

Hands-On RESTful Python Web Services - Second Edition

1 (1)
By: Gaston C. Hillar

Overview of this book

Python is the language of choice for millions of developers worldwide that builds great web services in RESTful architecture. This second edition of Hands-On RESTful Python Web Services will cover the best tools you can use to build engaging web services. This book shows you how to develop RESTful APIs using the most popular Python frameworks and all the necessary stacks with Python, combined with related libraries and tools. You’ll learn to incorporate all new features of Python 3.7, Flask 1.0.2, Django 2.1, Tornado 5.1, and also a new framework, Pyramid. As you advance through the chapters, you will get to grips with each of these frameworks to build various web services, and be shown use cases and best practices covering when to use a particular framework. You’ll then successfully develop RESTful APIs with all frameworks and understand how each framework processes HTTP requests and routes URLs. You’ll also discover best practices for validation, serialization, and deserialization. In the concluding chapters, you will take advantage of specific features available in certain frameworks such as integrated ORMs, built-in authorization and authentication, and work with asynchronous code. At the end of each framework, you will write tests for RESTful APIs and improve code coverage. By the end of the book, you will have gained a deep understanding of the stacks needed to build RESTful web services.
Table of Contents (19 chapters)
Title Page
Dedication
About Packt
Contributors
Preface
Index

Using a dictionary as a repository


Now, we will create a NotificationManager class that we will use to persist the NotificationModel instances in an in-memory dictionary. Our API methods will call methods for the NotificationManager class to retrieve, insert, update, and delete NotificationModel instances. Create a new service.py file in the service folder. The following lines show the code that creates a NotificationManager class in the service/service.py file. In addition, the following lines declare all the imports we will need for all the code we will write in this file. The code file for the sample is included in the restful_python_2_01_01 folder, in the Flask01/service/service.py file:

from flask import Flask 
from flask_restful import abort, Api, fields, marshal_with, reqparse, Resource 
from datetime import datetime 
from models import NotificationModel 
from http_status import HttpStatus 
from pytz import utc 
 
 
class NotificationManager(): 
    last_id = 0 
    def __init__(self): 
        self.notifications = {} 
 
    def insert_notification(self, notification): 
        self.__class__.last_id += 1 
        notification.id = self.__class__.last_id 
        self.notifications[self.__class__.last_id] = notification 
 
    def get_notification(self, id): 
        return self.notifications[id] 
 
    def delete_notification(self, id): 
        del self.notifications[id] 

The NotificationManager class declares a last_id class attribute and initializes it to 0. This class attribute stores the last ID that was generated and assigned to a NotificationModel instance stored in a dictionary. The constructor, that is, the __init__ method, creates and initializes the notifications attribute as an empty dictionary.

 

The code declares the following three methods for the class:

  • insert_notification: This method receives a recently created NotificationModel instance in the notification argument. The code increases the value for the last_id class attribute and then assigns the resulting value to the ID for the received notification. The code uses self.__class__ to reference the type of the current instance. Finally, the code adds notification as a value to the key identified with the generated ID, last_id, in the self.notifications dictionary.
  • get_notification: This method receives the id of the notification that has to be retrieved from the self.notifications dictionary. The code returns the value related to the key that matches the received id in the self.notifications dictionary that we are using as our data source.
  • delete_notification: This method receives the id of the notification that has to be removed from the self.notifications dictionary. The code deletes the key-value pair whose key matches the ID received in the self.notifications dictionary that we are using as our data source.

We don't need a method to update a notification because we will just make changes to the attributes of the NotificationModel instance that is already stored in the self.notifications dictionary. The value stored in the dictionary is a reference to the NotificationModel instance that we are updating and, therefore, we don't need to call a specific method to update the instance in the dictionary. However, in case we were working with a database, we would need to call an update method for our ORM, data repository, or database service.