Skip to content

srikaaviya/api_rate_limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ API Rate Limiter

A backend Django application that implements a simple IP-based API rate limiting mechanism using a fixed time window strategy. This protects APIs from abuse or spam by throttling excessive requests from the same IP.


πŸš€ Features

  • Tracks API requests per IP address
  • Allows configurable request limits and time windows
  • Blocks requests exceeding the limit
  • Sends meaningful error responses to the client
  • Logs and stores request data in a SQLite database
  • Unit tested using pytest
  • Easily testable with Postman

πŸ› οΈ Tech Stack

  • Language: Python
  • Framework: Django, Django REST Framework (DRF)
  • Database: SQLite
  • Testing: Pytest
  • Client Testing Tool: Postman
  • Version Control: Git & GitHub

πŸ“‚ Project Structure

rate_limiter_project/ β”‚ β”œβ”€β”€ limiter/ # Django app β”‚ β”œβ”€β”€ models.py # IPRequestLog model for tracking β”‚ β”œβ”€β”€ views.py # Main API view with rate limiter logic β”‚ β”œβ”€β”€ tests.py # Unit tests β”‚ β”œβ”€β”€ urls.py # API route β”‚ └── ... β”œβ”€β”€ rate_limiter_project/ β”‚ └── settings.py β”œβ”€β”€ db.sqlite3 # SQLite database β”œβ”€β”€ manage.py └── README.md


βš™οΈ Setup Instructions

1. Clone the Repo

git clone https://github.com/your-username/your-repo-name.git
cd your-repo-name

2. Set Up Virtual Environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

3. Install Dependencies
pip install -r requirements.txt

4. Run Migrations
python manage.py migrate

5. Run Development Server
python manage.py runserver
The API will be available at: http://127.0.0.1:8000/api/

πŸ§ͺ Running Tests

Make sure pytest is installed:

pip install pytest
Then run tests:

pytest
πŸ§ͺ API Endpoint Example

POST /api/limit/
Use Postman or curl to make repeated requests. After 5 requests (default limit), further requests from the same IP will be blocked temporarily.

πŸ“Œ How It Works

The user sends a request to the /api/limit/ endpoint.
The app checks how many requests this IP has made in the last N seconds.
If it's within the allowed limit, it logs the request and sends a success response.
If the limit is exceeded, it returns 429 Too Many Requests.
🧠 Concepts Used

Custom DRF views
Request throttling logic (manual)
IP extraction from headers
Query filtering with timestamps
Unit testing API views
SQLite ORM models
Serializers to control output
πŸ“„ Requirements

If you're creating requirements.txt, here’s a minimal version:

Django>=4.0
djangorestframework
pytest
πŸ‘©β€πŸ’» Author

Sri Kaaviya
GitHub: @srikaaviya

πŸ“œ License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages