Complete API reference for DataSentinel framework and generated services.
python auto_sentinel.py --api <SOURCE> [OPTIONS]| Argument | Description | Example |
|---|---|---|
--api |
API source (URL, file path, or endpoint) | --api ./spec.yaml |
| Argument | Short | Description | Default |
|---|---|---|---|
--output |
-o |
Output directory | ./generated |
--format |
-f |
Input format (json, openapi, graphql) |
Auto-detect |
--verbose |
-v |
Enable verbose logging | False |
--dry-run |
Show what would be generated | False |
|
--log-file |
Write logs to file | None | |
--version |
Show version and exit |
| Argument | Description | Example |
|---|---|---|
--auth-type |
Authentication type | bearer, api-key, basic, oauth2 |
--auth-token |
Authentication token or API key | --auth-token YOUR_TOKEN |
--auth-header |
Custom authentication header name | --auth-header X-API-Key |
--auth-username |
Username for basic auth | --auth-username user |
| Argument | Description |
|---|---|
--skip-models |
Skip models.py generation |
--skip-validators |
Skip validators.py generation |
--skip-tests |
Skip test_api.py generation |
--skip-app |
Skip app.py generation |
--skip-docs |
Skip documentation generation |
--skip-docker |
Skip Dockerfile generation |
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Generation failed |
# Basic usage
python auto_sentinel.py --api ./spec.yaml
# With authentication
python auto_sentinel.py \
--api https://api.example.com/openapi.json \
--auth-type bearer \
--auth-token $TOKEN
# Dry run
python auto_sentinel.py --api ./spec.yaml --dry-run
# Verbose mode
python auto_sentinel.py --api ./spec.yaml --verbose
# Skip specific generators
python auto_sentinel.py \
--api ./spec.yaml \
--skip-tests \
--skip-dockerAbstract base class for API providers with retry and authentication.
from core.base_provider import BaseProvider
class MyProvider(BaseProvider):
async def fetch(self, url: str, **kwargs) -> dict:
"""Fetch data from URL with retry and auth."""
return await super().fetch(url, **kwargs)Fetch data from URL with automatic retry and authentication.
Parameters:
url(str): URL to fetch**kwargs: Additional arguments for httpx
Returns:
dict: Response data
Raises:
NetworkError: On network failuresAuthenticationError: On auth failures
Introspect an API endpoint.
Parameters:
url(str): Endpoint URL
Returns:
dict: Endpoint metadata
Provides retry logic with exponential backoff.
from core.retry_handler import retry_with_backoff
@retry_with_backoff(max_retries=3, backoff_factor=2.0)
async def my_function():
"""Function with automatic retry."""
passParameters:
max_retries(int): Maximum retry attempts (default: 3)backoff_factor(float): Backoff multiplier (default: 2.0)max_backoff(float): Maximum backoff time (default: 60.0)jitter(bool): Add random jitter (default: True)
Manages authentication strategies.
from core.auth_manager import AuthManager, AuthType
# Create auth manager
auth = AuthManager.create(
auth_type=AuthType.BEARER,
token="your-token"
)
# Inject auth into headers
headers = auth.inject_auth({})Factory method to create auth handler.
Parameters:
auth_type(AuthType): Type of authentication**kwargs: Auth-specific parameters
Returns:
AuthHandler: Configured auth handler
from core.exceptions import (
DataSentinelError,
ConfigurationError,
ParsingError,
GenerationError,
ValidationFailedError,
NetworkError,
AuthenticationError
)DataSentinelError (base)
├── ConfigurationError
├── ParsingError
├── GenerationError
├── ValidationFailedError
├── NetworkError
└── AuthenticationError
Abstract base class for all parsers.
from parsers.base_parser import BaseParser
class MyParser(BaseParser):
async def parse(self) -> APISchema:
"""Parse input and return normalized schema."""
passInfers schema from JSON samples.
from parsers.json_inference_parser import JSONInferenceParser
parser = JSONInferenceParser("./sample.json")
schema = await parser.parse()JSONInferenceParser(source: str)Parameters:
source(str): File path or URL to JSON
Parse JSON and infer schema.
Returns:
APISchema: Normalized API schema
Parses OpenAPI/Swagger specifications.
from parsers.openapi_parser import OpenAPIParser
parser = OpenAPIParser("./openapi.yaml")
schema = await parser.parse()OpenAPIParser(source: str)Parameters:
source(str): File path or URL to OpenAPI spec
Parse OpenAPI specification.
Returns:
APISchema: Normalized API schema
Features:
- Resolves $ref references
- Supports OpenAPI 3.x and Swagger 2.0
- Extracts validation constraints
Parses GraphQL schemas via introspection.
from parsers.graphql_parser import GraphQLParser
parser = GraphQLParser("https://api.example.com/graphql")
schema = await parser.parse()GraphQLParser(endpoint: str, auth_token: Optional[str] = None)Parameters:
endpoint(str): GraphQL endpoint URLauth_token(Optional[str]): Authentication token
Execute introspection and parse schema.
Returns:
APISchema: Normalized API schema
Abstract base class for all generators.
from generators.base_generator import BaseGenerator
class MyGenerator(BaseGenerator):
def generate(self) -> Path:
"""Generate artifact."""
passGenerates Pydantic v2 models.
from generators.models_generator import ModelsGenerator
generator = ModelsGenerator(api_schema, output_dir)
models_file = generator.generate()ModelsGenerator(api_schema: APISchema, output_dir: Path)Generate models.py file.
Returns:
Path: Path to generated file
Generates validation logic.
from generators.validators_generator import ValidatorsGenerator
generator = ValidatorsGenerator(api_schema, output_dir)
validators_file = generator.generate()Generates pytest test suite.
from generators.tests_generator import TestsGenerator
generator = TestsGenerator(api_schema, output_dir)
tests_file = generator.generate()Generates FastAPI application.
from generators.app_generator import AppGenerator
generator = AppGenerator(api_schema, output_dir)
app_file = generator.generate()Generates documentation.
from generators.docs_generator import DocsGenerator
generator = DocsGenerator(api_schema, output_dir)
docs_file = generator.generate()Generates Docker configuration.
from generators.dockerfile_generator import DockerfileGenerator
generator = DockerfileGenerator(api_schema, output_dir)
dockerfile = generator.generate()Generated Pydantic models with validation.
from models import User
# Create instance
user = User(
id=1,
name="John Doe",
email="john@example.com"
)
# Validate data
try:
user = User(**data)
except ValidationError as e:
print(e.errors())
# Serialize
json_str = user.model_dump_json()
dict_data = user.model_dump()
# Deserialize
user = User.model_validate_json(json_str)
user = User.model_validate(dict_data)Generated validators with retry and drift detection.
from validators import UserValidator
# Initialize
validator = UserValidator(
base_url="https://api.example.com",
auth_token="your-token"
)
# Validate single record
result = await validator.validate_user(data)
# Batch validation
results = await validator.validate_batch([data1, data2])
# Drift detection
drift_result = await validator.detect_drift("/users/1")class ValidationResult(BaseModel):
success: bool
data: Optional[Dict[str, Any]]
errors: List[str]
warnings: List[str]
timestamp: datetime
drift_detected: boolGenerated FastAPI application endpoints.
GET /healthResponse:
{
"status": "healthy",
"service": "validator"
}POST /validate
Content-Type: application/json
{
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}Response:
{
"success": true,
"data": {...},
"errors": [],
"warnings": [],
"timestamp": "2024-01-15T10:30:00Z",
"drift_detected": false
}POST /validate/batch
Content-Type: application/json
[
{"id": 1, "name": "John"},
{"id": 2, "name": "Jane"}
]Response:
[
{
"success": true,
"data": {...},
"errors": []
},
{
"success": true,
"data": {...},
"errors": []
}
]GET /drift/users/1Response:
{
"success": true,
"drift_detected": false,
"warnings": [],
"errors": []
}Configuration via environment variables or .env file.
from config.settings import Settings
settings = Settings()| Variable | Description | Default |
|---|---|---|
LOG_LEVEL |
Logging level | INFO |
LOG_FORMAT |
Log format (json or text) |
json |
DEFAULT_TIMEOUT |
HTTP timeout (seconds) | 30 |
MAX_RETRIES |
Maximum retry attempts | 3 |
RETRY_BACKOFF_FACTOR |
Backoff multiplier | 2.0 |
OUTPUT_DIR |
Default output directory | ./output |
TEMPLATE_DIR |
Templates directory | ./templates |
Configure logging with Loguru.
from config.logging_config import setup_logging
# Setup logging
setup_logging(
level="DEBUG",
log_file="app.log"
)
# Use logger
from loguru import logger
logger.info("Message")
logger.error("Error message")
logger.debug("Debug message")Main schema representing the API.
class APISchema(BaseModel):
title: str
version: str
description: Optional[str]
base_url: str
models: Dict[str, ModelSchema]
endpoints: List[Endpoint]Represents a data model.
class ModelSchema(BaseModel):
name: str
description: Optional[str]
fields: List[FieldSchema]Represents a model field.
class FieldSchema(BaseModel):
name: str
type: FieldType
required: bool
description: Optional[str]
validators: List[Validator]
default: Optional[Any]Represents an API endpoint.
class Endpoint(BaseModel):
path: str
method: str
operation_id: str
description: Optional[str]
parameters: List[Parameter]
request_body: Optional[ModelSchema]
response: Optional[ModelSchema]Supported field types:
| Type | Python Type | Pydantic Type |
|---|---|---|
string |
str |
str |
integer |
int |
int |
number |
float |
float |
boolean |
bool |
bool |
array |
List |
List[T] |
object |
Dict |
Nested model |
email |
str |
EmailStr |
url |
str |
HttpUrl |
uuid |
str |
UUID |
datetime |
datetime |
datetime |
date |
date |
date |
Supported validation types:
| Validator | Description | Example |
|---|---|---|
min_length |
Minimum string length | min_length=1 |
max_length |
Maximum string length | max_length=100 |
minimum |
Minimum numeric value | ge=0 |
maximum |
Maximum numeric value | le=150 |
pattern |
Regex pattern | pattern=r'^[A-Z]' |
email |
Email format | EmailStr |
url |
URL format | HttpUrl |
uuid |
UUID format | UUID |
All errors follow a consistent format:
{
"error": {
"type": "ValidationError",
"message": "Validation failed",
"details": [
{
"field": "email",
"message": "Invalid email format"
}
]
}
}| Code | Meaning |
|---|---|
| 200 | Success |
| 201 | Created |
| 400 | Bad Request (validation error) |
| 401 | Unauthorized |
| 404 | Not Found |
| 422 | Unprocessable Entity |
| 500 | Internal Server Error |
Always use type hints for better IDE support:
from typing import List, Optional, Dict, Any
async def validate_data(data: Dict[str, Any]) -> ValidationResult:
passAlways handle exceptions gracefully:
try:
result = await validator.validate_user(data)
except ValidationError as e:
logger.error(f"Validation failed: {e}")
except NetworkError as e:
logger.error(f"Network error: {e}")Leverage async for I/O operations:
async def process_batch(items: List[Dict]) -> List[ValidationResult]:
tasks = [validate_item(item) for item in items]
return await asyncio.gather(*tasks)Use structured logging:
logger.info("Validation started", extra={
"user_id": user_id,
"timestamp": datetime.now()
})import asyncio
from parsers.openapi_parser import OpenAPIParser
from generators.models_generator import ModelsGenerator
from generators.app_generator import AppGenerator
async def main():
# Parse OpenAPI spec
parser = OpenAPIParser("./openapi.yaml")
schema = await parser.parse()
# Generate models
models_gen = ModelsGenerator(schema, Path("./output"))
models_file = models_gen.generate()
# Generate FastAPI app
app_gen = AppGenerator(schema, Path("./output"))
app_file = app_gen.generate()
print(f"Generated: {models_file}, {app_file}")
if __name__ == "__main__":
asyncio.run(main())- 📖 Getting Started - Quick start guide
- 🎯 Input Formats - Supported input formats
- 🚀 Deployment - Deploy your service
- 📝 GitHub Issues
- 💬 Discussions
- 📧 Email: support@datasentinel.dev