Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On RESTful Python Web Services

You're reading from   Hands-On RESTful Python Web Services Develop RESTful web services or APIs with modern Python 3.7

Arrow left icon
Product type Paperback
Published in Dec 2018
Publisher
ISBN-13 9781789532227
Length 500 pages
Edition 2nd Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Gaston C. Hillar Gaston C. Hillar
Author Profile Icon Gaston C. Hillar
Gaston C. Hillar
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Developing RESTful APIs and Microservices with Flask 1.0.2 FREE CHAPTER 2. Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask 3. Improving Our API and Adding Authentication to it with Flask 4. Testing and Deploying an API in a Microservice with Flask 5. Developing RESTful APIs with Django 2.1 6. Working with Class-Based Views and Hyperlinked APIs in Django 2.1 7. Improving Our API and Adding Authentication to it with Django 8. Throttling, Filtering, Testing, and Deploying an API with Django 2.1 9. Developing RESTful APIs with Pyramid 1.10 10. Developing RESTful APIs with Tornado 5.1.1 11. Working with Asynchronous Code, Testing, and Deploying an API with Tornado 12. Assessment 13. Other Books You May Enjoy

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.

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.

You have been reading a chapter from
Hands-On RESTful Python Web Services - Second Edition
Published in: Dec 2018
Publisher:
ISBN-13: 9781789532227
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image