Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

alamaby/python-mock-api-server-cli

Open more actions menu

Repository files navigation

Mock API Server CLI

A tool for reading JSON/YAML files containing API endpoint definitions and running a local server that provides endpoints according to the given definitions.

Features

  • Supports GET, POST, PUT, DELETE, and PATCH methods
  • Customizable HTTP status codes
  • Delay simulation to mimic real API response times
  • Support for JSON and YAML definition formats
  • CORS support for frontend development
  • Auto-reload when definition files change
  • Simple CLI interface

Requirements

  • Python 3.10+

Installation

Install the dependencies:

pip install -r requirements.txt

Or install as a package (with dev dependencies):

pip install -e ".[dev]"

Usage

Run the mock API server with the command:

python mock_server.py <definition_file> [options]

Options

Option Short Default Description
--port -p 8000 Server port
--host -H 127.0.0.1 Server host
--cors disabled Enable CORS for all origins
--reload disabled Auto-reload when definition file changes

Examples

# Basic usage
python mock_server.py example_api.json

# Custom port with YAML definition
python mock_server.py example_api.yaml --port 3000

# Enable CORS for frontend development
python mock_server.py example_api.json --cors

# Enable auto-reload during development
python mock_server.py example_api.json --reload

# Bind to all interfaces with CORS and auto-reload
python mock_server.py example_api.json --host 0.0.0.0 --port 5000 --cors --reload

API Definition Format

API definition files can be in JSON or YAML format. Basic structure:

{
  "endpoints": [
    {
      "path": "/users",
      "method": "GET",
      "response": {
        "data": []
      },
      "status_code": 200,
      "delay": 0.5
    }
  ]
}

Endpoint Properties

Property Required Default Description
path Yes - Endpoint path (e.g., /users, /users/{id})
method Yes - HTTP method (GET, POST, PUT, DELETE, PATCH)
response No {} Response data to be returned
status_code No 200 HTTP status code
delay No 0 Delay time in seconds before returning the response

Examples

See example_api.json and example_api.yaml for example API definitions.

Testing

This project includes comprehensive unit tests:

pip install -e ".[dev]"
python -m pytest test_mock_server.py -v

License

Apache-2.0

Contributing

Please create a pull request if you want to add features or fix bugs.

About

A tool for reading JSON/YAML files containing API endpoint definitions and running a local server that provides endpoints according to the given definitions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.