This tool downloads the OpenAPI v3 specifications for the Iconik API and generates Pydantic v2 models based on the component schemas.
- Downloads all Iconik API specifications
- Generates Pydantic v2 models for each specification
- Resolves schema references
- Handles complex types (oneOf, anyOf)
- Sorts models by dependency
- Creates a well-structured Python package
- Automatically formats generated code (optional)
- Python 3.9 or later
- Required packages:
requests: API specification acquisitionjinja2: Template processingpydantic(v2): Model generation
- Optional formatting packages:
pycln: Import optimizationisort: Import organizationyapf: Code style normalization
# Repository acquisition
git clone https://github.com/briansumma/iconik-api-models-generator.git
cd iconik-api-models-generator
# Core dependency installation
pip install -e .
# Complete development environment
pip install -e ".[dev]"
# Formatting tools only
pip install -e ".[formatters]"
generate-iconik-models
The tool accepts the following command-line parameters:
| Option | Syntax | Purpose |
|---|---|---|
| Help | -h, --help |
Displays usage information and available options |
| Version | --version |
Shows program's version number and exits |
| Output Directory | -o OUTPUT_DIR, --output-dir OUTPUT_DIR |
Specifies target location for generated models (default: models) |
| Code Formatting | -F, --format-code |
Applies automated formatting using pycln, isort, and yapf |
| Preserve Downloads | --keep-downloads |
Retains downloaded specification files after processing |
| Debug Mode | --debug |
Enables verbose diagnostic logging for troubleshooting |
-
API Specification Acquisition
- Downloads latest OpenAPI v3 specifications from Iconik API endpoints
- Validates specification format compatibility
-
Schema Extraction
- Parses component schemas from specification documents
- Resolves reference dependencies between schemas
-
Model Generation
- Converts OpenAPI schemas to Pydantic v2 model classes
- Applies Python typing system for property validation
-
Package Structure Creation
- Organizes models into logical API-aligned modules
- Generates appropriate import statements and package hierarchy
-
Code Optimization (when using
--format-code)- Removes unused imports with
pycln - Sorts import statements with
isort - Eliminates trailing whitespace with
sed - Normalizes code formatting with
yapf
- Removes unused imports with
The output directory structure varies depending on command-line parameters:
models/
├── __init__.py
├── acls.py
├── assets.py
├── auth.py
├── automations.py
├── files.py
├── jobs.py
├── metadata.py
├── notifications.py
├── search.py
├── settings.py
├── stats.py
├── transcode.py
├── users_notifications.py
└── users.py
models/
├── __init__.py
├── _specs/
│ ├── acls.json
│ ├── assets.json
│ ├── auth.json
│ ├── automations.json
│ ├── files.json
│ ├── jobs.json
│ ├── metadata.json
│ ├── notifications.json
│ ├── search.json
│ ├── settings.json
│ ├── stats.json
│ ├── transcode.json
│ ├── users-notifications.json
│ └── users.json
├── acls.py
├── assets.py
├── auth.py
├── automations.py
├── files.py
├── jobs.py
├── metadata.py
├── notifications.py
├── search.py
├── settings.py
├── stats.py
├── transcode.py
├── users_notifications.py
└── users.py
Each Python module contains Pydantic models corresponding to the
associated Iconik API specification. When the --keep-downloads flag is
specified, the original JSON specification files are preserved in the
_specs/ directory for reference. This is useful if you're using an IDE
that can render OpenAPI definitions in JSON files, providing convenient
visualization and exploration of the API schema during development.
Once you've generated the models, you can use them in your code:
from models import assets, files
# Create an asset
asset = assets.AssetCreateSchema(
title="My Asset",
external_id="ext-123",
type="ASSET"
)
# Create a file
file = files.FileCreateSchema(
directory_path="/path/to/dir",
original_name="my_file.mp4",
type="FILE"
)
The --format-code option applies a comprehensive formatting workflow:
- Import optimization with
pycln - Import organization with
isort - Whitespace normalization with
sed - Code structure standardization with
yapf
# Verify code quality with pylint
pylint --errors-only --rcfile pyproject.toml --recursive yes models/
# Full quality assessment
pylint --rcfile pyproject.toml --recursive yes models/
Different code formatters offer varying formatting approaches:
| Tool | Installation | Usage | Characteristics |
|---|---|---|---|
| Black | pip install black |
black models/ |
Strict, deterministic formatting |
| autopep8 | pip install autopep8 |
autopep8 --in-place --recursive --aggressive --aggressive models/ |
Incremental PEP 8 compliance |
| Ruff | pip install ruff |
ruff format models/ |
High-performance formatter |
| Tool | Installation | Usage | Purpose |
|---|---|---|---|
| Flake8 | pip install flake8 |
flake8 models/ |
Style guide enforcement |
| Ruff | pip install ruff |
ruff check --fix models/ |
Fast linting with autofix |
The sed (stream editor) implementation varies between POSIX variants,
requiring different syntax for in-place editing operations:
| System Type | Command Pattern | Example |
|---|---|---|
| BSD/macOS | sed -i '<suffix>' -e '<pattern>' |
sed -i '' -e 's/[[:space:]]*$//' models/*.py |
| GNU/Linux | sed -i[<suffix>] -e '<pattern>' |
sed -i -e 's/[[:space:]]*$//' models/*.py |
- The
-iflag enables in-place editing of files - BSD implementations require an argument (use empty string
''for no backup) - GNU implementations accept an optional suffix parameter
- The pattern
's/[[:space:]]*$//'removes trailing whitespace from each line
For optimal code quality, apply formatters in this sequence:
# 1. Remove unused imports
pycln --all models/
# 2. Sort imports
isort models/
# 3. Format code style (choose one)
black -l 88 models/ # Option 1: Black formatter
# or
yapf -i models/*.py # Option 2: YAPF formatter
# 4. Lint and fix remaining issues
ruff check --fix models/
# 5. Final verification
pylint --rcfile pyproject.toml models/
This multi-stage process ensures both proper import organization and consistent code style.
If you encounter implementation issues:
-
Environment Verification
- Ensure Python 3.9+ is correctly installed and active
- Verify all dependencies are properly installed
- Confirm network connectivity for API specification download
-
Formatting Issues
- Install formatting tools:
pip install -e ".[formatters]" - Try manual formatting procedures (see section above)
- Verify tool compatibility with your Python version
- Install formatting tools:
-
Generation Failures
- Check for API specification changes
- Examine error messages for specific schema incompatibilities
- Ensure sufficient disk space for generated files
MIT