Building Restful API with Flask, Postman & PyTest - Part 2 (Read Time: 10 Mins) -

Building Restful API with Flask, Postman & PyTest - Part 2 (Read Time: 10 Mins) -

Today we shall cover the creation of mock endpoints in Postman. To help in the designing &amp; prototyping of API endpoints for the&nbsp;<strong>expense manager</strong>project using&nbsp;<strong>Flask</strong>&nbsp;and&nbsp;<strong>pytest</strong>&nbsp;in part 3.

Introduction

Today we shall cover the creation of mock endpoints in Postman. To help in the designing & prototyping of API endpoints for the expense managerproject using Flask and pytest in part 3.

For those that are new to this series, you can go to part 1 to understand the explanation of tools and libraries used for this series to create API endpoints in Flask.

You are encouraged to jump between each tutorial sections. I had included this postman collection to get you started to play around with mock endpoints. Tools* Postman Project Specification The expenses manager has to have the following features for the API endpoints:

  • Postman Why Create Mock APIs Instead of Building the API? The reason for it is to help in the prototyping process of developing the endpoints.

By creating prototypes, you gain the perspective on how to create the endpoints and see on how it will look like in the final product. Using your Prototype to Gain Understanding & Common Ground With the use of the prototypes, it allows you to communicate your understanding and get feedback to make changes before you start in developing the actual product. Breaking Dependence of Front-end & Back-end Through the creation of mock endpoints in postman, you break the inter-dependency issue that you might encounter with the front-end developers regarding on APIs.

There is always a problem in having the API endpoint to be operating 24/7 to test out your front-end's UI or the API you had built when you're moving at a fast pace.

Fortunately, with the creation of mock endpoints in Postman, it had made it easier to collaborate at a much faster pace. Creating a Postman Collection Before you start on creating mock endpoint you have to sign up for a postman account.

Once had created your new account, download the postman app in your os.

Create a new postman collection called Expenses Manager. A postman collection is a place to store your API request in postman.

Creating a Mock Server In Postman Now in your newly created Postman collection, click on the ">" icon and select the mocks tab.

There will be a button that says create a mock server click on that button to create a mock server as shown in the picture below.

Select the default settings and click create button as shown below.

With a mock server URL for your postman collection, you need to enable the Expenses Manager environment to use the mock server.

Enabling the Postman Environment

To enable the Postman, go to your top right corner just beside the icon with an eye.

Click a dropdown box with the name No Environment. Select Expenses Manager environment.

Select eye icon, it will show you the mock server's link under the local environment variable called url.

The variable allows you to create API points without the need to replace the URL of each request which saves you time.

Creating A New Request Now we shall proceed in setting your first request in postman. Select your "Expenses Manager" collection and click on the "..." button that is at the bottom of the arrow button.

Once you had click on the button, a list of options is displayed then click on Add Request as shown in the picture below.

From there enter the name of your new request called Get List of Transactions and save it to the expenses manager collection.

Editing the Description of a Request

Let's add descriptions to the request, click on arrow icon and then edit icon as shown below.

The description will become part of automatically created documentation. Therefore it is useful to add it in to explain what does the API do.

API Description: Get the list of transactions in the expenses manager

List of API Requests in Postman In RESTful API, there are a bunch of HTTP methods for different use cases.

You could get the list of methods founded by clicking on the arrow button of the GET method in your request.

Types of API Request Methods

Here are the HTTP methods that you will use for this expanses manager project or future APIs that you will be creating.

They follow a similar structure to the CRUD function of the typical database.

They are as follows:

  • Postman

    Edit Get List of Transactions Request

Now go to List of Transactions Request and enter the following URL.

{{url}}/transactions/

The 

{{url}}

represents the mock server URL. Duplicate the API Requests Duplicate the following request shown in the picture.

Creating an Example Response An example response is a mock response whenever you send an HTTP request.

Select the Get List of Transactions request and add a new example response.

Once you had created a new example, you will be shown the example response message as shown below.

Replace the name with Success Response and enter the body of the response shown below.

{
    "balance": 200,
    "transactions": [
        {
            "id": 1,
            "amount": "60",
            "description": "New jeans",
            "type": "expense",
            "inital_balance": 300,
            "current_balance": 240,
            "time": "2019-01-12 09:00:00"
        },
        {
            "id": 2,
            "amount": "40",
            "description": "Lunch at a restaurant",
            "type": "expense",
            "inital_balance": 240,
            "current_balance": 200,
            "time": "2019-01-12 12:00:00"
        },
        {
            "id": 3,
            "amount": "10",
            "description": "Tips",
            "type": "income",
            "inital_balance": 200,
            "current_balance": 210,
            "time": "2019-01-12 16:00:00"
        },
        {
            "id": 4,
            "amount": "10",
            "description": "Weekly bus pass",
            "type": "expense",
            "inital_balance": 210,
            "current_balance": 200,
            "time": "2019-01-12 18:00:00"
        }
    ]
}

Add Status Code

The status code is used to identify that an HTTP request to a server is successful. The most common type of HTTP status code is 404 which displays when there is no webpage for a website.

At the status dropdown box, you can select the status code for the example response which in this case is 200.

Now when you click on the send button on your Get List of Transactionsrequest.

You will be displayed the example response that you had created earlier.

Congratulations, you had created your first mock endpoint!!!

Now please create the remaining mock endpoints for the expenses manager project and test it out on your own.

I had included the Postman collection for the Expanses Manager. If your feeling lazy to play around with the mock endpoints, don't worry I won't tell anyone.

Expanses Manager Postman Collection Conclusion I hope that with your new founded skills in creating mock endpoints in postman. It can help you greatly in reducing the dependency between your front-end and the APIs you build.

Lastly, did you remember that you added the description of your API request?

Well, all Postman collections come with API documentation page that displays all your API requests and example responses for each API.

Having great API documentation is one of the keys to adopting APIs be it for internal, external or public use cases.

So, postman, have you covered in creating these documentations without spending any of your time writing it.

python

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Standard Data Types in Python - Python Tutorial - Python Training

This video on 'Standard Data Types in Python' will help you establish a foothold on Python by helping you learn basic concepts.

Python Libraries You Must Learn in 2020-Learn Python-Python Training

This LIVE session on 'Python Libraries' will help you understand the topmost trending Python libraries toy must learn.