Skip to main content

Django integration for APIDOG - Export, sync, and manage OpenAPI schemas

Project description

ennam-django-apidog

PyPI version Python versions Django versions License: MIT

Django integration for APIDOG - Export, sync, and manage OpenAPI schemas between Django REST Framework and APIDOG Cloud.

Features

  • Export OpenAPI Schema - Generate OpenAPI 3.0 schemas from your Django REST Framework APIs
  • Sync with APIDOG Cloud - Push and pull schemas to/from APIDOG Cloud
  • Compare Schemas - Compare local schemas with cloud versions
  • Environment Management - Generate environment configurations for different deployments
  • Schema Hooks - Custom drf-spectacular extensions for handling edge cases
  • Templates - Ready-to-use Makefile, Docker Compose, and configuration files

Installation

pip install ennam-django-apidog

Quick Start

1. Add to INSTALLED_APPS

# settings.py
INSTALLED_APPS = [
    ...
    'rest_framework',
    'drf_spectacular',
    'ennam_django_apidog',
]

2. Configure DRF Spectacular

# settings.py
REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

SPECTACULAR_SETTINGS = {
    'TITLE': 'Your API',
    'DESCRIPTION': 'Your API description',
    'VERSION': '1.0.0',
    'SERVE_INCLUDE_SCHEMA': False,
}

3. Add URL Routes

# urls.py
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView

urlpatterns = [
    ...
    path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
    path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
]

4. Initialize APIDOG

python manage.py apidog init

5. Export Schema

python manage.py apidog export

Configuration

Configure APIDOG settings in your Django settings.py:

APIDOG_SETTINGS = {
    # Output directory for schemas (default: apidog/ at project root)
    'OUTPUT_DIR': None,

    # Schema endpoint (default: /api/schema/)
    'SCHEMA_ENDPOINT': '/api/schema/',

    # APIDOG Cloud credentials
    'PROJECT_ID': 'your-project-id',  # or use env var APIDOG_PROJECT_ID
    'TOKEN': 'your-api-token',        # or use env var APIDOG_TOKEN

    # API configuration
    'API_VERSION': '2024-03-28',
    'API_BASE_URL': 'https://api.apidog.com/v1',
    'TIMEOUT': 60,

    # Environment configurations
    'ENVIRONMENTS': {
        'local': {
            'name': 'Local Development',
            'base_url': 'http://localhost:8000',
        },
        'production': {
            'name': 'Production',
            'base_url': 'https://api.yourapp.com',
        },
    },
}

Or use environment variables:

export APIDOG_PROJECT_ID="your-project-id"
export APIDOG_TOKEN="your-api-token"

Commands

Initialize Project

# Create apidog directory and templates
python manage.py apidog init

# Force overwrite existing files
python manage.py apidog init --force

Export Schema

# Export as JSON (default)
python manage.py apidog export

# Export as YAML
python manage.py apidog export --format yaml

# Custom output directory
python manage.py apidog export --output /path/to/output/

Validate Schema

# Validate latest schema
python manage.py apidog validate

# Validate specific file
python manage.py apidog validate --file /path/to/schema.json

Push to APIDOG Cloud

# Push latest schema
python manage.py apidog push

# Push specific file
python manage.py apidog push --file /path/to/schema.json

Pull from APIDOG Cloud

# Pull to default location
python manage.py apidog pull

# Pull to specific file
python manage.py apidog pull --output /path/to/output.json

Compare Schemas

# Compare local with cloud
python manage.py apidog compare

Generate Environment Config

python manage.py apidog env-config

Using Schema Hooks

Add custom schema hooks to handle edge cases:

# settings.py
SPECTACULAR_SETTINGS = {
    ...
    'PREPROCESSING_HOOKS': [
        'ennam_django_apidog.schema_hooks.preprocess_exclude_problematic_views',
    ],
    'EXTENSIONS': [
        'ennam_django_apidog.schema_hooks.BaseSerializerExtension',
    ],
}

Makefile Commands

After running apidog init, use the Makefile for shortcuts:

# Show help
make -f Makefile.apidog help

# Export schema
make -f Makefile.apidog export

# Push to cloud
make -f Makefile.apidog push

# Compare with cloud
make -f Makefile.apidog compare

# Export and push
make -f Makefile.apidog sync

Docker Support

Use the generated Docker Compose file for mock server:

# Start mock server
docker-compose -f docker-compose.apidog.yml up -d apidog-mock

