RESTful API built with FastAPI - scope: transactions & user interactions
- Python 3.10+
- pip
- Virtualenv
- Docker
-
Clone the repository:
git clone <repository-url> cd fido-transaction-api
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Create a .env file refer to .evn.example:
touch .evn
-
Install dependencies from the requirements.txt file:
pip install -r requirements.txt
-
Run docker compose:
docker compose up --build
-
API should be running on:
http://0.0.0.0:8000 # http://localhost:8000/
-
RESTful API: The API follows REST principles, making it intuitive and easy to use.
-
Status Codes: Proper HTTP status codes are used to indicate the result of an API request (e.g., 200 for success, 404 for not found, 500 for server error).
- Pydantic Models: Used Pydantic models for data validation and serialization. This ensures that the data conforms to the expected schema before processing.
- Usage:
TransactionModel
,AnalyticsModel
.
- Usage:
- MongoDB: Utilized MongoDB for its flexibility and scalability.
- AsyncIO Motor Client: Used
motor.motor_asyncio.AsyncIOMotorClient
for asynchronous database operations, improving the performance of the API. - Database Session Manager: Implemented a session manager to handle database connections efficiently.
- Usage:
MongoDBSessionManager
.
- Usage:
- AsyncIO Motor Client: Used
- Redis: Integrated Redis for caching frequently accessed data to reduce database load and improve response times. Implemented strategies of cache updates and invalidation.
- Usage:
redis_client
.
- Usage:
- Custom Exceptions: Defined custom exceptions to handle specific error scenarios gracefully.
- Usage:
ServiceError
,EntityDoesNotExistError
.
- Usage:
- Exception Handlers: Registered exception handlers to return meaningful error messages to the client.
- Usage:
service_error_handler
.
- Usage:
- Loguru: Used Loguru for logging, providing better insights into the application's behavior and aiding in debugging.
- Usage:
logger
.
- Usage:
- FastAPI Background Tasks: Utilized FastAPI's background tasks to handle operations that do not need to block the main request-response cycle.
- Usage:
update_user_statistics
.
- Usage:
- Analytics Computation Scheduler: Implemented a scheduler to periodically compute and store analytics data.
- Usage:
start_scheduler
.
- Usage:
- Environment Variables: Managed configuration using environment variables to keep sensitive information secure and make the application configurable.
- Strategy: Implement pagination for endpoints that return large datasets.
- Trade-offs:
- Pros: Reduces server load and improves response times.
- Cons: Adds complexity to the API and client-side code.
- Strategy: Use indexing on frequently queried fields to speed up database operations.
- Trade-offs:
- Pros: Significantly improves query performance.
- Cons: Increases storage requirements and can slow down write operations.
- Strategy: Implement an event-driven architecture to decouple services and handle asynchronous tasks.
- Trade-offs:
- Pros: Improves scalability and fault tolerance.
- Cons: Adds complexity to the system and requires robust event management.
- Strategy: Increase the scope of testing to include integration, performance, and stress tests.
- Trade-offs:
- Pros: Ensures the reliability and performance of the application under various conditions.
- Cons: Requires more resources and time to implement and maintain.
- Strategy: Use Kubernetes (K8s) for resilient and scalable deployments.
- Trade-offs:
- Pros: Provides automated scaling, self-healing, and efficient resource management.
- Cons: Adds complexity to the deployment process and requires expertise in Kubernetes.