mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 14:52:55 +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:
+195
-336
@@ -1,338 +1,197 @@
|
||||
---
|
||||
description: Comprehensive guide for aiohttp development covering code organization, performance, security, testing, and deployment best practices. Provides actionable guidance for developers to build robust and maintainable aiohttp applications.
|
||||
globs: *.py
|
||||
description: This guide defines definitive best practices for using aiohttp, focusing on efficient, robust, and maintainable asynchronous HTTP client code.
|
||||
globs: **/*.py
|
||||
---
|
||||
# Aiohttp Best Practices
|
||||
|
||||
This document provides a comprehensive guide to aiohttp development, covering code organization, performance, security, testing, and deployment.
|
||||
|
||||
Library Information:
|
||||
- Name: aiohttp
|
||||
- Tags: web, python, http-client, async
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
### 1.1. Directory Structure Best Practices:
|
||||
|
||||
* **Project Root:**
|
||||
* `src/`: Contains the main application code.
|
||||
* `main.py`: Entry point of the application.
|
||||
* `app.py`: Application factory and setup.
|
||||
* `routes.py`: Defines application routes.
|
||||
* `handlers/`: Contains request handlers.
|
||||
* `user_handlers.py`: User-related handlers.
|
||||
* `product_handlers.py`: Product-related handlers.
|
||||
* `middlewares/`: Custom middleware components.
|
||||
* `logging_middleware.py`: Logging middleware.
|
||||
* `auth_middleware.py`: Authentication middleware.
|
||||
* `utils/`: Utility modules.
|
||||
* `db.py`: Database connection and utilities.
|
||||
* `config.py`: Configuration management.
|
||||
* `tests/`: Contains unit and integration tests.
|
||||
* `conftest.py`: Pytest configuration file.
|
||||
* `unit/`: Unit tests.
|
||||
* `integration/`: Integration tests.
|
||||
* `static/`: Static files (CSS, JavaScript, images).
|
||||
* `templates/`: Jinja2 or other template files.
|
||||
* `docs/`: Project documentation.
|
||||
* `requirements.txt`: Python dependencies.
|
||||
* `Dockerfile`: Docker configuration file.
|
||||
* `docker-compose.yml`: Docker Compose configuration.
|
||||
* `.env`: Environment variables.
|
||||
* `README.md`: Project description and instructions.
|
||||
* `.gitignore`: Specifies intentionally untracked files that Git should ignore.
|
||||
* `.cursor/rules/`: Project specific Cursor AI rules.
|
||||
|
||||
### 1.2. File Naming Conventions:
|
||||
|
||||
* Python files: `snake_case.py` (e.g., `user_handlers.py`, `database_utils.py`).
|
||||
* Class names: `CamelCase` (e.g., `UserHandler`, `DatabaseConnection`).
|
||||
* Function names: `snake_case` (e.g., `get_user`, `create_product`).
|
||||
* Variables: `snake_case` (e.g., `user_id`, `product_name`).
|
||||
* Constants: `UPPER_SNAKE_CASE` (e.g., `DEFAULT_PORT`, `MAX_CONNECTIONS`).
|
||||
|
||||
### 1.3. Module Organization:
|
||||
|
||||
* Group related functionality into modules.
|
||||
* Use clear and descriptive module names.
|
||||
* Avoid circular dependencies.
|
||||
* Keep modules focused and concise.
|
||||
* Use packages to organize modules into a hierarchical structure.
|
||||
|
||||
### 1.4. Component Architecture:
|
||||
|
||||
* **Layered Architecture:** Separate the application into distinct layers (e.g., presentation, business logic, data access).
|
||||
* **Microservices Architecture:** Decompose the application into small, independent services.
|
||||
* **Hexagonal Architecture (Ports and Adapters):** Decouple the application core from external dependencies.
|
||||
* **MVC (Model-View-Controller):** Organize the application into models (data), views (presentation), and controllers (logic).
|
||||
|
||||
### 1.5. Code Splitting Strategies:
|
||||
|
||||
* **Route-based splitting:** Load modules based on the requested route.
|
||||
* **Feature-based splitting:** Divide the application into feature modules.
|
||||
* **Component-based splitting:** Split the application into reusable components.
|
||||
* **On-demand loading:** Load modules only when they are needed.
|
||||
* **Asynchronous loading:** Use `asyncio.gather` or similar techniques to load modules concurrently.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1. Design Patterns:
|
||||
|
||||
* **Singleton:** For managing shared resources like database connections or configuration objects.
|
||||
* **Factory:** For creating instances of classes with complex initialization logic.
|
||||
* **Strategy:** For implementing different algorithms or behaviors.
|
||||
* **Observer:** For implementing event-driven systems.
|
||||
* **Middleware:** For handling cross-cutting concerns like logging, authentication, and error handling.
|
||||
|
||||
### 2.2. Recommended Approaches for Common Tasks:
|
||||
|
||||
* **Request Handling:** Use request handlers to process incoming requests.
|
||||
* **Routing:** Use `aiohttp.web.RouteTableDef` for defining routes.
|
||||
* **Middleware:** Implement middleware for request pre-processing and response post-processing.
|
||||
* **Data Serialization:** Use `aiohttp.web.json_response` for serializing data to JSON.
|
||||
* **Error Handling:** Implement custom error handlers to handle exceptions gracefully.
|
||||
* **Session Management:** Use `aiohttp-session` for managing user sessions.
|
||||
* **WebSockets:** Utilize `aiohttp.web.WebSocketResponse` for handling WebSocket connections.
|
||||
|
||||
### 2.3. Anti-patterns and Code Smells:
|
||||
|
||||
* **Creating a new `ClientSession` for each request:** This is a performance bottleneck. Reuse a single `ClientSession`.
|
||||
* **Blocking operations in asynchronous code:** Avoid using blocking operations (e.g., `time.sleep`) in asynchronous code.
|
||||
* **Ignoring exceptions:** Always handle exceptions properly to prevent unexpected behavior.
|
||||
* **Overusing global variables:** Avoid using global variables as much as possible to maintain code clarity and testability.
|
||||
* **Tight coupling:** Decouple components to improve maintainability and reusability.
|
||||
* **Hardcoding configuration:** Use environment variables or configuration files to manage configuration settings.
|
||||
|
||||
### 2.4. State Management:
|
||||
|
||||
* **Application State:** Store application-level state in the `aiohttp.web.Application` instance.
|
||||
* **Request State:** Store request-specific state in the `aiohttp.web.Request` instance.
|
||||
* **Session State:** Use `aiohttp-session` to manage user session data.
|
||||
* **Database:** Use a database like PostgreSQL, MySQL, or MongoDB to store persistent state.
|
||||
* **Redis/Memcached:** Use in-memory data stores for caching frequently accessed data.
|
||||
|
||||
### 2.5. Error Handling:
|
||||
|
||||
* **Use `try-except` blocks:** Wrap code that may raise exceptions in `try-except` blocks.
|
||||
* **Handle specific exceptions:** Catch specific exception types instead of using a generic `except Exception`.
|
||||
* **Log exceptions:** Log exceptions with detailed information for debugging.
|
||||
* **Return informative error responses:** Return appropriate HTTP status codes and error messages to the client.
|
||||
* **Implement custom error handlers:** Create custom error handlers to handle specific exception types.
|
||||
* **Use `aiohttp.web.HTTPException`:** Raise `aiohttp.web.HTTPException` to return HTTP error responses.
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1. Optimization Techniques:
|
||||
|
||||
* **Reuse `ClientSession`:** Always reuse a single `ClientSession` instance for making multiple requests.
|
||||
* **Connection Pooling:** aiohttp automatically uses connection pooling, so reuse your session.
|
||||
* **Keep-Alive Connections:** Keep-alive connections are enabled by default, reducing connection overhead.
|
||||
* **Gzip Compression:** Enable Gzip compression for responses to reduce bandwidth usage.
|
||||
* **Caching:** Implement caching for frequently accessed data to reduce database load.
|
||||
* **Optimize Database Queries:** Optimize database queries to improve response times.
|
||||
* **Use Indexes:** Use indexes in your database tables to speed up queries.
|
||||
* **Limit Payload Size:** Keep request and response payloads as small as possible.
|
||||
* **Background Tasks:** Use `asyncio.create_task` to offload long-running tasks to the background.
|
||||
* **Profiling:** Use profiling tools to identify performance bottlenecks.
|
||||
|
||||
### 3.2. Memory Management:
|
||||
|
||||
* **Avoid Memory Leaks:** Ensure that all resources are properly released to prevent memory leaks.
|
||||
* **Use Generators:** Use generators to process large datasets in chunks.
|
||||
* **Limit Object Creation:** Minimize the creation of objects to reduce memory overhead.
|
||||
* **Use Data Structures Efficiently:** Choose appropriate data structures to optimize memory usage.
|
||||
* **Garbage Collection:** Understand how Python's garbage collection works and optimize your code accordingly.
|
||||
|
||||
### 3.3. Rendering Optimization:
|
||||
|
||||
* **Template Caching:** Cache templates to reduce rendering time.
|
||||
* **Minimize Template Logic:** Keep template logic simple and move complex logic to request handlers.
|
||||
* **Use Asynchronous Templates:** Use asynchronous template engines like `aiohttp-jinja2`.
|
||||
* **Optimize Static Files:** Optimize static files (CSS, JavaScript, images) to reduce page load times.
|
||||
|
||||
### 3.4. Bundle Size Optimization:
|
||||
|
||||
* **Minimize Dependencies:** Reduce the number of dependencies in your project.
|
||||
* **Tree Shaking:** Use tree shaking to remove unused code from your bundles.
|
||||
* **Code Minification:** Minify your code to reduce bundle sizes.
|
||||
* **Code Compression:** Compress your code to further reduce bundle sizes.
|
||||
|
||||
### 3.5. Lazy Loading:
|
||||
|
||||
* **Lazy Load Modules:** Load modules only when they are needed.
|
||||
* **Lazy Load Images:** Load images only when they are visible in the viewport.
|
||||
* **Use Asynchronous Loading:** Use `asyncio.gather` or similar techniques to load resources concurrently.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### 4.1. Common Vulnerabilities:
|
||||
|
||||
* **SQL Injection:** Prevent SQL injection by using parameterized queries or an ORM.
|
||||
* **Cross-Site Scripting (XSS):** Prevent XSS by escaping user input in templates.
|
||||
* **Cross-Site Request Forgery (CSRF):** Prevent CSRF by using CSRF tokens.
|
||||
* **Authentication and Authorization Issues:** Implement secure authentication and authorization mechanisms.
|
||||
* **Denial-of-Service (DoS) Attacks:** Implement rate limiting and other measures to prevent DoS attacks.
|
||||
* **Insecure Dependencies:** Keep your dependencies up to date to prevent vulnerabilities.
|
||||
|
||||
### 4.2. Input Validation:
|
||||
|
||||
* **Validate all user input:** Validate all user input to prevent malicious data from entering your application.
|
||||
* **Use a validation library:** Use a validation library like `marshmallow` or `voluptuous` to simplify input validation.
|
||||
* **Sanitize user input:** Sanitize user input to remove potentially harmful characters.
|
||||
* **Limit input length:** Limit the length of input fields to prevent buffer overflows.
|
||||
* **Use regular expressions:** Use regular expressions to validate input patterns.
|
||||
|
||||
### 4.3. Authentication and Authorization:
|
||||
|
||||
* **Use a strong authentication scheme:** Use a strong authentication scheme like OAuth 2.0 or JWT.
|
||||
* **Store passwords securely:** Store passwords securely using a hashing algorithm like bcrypt.
|
||||
* **Implement role-based access control (RBAC):** Use RBAC to control access to resources based on user roles.
|
||||
* **Use secure cookies:** Use secure cookies to protect session data.
|
||||
* **Implement multi-factor authentication (MFA):** Use MFA to add an extra layer of security.
|
||||
|
||||
### 4.4. Data Protection:
|
||||
|
||||
* **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 data securely:** Store data in a secure location with appropriate access controls.
|
||||
* **Regularly back up data:** Regularly back up data to prevent data loss.
|
||||
* **Comply with data privacy regulations:** Comply with data privacy regulations like GDPR and CCPA.
|
||||
|
||||
### 4.5. Secure API Communication:
|
||||
|
||||
* **Use HTTPS:** Always use HTTPS for API communication.
|
||||
* **Implement API authentication:** Use API keys or tokens to authenticate API requests.
|
||||
* **Rate limit API requests:** Implement rate limiting to prevent abuse.
|
||||
* **Validate API requests:** Validate API requests to prevent malicious data from entering your application.
|
||||
* **Log API requests:** Log API requests for auditing and debugging.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### 5.1. Unit Testing:
|
||||
|
||||
* **Test individual components:** Unit tests should test individual components in isolation.
|
||||
* **Use a testing framework:** Use a testing framework like `pytest` or `unittest`.
|
||||
* **Write clear and concise tests:** Write clear and concise tests that are easy to understand.
|
||||
* **Test edge cases:** Test edge cases and boundary conditions.
|
||||
* **Use mocks and stubs:** Use mocks and stubs to isolate components under test.
|
||||
|
||||
### 5.2. Integration Testing:
|
||||
|
||||
* **Test interactions between components:** Integration tests should test interactions between different components.
|
||||
* **Test with real dependencies:** Integration tests should use real dependencies whenever possible.
|
||||
* **Test the entire application flow:** Integration tests should test the entire application flow.
|
||||
* **Use a testing database:** Use a testing database to isolate integration tests from the production database.
|
||||
|
||||
### 5.3. End-to-End Testing:
|
||||
|
||||
* **Test the entire system:** End-to-end tests should test the entire system from end to end.
|
||||
* **Use a testing environment:** Use a testing environment that mimics the production environment.
|
||||
* **Automate end-to-end tests:** Automate end-to-end tests to ensure that the system is working correctly.
|
||||
* **Use a browser automation tool:** Use a browser automation tool like Selenium or Puppeteer.
|
||||
|
||||
### 5.4. Test Organization:
|
||||
|
||||
* **Organize tests by module:** Organize tests by module to improve test discovery and maintainability.
|
||||
* **Use descriptive test names:** Use descriptive test names that clearly indicate what the test is verifying.
|
||||
* **Use test fixtures:** Use test fixtures to set up and tear down test environments.
|
||||
* **Use test markers:** Use test markers to categorize tests and run specific test suites.
|
||||
|
||||
### 5.5. Mocking and Stubbing:
|
||||
|
||||
* **Use mocks to simulate dependencies:** Use mocks to simulate the behavior of dependencies.
|
||||
* **Use stubs to provide predefined responses:** Use stubs to provide predefined responses to API calls.
|
||||
* **Use mocking libraries:** Use mocking libraries like `unittest.mock` or `pytest-mock`.
|
||||
* **Avoid over-mocking:** Avoid over-mocking, as it can make tests less reliable.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1. Frequent Mistakes:
|
||||
|
||||
* **Not handling exceptions properly:** Always handle exceptions to prevent unexpected behavior.
|
||||
* **Using blocking operations in asynchronous code:** Avoid using blocking operations in asynchronous code.
|
||||
* **Not closing `ClientSession`:** Always close the `ClientSession` to release resources.
|
||||
* **Not validating user input:** Always validate user input to prevent security vulnerabilities.
|
||||
* **Not using HTTPS:** Always use HTTPS for secure communication.
|
||||
|
||||
### 6.2. Edge Cases:
|
||||
|
||||
* **Handling timeouts:** Implement proper timeout handling to prevent requests from hanging indefinitely.
|
||||
* **Handling connection errors:** Handle connection errors gracefully to prevent application crashes.
|
||||
* **Handling large payloads:** Handle large payloads efficiently to prevent memory issues.
|
||||
* **Handling concurrent requests:** Handle concurrent requests properly to prevent race conditions.
|
||||
* **Handling Unicode encoding:** Be aware of Unicode encoding issues when processing text data.
|
||||
|
||||
### 6.3. Version-Specific Issues:
|
||||
|
||||
* **aiohttp version compatibility:** Be aware of compatibility issues between different aiohttp versions.
|
||||
* **asyncio version compatibility:** Be aware of compatibility issues between aiohttp and different asyncio versions.
|
||||
* **Python version compatibility:** Be aware of compatibility issues between aiohttp and different Python versions.
|
||||
|
||||
### 6.4. Compatibility Concerns:
|
||||
|
||||
* **Compatibility with other libraries:** Be aware of compatibility issues between aiohttp and other libraries.
|
||||
* **Compatibility with different operating systems:** Be aware of compatibility issues between aiohttp and different operating systems.
|
||||
* **Compatibility with different web servers:** Be aware of compatibility issues between aiohttp and different web servers.
|
||||
|
||||
### 6.5. Debugging Strategies:
|
||||
|
||||
* **Use logging:** Use logging to track application behavior and identify issues.
|
||||
* **Use a debugger:** Use a debugger to step through code and examine variables.
|
||||
* **Use a profiler:** Use a profiler to identify performance bottlenecks.
|
||||
* **Use error reporting tools:** Use error reporting tools to track and fix errors in production.
|
||||
* **Use a network analyzer:** Use a network analyzer like Wireshark to capture and analyze network traffic.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1. Recommended Development Tools:
|
||||
|
||||
* **IDE:** Use an IDE like VS Code, PyCharm, or Sublime Text.
|
||||
* **Virtual Environment:** Use a virtual environment to isolate project dependencies.
|
||||
* **Package Manager:** Use a package manager like pip or poetry to manage dependencies.
|
||||
* **Testing Framework:** Use a testing framework like pytest or unittest.
|
||||
* **Linting Tool:** Use a linting tool like pylint or flake8 to enforce code style.
|
||||
* **Formatting Tool:** Use a formatting tool like black or autopep8 to format code automatically.
|
||||
|
||||
### 7.2. Build Configuration:
|
||||
|
||||
* **Use a build system:** Use a build system like Make or tox to automate build tasks.
|
||||
* **Define dependencies in `requirements.txt` or `pyproject.toml`:** Specify all project dependencies in a `requirements.txt` or `pyproject.toml` file.
|
||||
* **Use a Dockerfile:** Use a Dockerfile to create a containerized build environment.
|
||||
* **Use Docker Compose:** Use Docker Compose to manage multi-container applications.
|
||||
|
||||
### 7.3. Linting and Formatting:
|
||||
|
||||
* **Use a consistent code style:** Use a consistent code style throughout the project.
|
||||
* **Configure linting tools:** Configure linting tools to enforce code style rules.
|
||||
* **Configure formatting tools:** Configure formatting tools to format code automatically.
|
||||
* **Use pre-commit hooks:** Use pre-commit hooks to run linters and formatters before committing code.
|
||||
|
||||
### 7.4. Deployment:
|
||||
|
||||
* **Use a web server:** Use a web server like Nginx or Apache to serve the application.
|
||||
* **Use a process manager:** Use a process manager like Supervisor or systemd to manage the application process.
|
||||
* **Use a reverse proxy:** Use a reverse proxy to improve security and performance.
|
||||
* **Use a load balancer:** Use a load balancer to distribute traffic across multiple servers.
|
||||
* **Use a monitoring system:** Use a monitoring system to track application health and performance.
|
||||
* **Standalone Server:** aiohttp.web.run_app(), simple but doesn't utilize all CPU cores.
|
||||
* **Nginx + Supervisord:** Nginx prevents attacks, allows utilizing all CPU cores, and serves static files faster.
|
||||
* **Nginx + Gunicorn:** Gunicorn launches the app as worker processes, simplifying deployment compared to bare Nginx.
|
||||
|
||||
### 7.5. CI/CD Integration:
|
||||
|
||||
* **Use a CI/CD pipeline:** Use a CI/CD pipeline to automate the build, test, and deployment process.
|
||||
* **Use a CI/CD tool:** Use a CI/CD tool like Jenkins, GitLab CI, or GitHub Actions.
|
||||
* **Run tests in the CI/CD pipeline:** Run tests in the CI/CD pipeline to ensure that code changes don't break the application.
|
||||
* **Automate deployment:** Automate deployment to reduce manual effort and improve consistency.
|
||||
|
||||
## Additional Best Practices:
|
||||
|
||||
* **Session Management:** Always create a `ClientSession` for making requests and reuse it across multiple requests to benefit from connection pooling. Avoid creating a new session for each request, as this can lead to performance issues.
|
||||
* **Error Handling:** Implement robust error handling in your request handlers. Use try-except blocks to manage exceptions, particularly for network-related errors. For example, handle `ConnectionResetError` to manage client disconnections gracefully.
|
||||
* **Middleware Usage:** Utilize middleware for cross-cutting concerns such as logging, error handling, and modifying requests/responses. Define middleware functions that accept a request and a handler, allowing you to process requests before they reach your main handler.
|
||||
* **Graceful Shutdown:** Implement graceful shutdown procedures for your server to ensure that ongoing requests are completed before the application exits. This can be achieved by registering shutdown signals and cleaning up resources.
|
||||
* **Security Practices:** When deploying, consider using a reverse proxy like Nginx for added security and performance. Configure SSL/TLS correctly to secure your application.
|
||||
* **Character Set Detection:** If a response does not include the charset needed to decode the body, use `ClientSession` accepts a `fallback_charset_resolver` parameter which can be used to introduce charset guessing functionality.
|
||||
* **Persistent Session:** Use `Cleanup Context` when creating a persistent session.
|
||||
|
||||
By adhering to these practices, developers can enhance the reliability, performance, and security of their `aiohttp` applications.
|
||||
# `aiohttp` Best Practices
|
||||
|
||||
`aiohttp` is our go-to for high-performance asynchronous HTTP client operations. These rules ensure our `aiohttp` code is consistent, efficient, and resilient.
|
||||
|
||||
## 1. Manage `ClientSession` Lifecycle Properly
|
||||
|
||||
Always use a single, long-lived `ClientSession` per application or logical component. This leverages connection pooling and keep-alive, drastically reducing overhead. Always use it as an `async` context manager to ensure connections are closed cleanly.
|
||||
|
||||
❌ **BAD: Creating a new session for every request**
|
||||
```python
|
||||
import aiohttp
|
||||
import asyncio
|
||||
|
||||
async def fetch_data(url: str) -> str:
|
||||
async with aiohttp.ClientSession() as session: # Session created and closed per call
|
||||
async with session.get(url) as response:
|
||||
return await response.text()
|
||||
|
||||
async def main():
|
||||
await fetch_data("http://example.com/api/v1/data")
|
||||
await fetch_data("http://example.com/api/v1/status") # New session, new connection
|
||||
```
|
||||
|
||||
✅ **GOOD: Single, long-lived `ClientSession`**
|
||||
```python
|
||||
import aiohttp
|
||||
import asyncio
|
||||
|
||||
# Create the session once, typically at application startup
|
||||
# or pass it down through dependencies.
|
||||
_GLOBAL_HTTP_SESSION: aiohttp.ClientSession | None = None
|
||||
|
||||
async def get_http_session() -> aiohttp.ClientSession:
|
||||
global _GLOBAL_HTTP_SESSION
|
||||
if _GLOBAL_HTTP_SESSION is None or _GLOBAL_HTTP_SESSION.closed:
|
||||
_GLOBAL_HTTP_SESSION = aiohttp.ClientSession()
|
||||
return _GLOBAL_HTTP_SESSION
|
||||
|
||||
async def close_http_session():
|
||||
global _GLOBAL_HTTP_SESSION
|
||||
if _GLOBAL_HTTP_SESSION and not _GLOBAL_HTTP_SESSION.closed:
|
||||
await _GLOBAL_HTTP_SESSION.close()
|
||||
_GLOBAL_HTTP_SESSION = None
|
||||
|
||||
async def fetch_data(session: aiohttp.ClientSession, url: str) -> str:
|
||||
async with session.get(url) as response:
|
||||
response.raise_for_status() # Always raise for non-2xx statuses
|
||||
return await response.text()
|
||||
|
||||
async def main():
|
||||
session = await get_http_session()
|
||||
try:
|
||||
data = await fetch_data(session, "http://example.com/api/v1/data")
|
||||
status = await fetch_data(session, "http://example.com/api/v1/status")
|
||||
print(f"Data: {data[:20]}..., Status: {status[:20]}...")
|
||||
finally:
|
||||
await close_http_session() # Ensure session is closed on app shutdown
|
||||
```
|
||||
|
||||
## 2. Install All Speedups
|
||||
|
||||
Always install `aiohttp` with its `[speedups]` extra. This includes `aiodns` for faster DNS resolution and `backports.zstd` (for Python < 3.14) for modern compression, significantly boosting performance.
|
||||
|
||||
```bash
|
||||
pip install aiohttp[speedups]
|
||||
```
|
||||
|
||||
## 3. Implement Robust Timeouts and Error Handling
|
||||
|
||||
Explicitly define timeouts using `aiohttp.ClientTimeout` and enable `raise_for_status=True` for automatic HTTP error detection. Wrap requests in `try/except aiohttp.ClientError` to handle network and HTTP-level issues gracefully.
|
||||
|
||||
❌ **BAD: No timeouts, generic exception handling**
|
||||
```python
|
||||
async def fetch_unreliable(session: aiohttp.ClientSession, url: str) -> str:
|
||||
# This request could hang indefinitely
|
||||
async with session.get(url) as response:
|
||||
# 4xx/5xx responses won't raise an error, requiring manual check
|
||||
return await response.text()
|
||||
```
|
||||
|
||||
✅ **GOOD: Explicit timeouts, `raise_for_status`, and specific error handling**
|
||||
```python
|
||||
from aiohttp import ClientSession, ClientTimeout, ClientError, ClientResponseError
|
||||
import asyncio
|
||||
|
||||
async def fetch_reliable(session: ClientSession, url: str) -> str:
|
||||
timeout = ClientTimeout(total=5, connect=1, sock_read=3) # 5s total, 1s connect, 3s read
|
||||
try:
|
||||
async with session.get(url, timeout=timeout, raise_for_status=True) as response:
|
||||
return await response.text()
|
||||
except ClientResponseError as e:
|
||||
print(f"HTTP error for {url}: {e.status} - {e.message}")
|
||||
raise
|
||||
except ClientError as e:
|
||||
print(f"Network or client error for {url}: {e}")
|
||||
raise
|
||||
except asyncio.TimeoutError:
|
||||
print(f"Request to {url} timed out.")
|
||||
raise
|
||||
```
|
||||
|
||||
## 4. Use Typed, Reusable Request Helpers
|
||||
|
||||
Encapsulate common request patterns in small, type-hinted `async` functions. This improves readability, maintainability, and enables better IDE support.
|
||||
|
||||
❌ **BAD: Repeated request logic and untyped functions**
|
||||
```python
|
||||
async def get_user_data(session, user_id):
|
||||
resp = await session.get(f"http://api.example.com/users/{user_id}")
|
||||
return await resp.json()
|
||||
|
||||
async def create_user(session, user_data):
|
||||
resp = await session.post("http://api.example.com/users", json=user_data)
|
||||
return await resp.json()
|
||||
```
|
||||
|
||||
✅ **GOOD: Type-hinted, reusable functions with `json=` shortcut**
|
||||
```python
|
||||
from typing import Any, Dict
|
||||
from aiohttp import ClientSession
|
||||
|
||||
async def fetch_json(session: ClientSession, url: str) -> Dict[str, Any]:
|
||||
"""Fetches JSON from a URL, raises for status, and returns parsed data."""
|
||||
async with session.get(url, raise_for_status=True) as response:
|
||||
return await response.json()
|
||||
|
||||
async def post_json(session: ClientSession, url: str, payload: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Posts JSON payload to a URL, raises for status, and returns parsed response."""
|
||||
async with session.post(url, json=payload, raise_for_status=True) as response:
|
||||
return await response.json()
|
||||
|
||||
async def get_user_data(session: ClientSession, user_id: str) -> Dict[str, Any]:
|
||||
return await fetch_json(session, f"http://api.example.com/users/{user_id}")
|
||||
|
||||
async def create_user(session: ClientSession, user_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return await post_json(session, "http://api.example.com/users", user_data)
|
||||
```
|
||||
|
||||
## 5. Prefer Built-in Authentication and Session Headers
|
||||
|
||||
Use the `auth` parameter for basic authentication or `DigestAuthMiddleware` for digest auth. For other authentication schemes or default headers, set them when initializing `ClientSession` or update `session.headers`. Avoid manual `Authorization` header manipulation where built-in options exist.
|
||||
|
||||
❌ **BAD: Manually constructing `Authorization` headers**
|
||||
```python
|
||||
import base64
|
||||
async def fetch_with_basic_auth(session: ClientSession, url: str, user: str, password: str):
|
||||
auth_str = base64.b64encode(f"{user}:{password}".encode()).decode()
|
||||
headers = {"Authorization": f"Basic {auth_str}"}
|
||||
async with session.get(url, headers=headers) as response:
|
||||
return await response.text()
|
||||
```
|
||||
|
||||
✅ **GOOD: Using `auth` parameter for basic auth**
|
||||
```python
|
||||
from aiohttp import ClientSession, BasicAuth
|
||||
|
||||
async def fetch_with_basic_auth(session: ClientSession, url: str, user: str, password: str):
|
||||
auth = BasicAuth(login=user, password=password)
|
||||
async with session.get(url, auth=auth) as response:
|
||||
response.raise_for_status()
|
||||
return await response.text()
|
||||
|
||||
# For session-wide default headers (e.g., Bearer tokens)
|
||||
async def create_auth_session(token: str) -> ClientSession:
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
return ClientSession(headers=headers)
|
||||
```
|
||||
|
||||
## 6. Leverage Client Middleware for Cross-Cutting Concerns
|
||||
|
||||
For concerns like tracing, retries, or rate limiting that apply across many requests, implement client-side middleware. This keeps your request logic clean and separates concerns effectively.
|
||||
|
||||
```python
|
||||
from aiohttp import ClientSession, ClientRequest, ClientResponse
|
||||
from aiohttp.typedefs import ClientHandler
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
async def logging_middleware(req: ClientRequest, handler: ClientHandler) -> ClientResponse:
|
||||
"""Logs request and response details."""
|
||||
logging.info(f"Requesting: {req.method} {req.url}")
|
||||
response = await handler(req)
|
||||
logging.info(f"Received: {response.status} for {req.method} {req.url}")
|
||||
return response
|
||||
|
||||
async def main_with_middleware():
|
||||
async with ClientSession(middlewares=(logging_middleware,)) as session:
|
||||
await fetch_json(session, "http://httpbin.org/get")
|
||||
await post_json(session, "http://httpbin.org/post", {"key": "value"})
|
||||
|
||||
# Example usage with the `fetch_json` and `post_json` helpers from above.
|
||||
```
|
||||
Reference in New Issue
Block a user