How zmievsa/cadwyn works
Project Type & Purpose
• Cadwyn is not a typical full-stack web application; instead, it is a library/framework aimed at managing API versioning. Its purpose is to allow you to support a single codebase while automatically generating routes and schemas for older API versions. This is similar to the “Stripe-like” API versioning strategy.
Key Components from the File Structure
• Core Package (cadwyn):
◦ __init__.py and __main__.py – Entry and initialization of the library.
◦ Modules such as _asts.py, _importer.py, _render.py, _utils.py – Likely provide lower-level mechanisms for parsing, importing, rendering and general utilities that support API versioning.
◦ applications.py, changelogs.py, dependencies.py, exceptions.py – Represent the business logic layer that handles applications, schema changes, dependency injection, and error handling.
◦ middleware.py – A dedicated module for request/response middleware likely intercepting versioning logic.
◦ route_generation.py and routing.py – Responsible for generating routes dynamically based on version-specific modules.
◦ schema_generation.py – Handles auto-generation of API schemas (e.g., OpenAPI documentation).
• The cadwyn/structure directory further refines the organization by holding common types, data structures, endpoints, enums, schemas, and versions – this is where the abstracted versioned components are defined.
• The cadwyn/static directory holds static HTML docs – indicating that part of the library’s capability is to automatically generate developer-friendly documentation.
• Documentation files (docs and docs_src) and extensive testing modules (tests) indicate a high degree of support and automation around the core versioning functionality. Additionally, CI/CD workflows in the .github folder stress a production-ready design.
Architectural Relationships & Interactions
◦ Initialization: When Cadwyn is loaded (via __main__ and __init__), it sets up the FastAPI app integration.
◦ Middleware Integration: The middleware module intercepts incoming API requests to determine the API version based on context variables (as seen in _internal/context_vars.py) and delegates the request to the appropriate version module.
◦ Route & Schema Generation: Once a request is intercepted, the routing and route_generation modules work together to dynamically generate and dispatch the right endpoint. This process will use the schema_generation module to ensure the correct API documentation and request/response formats.
◦ Versioning Structure: The structure folder (common, endpoints, enums, schemas, versions, etc.) defines how the various API versions are maintained. Each version module is encapsulated in “version change” modules so that business logic stays decoupled from version-specific alterations.
◦ Exception & Dependency Management: Dependencies and exceptions are centrally managed (dependencies.py and exceptions.py) to support smooth operation and consistent error handling across API versions.
◦ Code Maintenance and Backporting: Modules like applications.py and changelogs.py track API version changes and apply migration-based response building, which is critical to enabling backwards compatibility.
Architectural Patterns and Guiding Principles
• The design leverages modularity: Each functional aspect (routing, schema generation, middleware, version structure) is separated into its own module.
• The approach resembles an MVC (Model-View-Controller) style adapted for API versioning:
◦ “Model” – Data structures in the structure folder (schemas, endpoints, data).
◦ “View” – The rendered documentation and API responses (handled in _render.py and static docs).
◦ “Controller” – The routing, middleware, and version change modules that control request flows.
• Integration with CI/CD, testing suites, and documentation generation suggests a strong emphasis on automation and maintainability.