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

Declaring status codes for the responses with an enumerable


Neither Flask nor Flask-RESTful includes the declaration of variables for the different HTTP status codes. We don't want to return numbers as status codes. We want our code to be easy to read and understand, and therefore, we will use descriptive HTTP status codes. Specifically, we will take advantage of the support for enumerations added in Python 3.4 to declare a class that defines unique sets of names and values that represent the different HTTP status codes.

 

First, create a service folder within the root folder for the recently created virtual environment. Create a new http_status.py file within the service folder. The following lines show the code that declares the HttpStatus class that inherits from the enum.Enum class. The code file for the sample is included in the restful_python_2_01_01 folder, in the Flask01/service/http_status.py file:

from enum import Enum 
 
 
class HttpStatus(Enum): 
    continue_100 = 100 
    switching_protocols_101 = 101 
    ok_200 = 200 
    created_201 = 201 
    accepted_202 = 202 
    non_authoritative_information_203 = 203 
    no_content_204 = 204 
    reset_content_205 = 205 
    partial_content_206 = 206 
    multiple_choices_300 = 300 
    moved_permanently_301 = 301 
    found_302 = 302 
    see_other_303 = 303 
    not_modified_304 = 304 
    use_proxy_305 = 305 
    reserved_306 = 306 
    temporary_redirect_307 = 307 
    bad_request_400 = 400 
    unauthorized_401 = 401 
    payment_required_402 = 402 
    forbidden_403 = 403 
    not_found_404 = 404 
    method_not_allowed_405 = 405 
    not_acceptable_406 = 406 
    proxy_authentication_required_407 = 407 
    request_timetout_408 = 408 
    conflict_409 = 409 
    gone_410 = 410 
    length_required_411 = 411 
    precondition_failed_412 = 412 
    request_entity_too_large_413 = 413 
    request_uri_too_long_414 = 414 
    unsupported_media_type_415 = 415 
    requested_range_not_satisfiable_416 = 416 
    expectation_failed_417 = 417 
    precondition_required_428 = 428 
    too_many_requests_429 = 429 
    request_header_fields_too_large_431 = 431 
    unavailable_for_legal_reasons_451 = 451 
    internal_server_error_500 = 500 
    not_implemented_501 = 501 
    bad_gateway_502 = 502 
    service_unavailable_503 = 503 
    gateway_timeout_504 = 504 
    http_version_not_supported_505 = 505 
    network_authentication_required_511 = 511 
 
    @staticmethod 
    def is_informational(cls, status_code): 
        return 100 <= status_code.value <= 199 
 
    @staticmethod 
    def is_success(status_code): 
        return 200 <= status_code.value <= 299 
 
    @staticmethod 
    def is_redirect(status_code): 
        return 300 <= status_code.value <= 399 
 
    @staticmethod 
    def is_client_error(status_code): 
        return 400 <= status_code.value <= 499 
 
    @staticmethod 
    def is_server_error(status_code): 
        return 500 <= status_code.value <= 599 

The HttpStatus class defines unique sets of names and values that represent the different HTTP status codes. The names use the description as a prefix and the HTTP status code number as a suffix. For example, the 200 value of the HTTP 200 OK status code is defined in the HttpStatus.ok_200 name, and the HTTP 404 Not Found status code is defined in the HttpStatus.not_found_404 name.

Note

We will use the names defined in the enumerable to return a specific status code whenever necessary in our code. For example, in case we have to return an HTTP 404 Not Found status code, we will return HttpStatus.not_found_404.value, instead of just 404. This way, it will be easier to understand the code because we won't have to remember the meaning of each number.

 

In addition, the HttpStatus class declares five static methods that receive any of the HTTP status codes defined in the enumerable as an argument and determines which of the following categories the status code belongs to: informational, success, redirect, client error, or server error.