How abhishek010-5/quiz-api-using-flask works
Project Type and Purpose
• This is a RESTful API built using Flask that serves quiz questions across different collections and topics.
• It focuses on secure access (using API keys), rate limiting, and fetching quiz content from a MongoDB database.
• The primary goal is to provide endpoints that retrieve question data, with built-in error handling and security features.
File Structure
• The top-level "api" directory houses all backend logic:
• app.py – Likely the entry point that initializes the Flask application.
• routes.py – Contains the endpoint definitions (e.g., GET /, GET /get_questions/<collection_name>/<topic>).
• config.py – Manages configuration settings, potentially including API key validation, rate limits, and external resource configurations.
• database.py – Handles connections to MongoDB and database operations.
• Other files such as requirements.txt list dependencies, vercel.json configures deployment (hinting at Vercel deployment), and sample_key.txt provides a sample API key.
• The README outlines installation, usage, API endpoint details, rate limiting, and even provides a sample of the expected JSON schema from the MongoDB collections.
Architecture Components to Highlight
a. API Layer (Flask Application)
• app.py: Acts as the main entry.
• routes.py: Defines endpoints such as the home route and the questions retrieval route.
• API Key Authentication: Middleware or checks (in routes or before_request) ensuring that only valid API key holders can access the endpoints.
• Rate Limiting Module: Implements logic to restrict requests (50 per hour per IP) to protect the API.
b. Configuration Management
• config.py: Central location for configurable settings like rate limits, API key, and secret keys.
• .env File (mentioned in README): Environmental settings for sensitive information such as API_KEY, MongoDB connection URI, DB name, and Flask’s secret key.
c. Database Layer (MongoDB)
• database.py: Manages the connection to MongoDB, handling operations such as connecting to the DB and executing queries.
• Collection Structure: Different collections for topics like "current_affairs", "reasoning", and "aptitude" with a uniform document schema.
d. External Deployment / Configuration
• vercel.json: Configures the deployment environment on Vercel if applicable.
• Requirements and Environment Setup: requirements.txt ensures that the appropriate libraries are in place.
Relationships and Interactions
• The client sends HTTP requests to the Flask server.
• When a request is received:
• The API Endpoint (from routes.py) first validates the API key (using configuration from config.py).
• It then checks rate limit restrictions.
• Upon passing authentication and rate limit checks, the request is forwarded to interact with the database via database.py.
• MongoDB is queried for the required collection and topic. The response is then formatted as JSON and returned.
• Any failures (e.g., unauthorized access, missing data, or internal errors) generate appropriate error messages as depicted in the README.
Architectural Patterns and Principles
• Separation of Concerns: The logic is clearly divided among routing, configuration, and database access.
• Security: API key checks and proper error responses ensure that only valid, authorized requests are processed.
• Scalability Considerations: Although simple, rate limiting is employed to avoid abuse which is critical for public APIs.
• Externalization of Configuration: Using an .env file and config.py makes it easy to change settings without modifying source code.