mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+247
-246
@@ -1,314 +1,315 @@
|
||||
---
|
||||
description: A comprehensive guide to best practices for developing RESTful APIs using Flask and Flask-RESTful, covering code organization, performance, security, and testing.
|
||||
globs: *.py
|
||||
description: This guide defines definitive best practices for building robust, maintainable, and modern REST APIs using Flask-RESTful, emphasizing Pydantic for validation and clear architectural patterns.
|
||||
globs: **/*.py
|
||||
---
|
||||
# flask-restful Best Practices: A Comprehensive Guide
|
||||
# flask-restful Best Practices
|
||||
|
||||
This document provides a comprehensive guide to developing RESTful APIs using Flask and Flask-RESTful, emphasizing maintainability, scalability, and performance. It covers various aspects, including code organization, common patterns, performance considerations, security, testing, and common pitfalls.
|
||||
|
||||
## Library Information:
|
||||
|
||||
- Name: flask-restful
|
||||
- Tags: web, api, python, flask
|
||||
Flask-RESTful provides a lightweight foundation for REST APIs. To build modern, scalable, and maintainable services in 2025, we augment its core with established Python best practices, focusing on type safety, clear separation of concerns, and robust data handling.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
### 1.1 Directory Structure Best Practices
|
||||
Always structure your application for modularity and testability. Use Flask Blueprints to organize API versions or domains.
|
||||
|
||||
A well-organized directory structure is crucial for maintainability. Here's a recommended structure:
|
||||
### ✅ GOOD: Modular Project Structure with Blueprints
|
||||
|
||||
Organize your application into logical packages:
|
||||
|
||||
project/
|
||||
├── api/
|
||||
│ ├── __init__.py
|
||||
│ ├── resources/
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── user.py # User resource
|
||||
│ │ ├── item.py # Item resource
|
||||
│ ├── models/
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── user_model.py # User model
|
||||
│ │ ├── item_model.py # Item model
|
||||
│ ├── schemas/
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── user_schema.py # User schema (using Marshmallow)
|
||||
│ │ ├── item_schema.py # Item schema
|
||||
│ ├── utils/
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── auth.py # Authentication utilities
|
||||
│ ├── app.py # Flask application initialization
|
||||
│ ├── config.py # Configuration settings
|
||||
├── tests/
|
||||
│ ├── __init__.py
|
||||
│ ├── test_user.py # Unit tests for user resource
|
||||
│ ├── test_item.py # Unit tests for item resource
|
||||
├── migrations/ # Alembic migrations (if using SQLAlchemy)
|
||||
├── venv/ # Virtual environment
|
||||
├── requirements.txt # Project dependencies
|
||||
├── Pipfile # Pipenv file
|
||||
├── Pipfile.lock # Pipenv lock file
|
||||
├── README.md
|
||||
```
|
||||
.
|
||||
├── app/
|
||||
│ ├── __init__.py # Flask app creation, API initialization
|
||||
│ ├── api/
|
||||
│ │ ├── __init__.py # Blueprint definition
|
||||
│ │ ├── v1/
|
||||
│ │ │ ├── __init__.py # Register resources to v1 blueprint
|
||||
│ │ │ ├── resources.py# API Resource classes
|
||||
│ │ │ └── services.py # Business logic for v1
|
||||
│ ├── schemas.py # Pydantic models for input/output
|
||||
│ ├── models.py # SQLAlchemy/SQLModel ORM definitions
|
||||
│ └── errors.py # Custom exception classes
|
||||
├── config.py
|
||||
├── run.py # Application entry point
|
||||
└── requirements.txt
|
||||
```
|
||||
|
||||
**`app/__init__.py`**:
|
||||
|
||||
* **api/**: Contains all API-related code.
|
||||
* **api/resources/**: Defines the API resources (e.g., User, Item).
|
||||
* **api/models/**: Defines the data models (e.g., User, Item).
|
||||
* **api/schemas/**: Defines the serialization/deserialization schemas (using Marshmallow or similar).
|
||||
* **api/utils/**: Utility functions (e.g., authentication, authorization).
|
||||
* **api/app.py**: Initializes the Flask application.
|
||||
* **api/config.py**: Stores configuration settings (e.g., database URI).
|
||||
* **tests/**: Contains unit and integration tests.
|
||||
* **migrations/**: Contains database migration scripts (if using SQLAlchemy).
|
||||
* **venv/**: The virtual environment (should not be committed to version control).
|
||||
* **requirements.txt**: Lists project dependencies. Alternatively `Pipfile` and `Pipfile.lock` for pipenv
|
||||
* **README.md**: Project documentation.
|
||||
```python
|
||||
from flask import Flask
|
||||
from flask_restful import Api
|
||||
from config import Config
|
||||
from app.api import api_bp # Import the blueprint
|
||||
|
||||
### 1.2 File Naming Conventions
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(Config)
|
||||
|
||||
* Python files: Use snake_case (e.g., `user_model.py`, `item_resource.py`).
|
||||
* Class names: Use PascalCase (e.g., `UserModel`, `ItemResource`).
|
||||
* Variable names: Use snake_case (e.g., `user_id`, `item_name`).
|
||||
* Constants: Use SCREAMING_SNAKE_CASE (e.g., `MAX_RETRIES`, `API_VERSION`).
|
||||
# Register API blueprint
|
||||
app.register_blueprint(api_bp, url_prefix='/api')
|
||||
|
||||
### 1.3 Module Organization
|
||||
# Centralized error handling (see section 3)
|
||||
from app.errors import register_error_handlers
|
||||
register_error_handlers(app)
|
||||
|
||||
* Group related resources into modules (e.g., a `user` module containing `user_model.py`, `user_resource.py`, `user_schema.py`).
|
||||
* Use Blueprints to organize related views and other code. Blueprints can be registered with the app, tying all operations to it.
|
||||
* Keep modules small and focused on a single responsibility.
|
||||
* Use clear and descriptive module names.
|
||||
return app
|
||||
```
|
||||
|
||||
### 1.4 Component Architecture
|
||||
**`app/api/__init__.py`**:
|
||||
|
||||
A common component architecture involves the following layers:
|
||||
```python
|
||||
from flask import Blueprint
|
||||
from flask_restful import Api
|
||||
|
||||
* **Resource Layer:** Exposes the API endpoints using `flask-restful`'s `Resource` class.
|
||||
* **Service Layer:** Contains the business logic and interacts with the data models.
|
||||
* **Model Layer:** Defines the data models and interacts with the database (if applicable).
|
||||
* **Schema Layer:** Defines the serialization/deserialization logic using libraries like Marshmallow.
|
||||
api_bp = Blueprint('api', __name__)
|
||||
api = Api(api_bp)
|
||||
|
||||
### 1.5 Code Splitting Strategies
|
||||
# Import and register resources from v1
|
||||
from app.api.v1 import register_v1_resources
|
||||
register_v1_resources(api)
|
||||
```
|
||||
|
||||
* **Functional Decomposition:** Split code into functions based on their functionality.
|
||||
* **Modular Decomposition:** Split code into modules based on related functionality (e.g., user management, item management).
|
||||
* **Layered Architecture:** As described above, separate the resource, service, model, and schema layers.
|
||||
**`app/api/v1/__init__.py`**:
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
```python
|
||||
from flask_restful import Api
|
||||
|
||||
### 2.1 Design Patterns
|
||||
def register_v1_resources(api: Api):
|
||||
from .resources import UserResource, UserListResource
|
||||
api.add_resource(UserListResource, '/v1/users')
|
||||
api.add_resource(UserResource, '/v1/users/<int:user_id>')
|
||||
```
|
||||
|
||||
* **Resource Controller:** Use `flask-restful`'s `Resource` class to define API endpoints. It provides a structured way to handle different HTTP methods for a given resource.
|
||||
* **Data Access Object (DAO):** Encapsulate database access logic within DAOs to abstract the database implementation from the rest of the application.
|
||||
* **Repository Pattern:** Similar to DAO, but provides a higher-level abstraction for accessing data, often used with ORMs like SQLAlchemy.
|
||||
* **Serialization/Deserialization:** Use libraries like Marshmallow to handle the serialization and deserialization of data between Python objects and JSON.
|
||||
* **Middleware:** Implement custom middleware to handle tasks like authentication, logging, and request validation.
|
||||
* **Factory Pattern:** Used to create objects, especially resources and their dependencies, decoupling code from concrete implementations.
|
||||
## 2. Request/Response Patterns: Pydantic for Validation and Serialization
|
||||
|
||||
### 2.2 Recommended Approaches for Common Tasks
|
||||
**NEVER** use `flask_restful.reqparse` for complex input validation or `flask_restful.fields` for output serialization. These are outdated and lack type safety. **ALWAYS** use Pydantic for robust, type-checked data handling.
|
||||
|
||||
* **Input Validation:** Use Marshmallow schemas for input validation. Define the expected data types, required fields, and validation rules in the schema.
|
||||
* **Authentication:** Implement authentication using JWT (JSON Web Tokens). Use libraries like `Flask-JWT-Extended` or `Authlib` to handle JWT generation, verification, and storage.
|
||||
* **Authorization:** Implement authorization using roles and permissions. Use decorators to restrict access to specific endpoints based on user roles.
|
||||
* **Error Handling:** Implement centralized error handling using Flask's `errorhandler` decorator. Return meaningful error messages and appropriate HTTP status codes to the client.
|
||||
* **Pagination:** Implement pagination for large datasets to improve performance. Use query parameters to specify the page number and page size.
|
||||
* **API Versioning:** Use URL prefixes or custom headers to version your API. This allows you to introduce breaking changes without affecting existing clients.
|
||||
* **Rate Limiting:** Implement rate limiting to prevent abuse of your API. Use libraries like `Flask-Limiter` to limit the number of requests that can be made from a specific IP address within a given time period.
|
||||
### ❌ BAD: `reqparse` and `fields`
|
||||
|
||||
### 2.3 Anti-patterns and Code Smells
|
||||
```python
|
||||
# app/api/v1/resources.py (BAD)
|
||||
from flask_restful import Resource, reqparse, fields, marshal_with
|
||||
|
||||
* **Fat Resources:** Avoid putting too much logic directly into the resource classes. Delegate business logic to service classes.
|
||||
* **Tight Coupling:** Avoid tight coupling between resources and models. Use interfaces or abstract classes to decouple components.
|
||||
* **Ignoring Errors:** Always handle errors gracefully and return meaningful error messages to the client.
|
||||
* **Lack of Input Validation:** Failing to validate input can lead to security vulnerabilities and data corruption.
|
||||
* **Hardcoding Configuration:** Avoid hardcoding configuration values in the code. Use environment variables or configuration files instead.
|
||||
* **Inconsistent Naming:** Use consistent naming conventions throughout the codebase.
|
||||
* **Over-engineering:** Avoid over-engineering solutions. Keep the code simple and focused on solving the specific problem.
|
||||
user_fields = {
|
||||
'id': fields.Integer,
|
||||
'name': fields.String,
|
||||
'email': fields.String,
|
||||
}
|
||||
|
||||
### 2.4 State Management
|
||||
class UserResource(Resource):
|
||||
@marshal_with(user_fields)
|
||||
def get(self, user_id):
|
||||
# ... fetch user ...
|
||||
return user
|
||||
|
||||
Flask-RESTful is designed to be stateless. However, if you need to manage state, consider the following:
|
||||
def post(self):
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument('name', type=str, required=True, help='Name is required')
|
||||
parser.add_argument('email', type=str, required=True, help='Email is required')
|
||||
args = parser.parse_args()
|
||||
# ... create user ...
|
||||
return {'message': 'User created'}, 201
|
||||
```
|
||||
|
||||
* **Session Management:** Use Flask's session management capabilities for storing user-specific data across requests.
|
||||
* **Caching:** Use caching mechanisms (e.g., Redis, Memcached) for storing frequently accessed data to improve performance.
|
||||
* **Database:** Store persistent state in a database.
|
||||
### ✅ GOOD: Pydantic for Input Validation and Output Serialization
|
||||
|
||||
### 2.5 Error Handling
|
||||
Pydantic provides clear, declarative schemas, automatic validation, and excellent integration with type checkers.
|
||||
|
||||
* **Global Exception Handling:** Use `app.errorhandler` to handle exceptions globally. This provides a centralized way to catch unhandled exceptions and return appropriate error responses.
|
||||
* **Custom Exceptions:** Define custom exception classes for specific error conditions. This makes it easier to handle errors in a consistent way.
|
||||
* **Logging:** Log errors and exceptions to a file or a logging service. This helps with debugging and troubleshooting.
|
||||
* **HTTP Status Codes:** Return appropriate HTTP status codes to indicate the success or failure of a request.
|
||||
**`app/schemas.py`**:
|
||||
|
||||
## 3. Performance Considerations
|
||||
```python
|
||||
from pydantic import BaseModel, Field, EmailStr
|
||||
from typing import Optional
|
||||
|
||||
### 3.1 Optimization Techniques
|
||||
class UserCreateSchema(BaseModel):
|
||||
name: str = Field(..., min_length=1, description="User's full name")
|
||||
email: EmailStr = Field(..., description="User's email address")
|
||||
|
||||
* **Database Optimization:** Optimize database queries, use indexes, and consider caching database results.
|
||||
* **Caching:** Use caching mechanisms (e.g., Redis, Memcached) to store frequently accessed data.
|
||||
* **Gunicorn with multiple workers:** Gunicorn is a WSGI server that can run multiple worker processes to handle concurrent requests. This can significantly improve performance.
|
||||
* **Asynchronous Tasks:** Use Celery or other task queues for long-running tasks to avoid blocking the main thread.
|
||||
* **Code Profiling:** Use profiling tools to identify performance bottlenecks in the code.
|
||||
* **Connection Pooling:** Use connection pooling to reduce the overhead of establishing database connections.
|
||||
* **Avoid N+1 Query Problem:** When fetching related data, use eager loading or join queries to avoid the N+1 query problem.
|
||||
class UserResponseSchema(BaseModel):
|
||||
id: int = Field(..., description="Unique user identifier")
|
||||
name: str
|
||||
email: EmailStr
|
||||
is_active: bool = True
|
||||
|
||||
### 3.2 Memory Management
|
||||
class Config:
|
||||
from_attributes = True # Allow Pydantic to read ORM attributes
|
||||
```
|
||||
|
||||
* **Use Generators:** Use generators for processing large datasets to avoid loading the entire dataset into memory.
|
||||
* **Close Database Connections:** Always close database connections after use to release resources.
|
||||
* **Limit Data Serialization:** Avoid serializing large amounts of data unnecessarily.
|
||||
* **Garbage Collection:** Be aware of Python's garbage collection mechanism and avoid creating circular references.
|
||||
**`app/api/v1/resources.py`**:
|
||||
|
||||
### 3.3 Rendering Optimization
|
||||
```python
|
||||
from flask import request
|
||||
from flask_restful import Resource
|
||||
from app.schemas import UserCreateSchema, UserResponseSchema
|
||||
from app.api.v1.services import UserService # Business logic
|
||||
|
||||
* **Use Templates:** Use Jinja2 templates for rendering HTML content. Templates can be cached to improve performance.
|
||||
* **Minimize DOM Manipulation:** Minimize DOM manipulation in the client-side JavaScript code. Use techniques like virtual DOM to improve performance.
|
||||
* **Compress Responses:** Use gzip compression to reduce the size of the responses.
|
||||
class UserListResource(Resource):
|
||||
def post(self):
|
||||
try:
|
||||
# Validate input using Pydantic
|
||||
user_data = UserCreateSchema.parse_obj(request.json)
|
||||
user = UserService.create_user(user_data)
|
||||
# Serialize output using Pydantic
|
||||
return UserResponseSchema.from_orm(user).dict(), 201
|
||||
except ValueError as e: # Pydantic validation errors
|
||||
return {'message': str(e)}, 400
|
||||
|
||||
### 3.4 Bundle Size Optimization
|
||||
class UserResource(Resource):
|
||||
def get(self, user_id: int):
|
||||
user = UserService.get_user(user_id)
|
||||
if not user:
|
||||
return {'message': 'User not found'}, 404
|
||||
return UserResponseSchema.from_orm(user).dict(), 200
|
||||
```
|
||||
|
||||
* **Use a CDN:** Use a Content Delivery Network (CDN) to serve static assets like CSS, JavaScript, and images.
|
||||
* **Minify CSS and JavaScript:** Minify CSS and JavaScript files to reduce their size.
|
||||
* **Tree Shaking:** Use tree shaking to remove unused code from the JavaScript bundles.
|
||||
## 3. Error Handling
|
||||
|
||||
### 3.5 Lazy Loading
|
||||
Centralize error handling to provide consistent, informative JSON responses.
|
||||
|
||||
* **Lazy Load Images:** Use lazy loading for images to improve page load time.
|
||||
* **Lazy Load Modules:** Use lazy loading for modules that are not immediately needed.
|
||||
### ✅ GOOD: Custom Exceptions and Centralized Handling
|
||||
|
||||
## 4. Security Best Practices
|
||||
Define custom exceptions and register them with Flask's `app.register_error_handler` or Flask-RESTful's `api.handle_error` (or `error_router` if using Flask-RESTX).
|
||||
|
||||
### 4.1 Common Vulnerabilities
|
||||
**`app/errors.py`**:
|
||||
|
||||
* **SQL Injection:** Occurs when user input is directly inserted into SQL queries.
|
||||
* **Cross-Site Scripting (XSS):** Occurs when malicious JavaScript code is injected into the website.
|
||||
* **Cross-Site Request Forgery (CSRF):** Occurs when a malicious website tricks the user into performing an action on the legitimate website.
|
||||
* **Authentication and Authorization Flaws:** Occurs when authentication or authorization mechanisms are not properly implemented.
|
||||
* **Denial of Service (DoS):** Occurs when an attacker floods the server with requests, making it unavailable to legitimate users.
|
||||
```python
|
||||
from flask import Flask, jsonify
|
||||
from werkzeug.exceptions import HTTPException
|
||||
|
||||
### 4.2 Input Validation
|
||||
class APIError(HTTPException):
|
||||
code = 500
|
||||
description = 'An unexpected error occurred.'
|
||||
|
||||
* **Validate All Input:** Validate all user input, including query parameters, request bodies, and headers.
|
||||
* **Use Whitelisting:** Use whitelisting to allow only specific characters or values. Avoid blacklisting, which can be easily bypassed.
|
||||
* **Escape Output:** Escape output to prevent XSS vulnerabilities.
|
||||
def __init__(self, message: str = None, code: int = None, payload: dict = None):
|
||||
if message:
|
||||
self.description = message
|
||||
if code:
|
||||
self.code = code
|
||||
self.payload = payload
|
||||
super().__init__(description=self.description)
|
||||
|
||||
### 4.3 Authentication and Authorization
|
||||
def get_response(self, environment=None):
|
||||
response = jsonify({
|
||||
'message': self.description,
|
||||
'status_code': self.code,
|
||||
'payload': self.payload
|
||||
})
|
||||
response.status_code = self.code
|
||||
return response
|
||||
|
||||
* **Use JWT (JSON Web Tokens):** Use JWT for authentication. JWTs are a standard way to represent claims securely between two parties.
|
||||
* **Implement Role-Based Access Control (RBAC):** Implement RBAC to control access to resources based on user roles.
|
||||
* **Use Strong Passwords:** Enforce strong password policies, such as minimum length, complexity, and expiration.
|
||||
* **Implement Two-Factor Authentication (2FA):** Implement 2FA for added security.
|
||||
* **Rate Limiting:** Apply rate limits to prevent brute-force attacks.
|
||||
class NotFoundError(APIError):
|
||||
code = 404
|
||||
description = 'Resource not found.'
|
||||
|
||||
### 4.4 Data Protection
|
||||
class BadRequestError(APIError):
|
||||
code = 400
|
||||
description = 'Invalid request payload.'
|
||||
|
||||
* **Encrypt Sensitive Data:** Encrypt sensitive data at rest and in transit.
|
||||
* **Use HTTPS:** Use HTTPS to encrypt communication between the client and the server.
|
||||
* **Store Passwords Securely:** Store passwords securely using a one-way hash function like bcrypt or Argon2.
|
||||
* **Regularly Back Up Data:** Regularly back up data to prevent data loss.
|
||||
def register_error_handlers(app: Flask):
|
||||
@app.errorhandler(APIError)
|
||||
def handle_api_error(error: APIError):
|
||||
return error.get_response()
|
||||
|
||||
### 4.5 Secure API Communication
|
||||
@app.errorhandler(HTTPException)
|
||||
def handle_http_exception(e: HTTPException):
|
||||
return APIError(message=e.description, code=e.code).get_response()
|
||||
|
||||
* **Use HTTPS:** Always use HTTPS for API communication.
|
||||
* **Validate SSL Certificates:** Validate SSL certificates to prevent man-in-the-middle attacks.
|
||||
* **Use API Keys:** Use API keys to identify and authenticate clients.
|
||||
* **Implement CORS:** Implement Cross-Origin Resource Sharing (CORS) to control which domains can access the API.
|
||||
@app.errorhandler(Exception)
|
||||
def handle_generic_exception(e: Exception):
|
||||
app.logger.error(f"Unhandled exception: {e}", exc_info=True)
|
||||
return APIError(message="An unexpected server error occurred.", code=500).get_response()
|
||||
```
|
||||
|
||||
## 5. Testing Approaches
|
||||
**`app/api/v1/services.py`**:
|
||||
|
||||
### 5.1 Unit Testing
|
||||
```python
|
||||
from app.errors import NotFoundError
|
||||
from app.schemas import UserCreateSchema
|
||||
# Assume some ORM/DB interaction
|
||||
from typing import Optional
|
||||
|
||||
* **Test Individual Components:** Unit tests should focus on testing individual components in isolation.
|
||||
* **Mock Dependencies:** Use mocking to isolate the component being tested from its dependencies.
|
||||
* **Test Edge Cases:** Test edge cases and boundary conditions.
|
||||
* **Use Assertions:** Use assertions to verify that the code behaves as expected.
|
||||
class User: # Mock ORM model
|
||||
def __init__(self, id, name, email):
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.email = email
|
||||
self.is_active = True
|
||||
|
||||
### 5.2 Integration Testing
|
||||
_users = {} # In-memory mock DB
|
||||
|
||||
* **Test Interactions Between Components:** Integration tests should focus on testing the interactions between components.
|
||||
* **Use a Test Database:** Use a test database for integration tests.
|
||||
* **Test the API Endpoints:** Test the API endpoints to ensure that they return the correct data.
|
||||
class UserService:
|
||||
@staticmethod
|
||||
def create_user(data: UserCreateSchema) -> User:
|
||||
new_id = len(_users) + 1
|
||||
user = User(id=new_id, name=data.name, email=data.email)
|
||||
_users[new_id] = user
|
||||
return user
|
||||
|
||||
### 5.3 End-to-End Testing
|
||||
@staticmethod
|
||||
def get_user(user_id: int) -> Optional[User]:
|
||||
user = _users.get(user_id)
|
||||
if not user:
|
||||
raise NotFoundError(f"User with ID {user_id} not found.")
|
||||
return user
|
||||
```
|
||||
|
||||
* **Test the Entire Application:** End-to-end tests should focus on testing the entire application, including the front-end and back-end.
|
||||
* **Use a Testing Framework:** Use a testing framework like Selenium or Cypress to automate end-to-end tests.
|
||||
## 4. Type Hints
|
||||
|
||||
### 5.4 Test Organization
|
||||
**ALWAYS** use type hints for all functions, methods, and variables. This improves code readability, enables static analysis with tools like `mypy`, and reduces bugs.
|
||||
|
||||
* **Separate Test Files:** Create separate test files for each module or component.
|
||||
* **Use Descriptive Test Names:** Use descriptive test names to indicate what is being tested.
|
||||
* **Organize Tests by Feature:** Organize tests by feature to make it easier to find and run tests.
|
||||
```python
|
||||
# Example in app/api/v1/resources.py
|
||||
from flask_restful import Resource, Api
|
||||
from typing import Dict, Any
|
||||
|
||||
### 5.5 Mocking and Stubbing
|
||||
class HealthCheck(Resource):
|
||||
def get(self) -> Dict[str, Any]:
|
||||
"""Returns the API health status."""
|
||||
return {"status": "healthy", "version": "1.0.0"}
|
||||
|
||||
* **Use Mocking Libraries:** Use mocking libraries like `unittest.mock` or `pytest-mock` to create mock objects.
|
||||
* **Stub External Dependencies:** Stub external dependencies to isolate the component being tested.
|
||||
* **Verify Interactions:** Verify that the component being tested interacts with its dependencies as expected.
|
||||
def register_v1_resources(api: Api) -> None:
|
||||
# ...
|
||||
api.add_resource(HealthCheck, '/v1/health')
|
||||
```
|
||||
|
||||
## 5. Performance Considerations: Rate Limiting
|
||||
|
||||
Protect your API from abuse and ensure fair usage by implementing rate limiting.
|
||||
|
||||
### ✅ GOOD: `Flask-Limiter`
|
||||
|
||||
```python
|
||||
# app/__init__.py (partial)
|
||||
from flask import Flask
|
||||
from flask_restful import Api
|
||||
from flask_limiter import Limiter
|
||||
from flask_limiter.util import get_remote_address
|
||||
from config import Config
|
||||
|
||||
limiter = Limiter(
|
||||
key_func=get_remote_address,
|
||||
default_limits=["200 per day", "50 per hour"],
|
||||
storage_uri="memory://", # Use Redis in production
|
||||
)
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(Config)
|
||||
limiter.init_app(app) # Initialize limiter with the app
|
||||
|
||||
# ... rest of app setup ...
|
||||
|
||||
# Apply specific limits to resources
|
||||
from app.api.v1.resources import UserListResource
|
||||
limiter.limit("10 per minute")(UserListResource) # Apply to a specific resource
|
||||
|
||||
return app
|
||||
```
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1 Frequent Mistakes
|
||||
|
||||
* **Incorrect HTTP Method:** Using the wrong HTTP method for an operation (e.g., using GET to create a resource).
|
||||
* **Missing Content-Type Header:** Failing to set the `Content-Type` header in the request.
|
||||
* **Incorrect JSON Format:** Sending or receiving invalid JSON data.
|
||||
* **Ignoring the `request` Object:** Not properly handling the request object.
|
||||
* **Failing to Handle Errors:** Not gracefully handling exceptions and returning meaningful error messages.
|
||||
* **Not Protecting Against CSRF:** Not implementing CSRF protection.
|
||||
|
||||
### 6.2 Edge Cases
|
||||
|
||||
* **Empty Data:** Handling cases where the database returns empty data.
|
||||
* **Invalid Input:** Handling cases where the user provides invalid input.
|
||||
* **Network Errors:** Handling network errors and timeouts.
|
||||
* **Concurrent Requests:** Handling concurrent requests and race conditions.
|
||||
|
||||
### 6.3 Version-Specific Issues
|
||||
|
||||
* **Compatibility with Flask and Other Libraries:** Ensuring compatibility between flask-restful and other libraries.
|
||||
* **Deprecated Features:** Being aware of deprecated features and migrating to the new ones.
|
||||
|
||||
### 6.4 Compatibility Concerns
|
||||
|
||||
* **Python Versions:** Ensuring compatibility with different Python versions.
|
||||
* **Database Drivers:** Ensuring compatibility with different database drivers.
|
||||
* **Operating Systems:** Ensuring compatibility with different operating systems.
|
||||
|
||||
### 6.5 Debugging Strategies
|
||||
|
||||
* **Use Logging:** Use logging to track the flow of execution and identify errors.
|
||||
* **Use a Debugger:** Use a debugger to step through the code and inspect variables.
|
||||
* **Read Error Messages Carefully:** Pay attention to error messages and stack traces.
|
||||
* **Use Postman or curl:** Use Postman or curl to test API endpoints.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1 Recommended Development Tools
|
||||
|
||||
* **Virtual Environment:** Use a virtual environment (e.g., `venv`, `pipenv`, `conda`) to isolate project dependencies.
|
||||
* **IDE:** Use an Integrated Development Environment (IDE) like VS Code, PyCharm, or Sublime Text.
|
||||
* **REST Client:** Use a REST client like Postman or Insomnia to test API endpoints.
|
||||
* **Database Client:** Use a database client like DBeaver or pgAdmin to manage databases.
|
||||
|
||||
### 7.2 Build Configuration
|
||||
|
||||
* **Use a Build System:** Use a build system like Make or Fabric to automate build tasks.
|
||||
* **Specify Dependencies:** Specify all project dependencies in a `requirements.txt` file or Pipfile.
|
||||
* **Use a Configuration File:** Use a configuration file to store configuration values.
|
||||
|
||||
### 7.3 Linting and Formatting
|
||||
|
||||
* **Use a Linter:** Use a linter like pylint or flake8 to enforce code style guidelines.
|
||||
* **Use a Formatter:** Use a formatter like black or autopep8 to automatically format the code.
|
||||
|
||||
### 7.4 Deployment
|
||||
|
||||
* **Use a WSGI Server:** Use a WSGI server like Gunicorn or uWSGI to deploy the application.
|
||||
* **Use a Reverse Proxy:** Use a reverse proxy like Nginx or Apache to handle incoming requests and route them to the WSGI server.
|
||||
* **Use a Load Balancer:** Use a load balancer to distribute traffic across multiple servers.
|
||||
* **Use a Process Manager:** Use a process manager like Systemd or Supervisor to manage the WSGI server process.
|
||||
|
||||
### 7.5 CI/CD Integration
|
||||
|
||||
* **Use a CI/CD Tool:** Use a CI/CD tool like Jenkins, GitLab CI, or CircleCI to automate the build, test, and deployment process.
|
||||
* **Run Tests Automatically:** Run unit tests and integration tests automatically on every commit.
|
||||
* **Deploy Automatically:** Deploy the application automatically after the tests pass.
|
||||
* **Mixing concerns**: Keep business logic out of resources. Resources handle HTTP, services handle logic.
|
||||
* **Inconsistent response formats**: Always return JSON, even for errors.
|
||||
* **Lack of API versioning**: Use `/v1/`, `/v2/` in URLs to manage changes.
|
||||
* **Ignoring HTTP status codes**: Use appropriate codes (200 OK, 201 Created, 204 No Content, 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 500 Internal Server Error).
|
||||
* **No documentation**: Use OpenAPI/Swagger (consider Flask-RESTX for automatic generation).
|
||||
* **No testing**: Implement unit and integration tests with `pytest` and `factory-boy`.
|
||||
Reference in New Issue
Block a user