|
|
||
|---|---|---|
| .. | ||
| README.md | ||
| endpoint_structure_reference.py | ||
README.md
Endpoint Structure Pattern
This document describes the Endpoint Structure pattern used in the EVYOS backend services.
Overview
The Endpoint Structure is a modular, event-driven architecture pattern that provides clear separation of concerns for API services. It consists of three main components:
- Events Component
- Endpoints Component
- Validations Component
This architecture enables scalable, maintainable API services with a consistent structure.
Directory Structure
ApiServices/
└── [ServiceName]/
├── Events/
│ └── [entity]/
│ ├── cluster.py
│ └── supers_events.py
├── Endpoints/
│ └── [entity]/
│ └── route.py
└── Validations/
└── [entity]/
└── [entity]/
└── validations.py
Components
1. Events Component
Located at ApiServices/[ServiceName]/Events/[entity]/
cluster.py
- Organizes events into clusters for routing
- Contains:
RouterCluster: Top-level container for event clustersEventCluster: Groups related events (List, Create, Update)- Each cluster has a unique name and UUID
supers_events.py
- Defines event handlers with business logic
- Contains:
Eventobjects with unique name, key, validators, and description- Callable methods implementing business logic
- Events are registered to their respective clusters
2. Endpoints Component
Located at ApiServices/[ServiceName]/Endpoints/[entity]/
route.py
- Defines FastAPI routes that map to event handlers
- Contains:
- APIRouter with prefix and tags
- Route functions that:
- Extract token information
- Retrieve event codes
- Match to appropriate event cluster
- Call corresponding event handler
3. Validations Component
Located at ApiServices/[ServiceName]/Validations/[entity]/[entity]/
validations.py
- Defines Pydantic models for request/response validation
- Contains:
- Request models with field definitions
- Response models for structured responses
Data Flow
- Request → FastAPI endpoint
- Data validation with Pydantic
- Token extraction and event code retrieval
- Event matching and handler execution
- Business logic execution
- Formatted response return
Implementation Guide
To implement a new endpoint using this structure:
- Create the directory structure for your entity
- Define validation models in
validations.py - Create events and their callable methods in
supers_events.py - Organize events into clusters in
cluster.py - Define API routes in
route.py
Benefits
- Clear separation of concerns
- Consistent structure across services
- Scalable and maintainable architecture
- Flexible event-driven design
- Standardized response format