# Mock server available at http://localhost:4010

CI/CD Integration

Example GitHub Actions workflow:

- name: Export OpenAPI Schema
  run: |
    python manage.py apidog export --format json

- name: Push to APIDOG
  env:
    APIDOG_PROJECT_ID: ${{ secrets.APIDOG_PROJECT_ID }}
    APIDOG_TOKEN: ${{ secrets.APIDOG_TOKEN }}
  run: |
    python manage.py apidog push

Documentation

For full documentation, see docs/GUIDE.md.

Testing

Running Tests

# Install test dependencies first
pip install -e ".[dev]"

# Run all tests with coverage
pytest --cov=src --cov-report=term

# Run specific test file
pytest tests/test_commands.py -v

# Run specific test
pytest tests/test_commands.py::TestApidogCommand::test_export_command -v

# Run only HTTP mocking tests
pytest tests/test_commands_http.py -v

# Run only edge case tests
pytest tests/test_commands_errors.py -v

# Generate HTML coverage report
pytest --cov=src --cov-report=html
# Open htmlcov/index.html to view coverage

Test Coverage

Current coverage: 40-50% focusing on critical functionality:

HTTP Mocking Tests (14 tests):

  • ✅ Push schema to APIDOG (success, auth, timeout, no credentials)
  • ✅ Pull schema from APIDOG (success, auth, 404, 500 errors)
  • ✅ Compare local vs cloud schemas

Edge Case Tests (17 tests):

  • ✅ Export command: directory creation, formats, indentation
  • ✅ Validate command: invalid JSON, missing fields
  • ✅ Init command: setup, gitignore, readme, overwrite
  • ✅ Environment configuration

Settings & Credentials Tests (7 tests):

  • ✅ Configuration hierarchy (settings → env → defaults)
  • ✅ Credential resolution with overrides
  • ✅ Settings reload and caching

All tests use HTTP mocking via responses library - no real API calls during testing.

Development Setup

# Install development dependencies
pip install -e ".[dev]"

# Install pre-commit hooks
pre-commit install

# Run linting
ruff check src/ tests/
ruff format src/ tests/

# Run type checking
mypy src/

# Run all pre-commit checks
pre-commit run --all-files

Pre-commit Hooks

This project uses pre-commit hooks for quality assurance:

  • ruff: Code linting and formatting
  • mypy: Static type checking with Django stubs
  • Standard checks: YAML/TOML validation, trailing whitespace, etc.

Hooks run automatically on git commit. The same checks run in CI.

Release Checklist

Before publishing to PyPI:

  • All tests pass: pytest --cov=src
  • Coverage ≥ 40%: pytest --cov=src --cov-report=term
  • No mypy errors: mypy src/
  • Linting passes: ruff check src/ tests/
  • Pre-commit passes: pre-commit run --all-files
  • CI passes on all Python/Django versions
  • Version bumped in pyproject.toml
  • CHANGELOG.md updated
  • Test build: python -m build && twine check dist/*

Continuous Integration

Tests run on GitHub Actions with:

  • Python versions: 3.8, 3.9, 3.10, 3.11, 3.12
  • Django versions: 3.2, 4.0, 4.1, 4.2, 5.0

See .github/workflows/test.yml for details.

Requirements

  • Python >= 3.8
  • Django >= 3.2
  • Django REST Framework >= 3.12
  • drf-spectacular >= 0.26

License

MIT License - see LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ennam_django_apidog-0.1.1.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ennam_django_apidog-0.1.1-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file ennam_django_apidog-0.1.1.tar.gz.

File metadata

  • Download URL: ennam_django_apidog-0.1.1.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for ennam_django_apidog-0.1.1.tar.gz
Algorithm Hash digest
SHA256 13fba5fc112306306f9382d51a3e510d32bcaf22662a6cc25834d8281ee0e7c8
MD5 85f21c458bfb9b0c36f6a0c38371d28a
BLAKE2b-256 0014cab1d835a92a4d823610c66ddf2e7bc5ab7176c769d1a12541b4ddb014d2

See more details on using hashes here.

File details

Details for the file ennam_django_apidog-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ennam_django_apidog-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4e3efa7c247fdadb01e3cb57d14e2309d80fb763d5ead4fc2da78934752eb80f
MD5 766bd4ebe80c1e64ac09c209373c83ab
BLAKE2b-256 d336bd07c2b3b8fc155855d3aadd9374370b57baa808e39ddd79bf1afc3130bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page