Open In App

Building Mock APIs with Swagger

Last Updated : 22 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

This step-by-step tutorial for constructing a mock API in Python using Swagger. In this article, we’ll step through the full process, from defining our API with Swagger to developing and testing a mock server. Whether you’re an experienced developer or just starting started, this article will show you how to construct Mock APIs with Swagger. The project entails developing basic Mock APIs with Swagger, building a mock server in Python, and testing it to replicate API behavior.

Basic Terminologies

  • Swagger: An API standard with tools for designing, creating, documenting, and consuming RESTful web services.
  • A mock API is a mimicked version of a genuine API that is used for testing and development.

Building Mock APIs with Swagger in Python

Using Swagger, we will build a “basic Todo API“, produce a Python server stub, implement mock server logic, and execute the application. To replicate API answers, mock data for todos will be utilized. Below is the step-by-step procedure to build mock APIs with Swagger:

Step 1: Installation

In this step, we will install all the dependencies that we will require to build Mock APIs with Swagger. These are:

Install the below dependencies using pip and the following command:

pip install flask-restful
pip install flasgger

Step 2: Project Structure

Folder Structure
  |--->app.py
  |--->swagger.yml

Step 3: Create Swagger Specification

The provided Swagger YAML file (swagger.yml) defines the API structure and is used by the Swagger instance to generate Swagger UI documentation for the API. In this code, a Swagger 2.0 specification defines a Todo API with CRUD operations. It includes endpoints for retrieving all todos, creating a new todo, and managing individual todos by ID through GET, PUT, and DELETE operations.

Python3




swagger: '2.0'
info:
  title: Todo API
  description: Simple Todo API with CRUD operations
  version: '1.0'
 
paths:
  /todos:
    get:
      summary: Get all todos
      responses:
        '200':
          description: A list of todos
    post:
      summary: Create a new todo
      parameters:
        - name: todo
          in: body
          required: true
          schema:
            type: object
            properties:
              task:
                type: string
                description: The task for the new todo
              completed:
                type: boolean
                description: Whether the task is completed
      responses:
        '201':
          description: Todo created successfully
 
  /todos/{todo_id}:
    parameters:
      - name: todo_id
        in: path
        required: true
        type: integer
    get:
      summary: Get a specific todo by ID
      responses:
        '200':
          description: Todo details
        '404':
          description: Todo not found
    put:
      summary: Update a specific todo by ID
      parameters:
        - name: todo
          in: body
          required: true
          schema:
            type: object
            properties:
              task:
                type: string
                description: The updated task
              completed:
                type: boolean
                description: Whether the task is completed
      responses:
        '200':
          description: Todo updated successfully
        '404':
          description: Todo not found
    delete:
      summary: Delete a specific todo by ID
      responses:
        '200':
          description: Todo deleted successfully
        '404':
          description: Todo not found


Step 4: Implement Mock API Server Logic

In this Python code, a Flask application is created to implement a Todo API using Flask-Restful. The endpoints for managing todos are defined with GET, PUT, DELETE, POST methods, and Swagger is integrated for API documentation using the provided Swagger YAML template.

app.py

Python3




from flask import Flask, request
from flask_restful import Api, Resource
from flasgger import Swagger
 
app = Flask(__name__)
api = Api(app)
swagger = Swagger(app, template_file='swagger.yml')
 
todos = []
 
class TodoResource(Resource):
    def get(self, todo_id):
        todo = next((t for t in todos if t['id'] == todo_id), None)
        if todo:
            return todo
        else:
            return {'message': 'Todo not found'}, 404
 
    def put(self, todo_id):
        todo = next((t for t in todos if t['id'] == todo_id), None)
        if todo:
            todo['task'] = request.json.get('task', todo['task'])
            todo['completed'] = request.json.get(
                'completed', todo['completed'])
            return {'message': 'Todo updated successfully'}
        else:
            return {'message': 'Todo not found'}, 404
 
    def delete(self, todo_id):
        global todos
        todos = [t for t in todos if t['id'] != todo_id]
        return {'message': 'Todo deleted successfully'}
 
 
class TodoListResource(Resource):
    def get(self):
        return {'todos': todos}
 
    def post(self):
        new_todo = {
            'id': len(todos) + 1,
            'task': request.json['task'],
            'completed': request.json.get('completed', False)
        }
        todos.append(new_todo)
        return {'message': 'Todo created successfully'}, 201
 
api.add_resource(TodoListResource, '/todos')
api.add_resource(TodoResource, '/todos/<int:todo_id>')
 
if __name__ == '__main__':
    app.run(debug=True)


Step By Step Explanation of this Code

Import Necessary Modules

Import necessary modules and libraries, including Flask for the web framework, request for handling HTTP requests, Api and Resource from Flask-RESTful for building REST APIs, and Swagger from flasgger for integrating Swagger UI. Initialize the Flask app, API, and Swagger.

Python3




from flask import Flask, request
from flask_restful import Api, Resource
from flasgger import Swagger
 
app = Flask(__name__)
api = Api(app)
swagger = Swagger(app, template_file='swagger.yml')
todos = []


TodoResource Class

Define a class for handling individual todos (TodoResource). Implement methods for handling GET (retrieve todo by ID), PUT (update todo by ID), and DELETE (delete todo by ID) operations. The methods use the global todos list to store todo items.

Python3




class TodoResource(Resource):
    def get(self, todo_id):
        todo = next((t for t in todos if t['id'] == todo_id), None)
        if todo:
            return todo
        else:
            return {'message': 'Todo not found'}, 404
 
    def put(self, todo_id):
        todo = next((t for t in todos if t['id'] == todo_id), None)
        if todo:
            todo['task'] = request.json.get('task', todo['task'])
            todo['completed'] = request.json.get(
                'completed', todo['completed'])
            return {'message': 'Todo updated successfully'}
        else:
            return {'message': 'Todo not found'}, 404
 
    def delete(self, todo_id):
        global todos
        todos = [t for t in todos if t['id'] != todo_id]
        return {'message': 'Todo deleted successfully'}


TodoListResource Class

Define a class for handling the list of todos (TodoListResource). Implement methods for handling GET (retrieve all todos) and POST (create a new todo) operations. The POST method adds a new todo to the todos list.

Python3




class TodoListResource(Resource):
    def get(self):
        return {'todos': todos}
 
    def post(self):
        new_todo = {
            'id': len(todos) + 1,
            'task': request.json['task'],
            'completed': request.json.get('completed', False)
        }
        todos.append(new_todo)
        return {'message': 'Todo created successfully'}, 201


Resource Registration

Register the TodoListResource class to handle routes for listing and creating todos (‘/todos’) and the TodoResource class to handle routes for individual todos (‘/todos/<int:todo_id>’). Start the Flask application if the script is run directly.

Python3




api.add_resource(TodoListResource, '/todos')
api.add_resource(TodoResource, '/todos/<int:todo_id>')
 
if __name__ == '__main__':
    app.run(debug=True)


Step 5: Run the Mock Server:

Execute the Python script (python app.py). To execute the script, open a terminal and navigate to the project directory then run the command:

python app.py

Now, access Swagger UI at http://127.0.0.1:5000/apidocs/ to interact with the mock API.

http://127.0.0.1:5000/apidocs/

Output

Screenshot2024-01-17112658-ezgifcom-resize

Video Demonstration

Building Mock API Using Swagger Another Example

Below is the step-by-step procedure to build another mock APIs with Swagger in Python.

Note: We are using same file structure as above.

Folder Structure
  |--->books.py
  |--->swagger_books.yml

Step 1: Create Swagger Specification (swagger_books.yml)

In this code, a Swagger 2.0 specification defines a simple Book API with CRUD operations, including endpoints for retrieving all books, creating a new book, and managing individual books by ID through GET, PUT, and DELETE operations. The API documentation is structured to include details about book title, author, and appropriate HTTP responses.

Python3




swagger: '2.0'
info:
    title: Book API
    description: Simple Book API with CRUD operations
    version: '1.0'
 
paths:
    /books:
        get:
            summary: Get all books
            responses:
                '200':
                    description: A list of books
        post:
            summary: Create a new book
            parameters:
                - name: book
                in: body
                required: true
                schema:
                        type: object
                        properties:
                            title:
                                type: string
                                description: The title of the book
                            author:
                                type: string
                                description: The author of the book
            responses:
                '201':
                    description: Book created successfully
 
    /books/{book_id}:
        parameters:
            - name: book_id
            in: path
            required: true
            type: integer
        get:
            summary: Get a specific book by ID
            responses:
                '200':
                    description: Book details
                '404':
                    description: Book not found
        put:
            summary: Update a specific book by ID
            parameters:
                - name: book
                in: body
                required: true
                schema:
                        type: object
                        properties:
                            title:
                                type: string
                                description: The updated title
                            author:
                                type: string
                                description: The updated author
            responses:
                '200':
                    description: Book updated successfully
                '404':
                    description: Book not found
        delete:
            summary: Delete a specific book by ID
            responses:
                '200':
                    description: Book deleted successfully
                '404':
                    description: Book not found


Step 2: Write Python Script

In this Python code, a Flask application with Flask-Restful is implemented to create a Book API. Endpoints for managing books include GET, PUT, DELETE, and POST methods, with Swagger used for API documentation based on the provided Swagger YAML template (‘swagger_books.yml’). The code defines resources for book details, supports CRUD operations, and runs a development server when executed.

books.py

Python3




from flask import Flask, request
from flask_restful import Api, Resource
from flasgger import Swagger
 
app = Flask(__name__)
api = Api(app)
swagger = Swagger(app, template_file='swagger_books.yml'
 
books = []
 
 
class BookResource(Resource):
    def get(self, book_id):
        book = next((b for b in books if b['id'] == book_id), None)
        if book:
            return book
        else:
            return {'message': 'Book not found'}, 404
 
    def put(self, book_id):
        book = next((b for b in books if b['id'] == book_id), None)
        if book:
            book['title'] = request.json.get('title', book['title'])
            book['author'] = request.json.get('author', book['author'])
            return {'message': 'Book updated successfully'}
        else:
            return {'message': 'Book not found'}, 404
 
    def delete(self, book_id):
        global books
        books = [b for b in books if b['id'] != book_id]
        return {'message': 'Book deleted successfully'}
 
 
class BookListResource(Resource):
    def get(self):
        return {'books': books}
 
    def post(self):
        new_book = {
            'id': len(books) + 1,
            'title': request.json['title'],
            'author': request.json['author']
        }
        books.append(new_book)
        return {'message': 'Book created successfully'}, 201
 
 
api.add_resource(BookListResource, '/books')
api.add_resource(BookResource, '/books/<int:book_id>')
 
if __name__ == '__main__':
    app.run(debug=True)


Step 5: Run the Mock Server

Execute the Python script (python app.py). To execute the script, open a terminal and navigate to the project directory then run the command:

python books.py

Now, access Swagger UI at http://127.0.0.1:5000/apidocs/ to interact with the mock API.

http://127.0.0.1:5000/apidocs/

Video Demonstration



Similar Reads

Testing APIs with Swagger
API testing in Swagger involves validating the functionality and performance of APIs developed using the Swagger framework. Swagger, now known as the OpenAPI Specification, provides a standardized way to document and define RESTful APIs. API testing in Swagger focuses on verifying that the API endpoints adhere to the specified OpenAPI documentation
6 min read
Documenting RESTful APIs with Swagger
RESTful APIs play an important role in communicating between various software components. The interface used to consume APIs significantly impacts the chances of achieving business and technological objectives. In this article, we'll dive into the importance of RESTful API documentation and how Swagger simplifies this process. Documenting RESTful A
9 min read
Building RESTful APIs with FastAPI
FastAPI is a Python web framework that makes it easy to build APIs quickly and efficiently. There is Another famous framework for doing the same is Flask, which is also Python-based. In this article, we will focus on creating a RESTful API using FastAPI. In this article, we will learn how to create a basic ToDo List API using FastAPI. FastAPI is a
5 min read
Swagger UI
Swagger UI is a powerful tool for documenting and testing RESTful APIs. It provides an interactive web-based interface that makes it easy for developers to understand and explore APIs. In this article, we will explore Swagger UI with the help of Python examples. What is Swagger UI?Swagger Ui is a part of the Swagger system, which provides a set of
4 min read
Advanced Swagger Features
In this article, we'll explore advanced features of Swagger, which is known as the OpenAPI Specification, which has become pivotal in modern API development. Offering a standardized approach to describe, document, and consume RESTful web services, Swagger goes beyond basic functionality. We'll highlight five advanced features, showcasing their impl
5 min read
Spring Boot – REST API Documentation using Swagger
REST stands for Representational State Transfer. REST is an architectural design pattern that defines constraints that are used in web service development. Swagger is a framework in which we can test our REST APIs for different HTTP requests i.e. : GETPOSTPUTDELETEIn this article, we will be discussing using Swagger for documenting APIs in Spring B
4 min read
Streamlining API Documentation Workflows with Swagger
In the realm of software development, Application Programming Interfaces (APIs) serve as the backbone for seamless communication between different software components. To facilitate efficient utilization of these APIs, developers heavily rely on comprehensive documentation. One prominent tool in this domain is OpenAPI, formerly known as Swagger, wh
8 min read
Swagger and API Monitoring
API monitoring is like having a watchful guardian for your digital communication paths, playing a big role in making software work well. It keeps an eye on how things are going, manages mistakes, makes sure everything responds quickly, checks everything in real-time, makes the user experience better, and fixes problems before they become big issues
6 min read
Design First API Development with Swagger
API development plays a crucial role in modern software architecture, enabling different services to communicate with each other seamlessly. One popular approach to API development is the "Design-First" methodology, where the API's design is specified before the actual implementation. Swagger, now known as OpenAPI, is a powerful tool that facilitat
4 min read
Swagger Extensions
Swagger Extensions are a pivotal component in augmenting the capabilities of Swagger, a robust API development tool. This article delves into the significance of Swagger Extensions, providing insights into their functionality and how they contribute to crafting comprehensive and informative API documentation. What are Swagger Extensions?Swagger Ext
5 min read