DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Python REST API Example (With Microservices) — Part 1

Python REST API Example (With Microservices) — Part 1

Writing REST APIs with Python is an important skill for the microservices world. We'll start learning the steps with a basic class in this tutorial.

Bill Ward user avatar by
Bill Ward
·
Aug. 16, 18 · Tutorial
Like (20)
Save
Tweet
Share
56.32K Views

Join the DZone community and get the full member experience.

Join For Free

With the transition to microservices, it becomes necessary to know how to write simple REST APIs using Python.

In this post, I give a Python REST API example using Tornado. This is the first post in the series where we will design the microservice and code the sample class that the microservice will manage.

Design

To begin with, let's go ahead and define what out microservice is going to do. For this example, I want to track books. It can add books, remove books and give us a list of all the books. We could add more functionality but I want to keep this simple. This gives us a couple of endpoints:

We are not going to use a database, so we will have no persistence if we kill the web service. Again, I really want to keep this Python REST API example simple.

Our Python REST API Example Code

Now let's get to the code. Let's start out by writing our Book class that we will use to track our books.

import json


class Book:

    def __init__(self):
        self.books = []

    def add_book(self, title, author):
        new_book = {}
        new_book["Title"] = title
        new_book["Author"] = author
        self.books.append(new_book)
        print("Book: {0}".format(new_book))
        return json.dumps(new_book)

    def del_book(self, title):
        found = False
        for idx, book in enumerate(self.books):
            if book["Title"] == title:
                index = idx
                found = True
                del self.books[idx]
        print("books: {0}".format(json.dumps(self.books)))
        return found

    def get_all_books(self):
        return self.books

    def json_list(self):
        return json.dumps(self.books)

This is just a very simple class to let us add, delete and show all books which is a simple list of dicts which contains the book title and author. Notice that we have a method that returns JSON back. We will need this later for our web service to send output back.

In the next part, we will code the actual API itself.

Conclusion

I hope you have enjoyed this article, if so please leave a comment below. Also, please feel free to share the article with your friends. Thanks again for reading this post.

REST API Web Protocols microservice Python (language)

Published at DZone with permission of Bill Ward, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Role of Data Governance in Data Strategy: Part II
  • How to Develop a Portrait Retouching Function
  • Public Cloud-to-Cloud Repatriation Trend
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: