|
| 1 | +# Publishing & Automating the Generated Client |
| 2 | + |
| 3 | +This document outlines how to publish the **generated API client** (the output of the `cdd sync` command) and how to keep it automatically synchronized with your upstream server's OpenAPI specification. |
| 4 | + |
| 5 | +## 1. Packaging the Generated Client |
| 6 | + |
| 7 | +When `cdd sync` produces a `client.py` and related models, you need to wrap them in a package before distributing. A standard minimal Python package structure looks like this: |
| 8 | + |
| 9 | +``` |
| 10 | +my-api-client/ |
| 11 | +├── pyproject.toml |
| 12 | +├── README.md |
| 13 | +└── src/ |
| 14 | + └── my_api_client/ |
| 15 | + ├── __init__.py |
| 16 | + └── client.py <-- Generated by CDD |
| 17 | +``` |
| 18 | + |
| 19 | +### Example `pyproject.toml` |
| 20 | +Create a `pyproject.toml` in your client repository: |
| 21 | + |
| 22 | +```toml |
| 23 | +[build-system] |
| 24 | +requires = ["hatchling"] |
| 25 | +build-backend = "hatchling.build" |
| 26 | + |
| 27 | +[project] |
| 28 | +name = "my-api-client" |
| 29 | +version = "1.0.0" |
| 30 | +description = "Auto-generated OpenAPI client" |
| 31 | +dependencies = [ |
| 32 | + "pydantic>=2.0", |
| 33 | + "urllib3" |
| 34 | +] |
| 35 | + |
| 36 | +[tool.hatch.build.targets.wheel] |
| 37 | +packages = ["src/my_api_client"] |
| 38 | +``` |
| 39 | + |
| 40 | +## 2. Publishing the Client to PyPI |
| 41 | + |
| 42 | +To manually publish the generated client to the Python Package Index: |
| 43 | + |
| 44 | +```bash |
| 45 | +cd my-api-client |
| 46 | +pip install build twine |
| 47 | +python -m build |
| 48 | +python -m twine upload dist/* |
| 49 | +``` |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## 3. Automating Updates via GitHub Actions |
| 54 | + |
| 55 | +The power of CDD is ensuring your client SDK is never out of sync with your API server. You can use a GitHub Actions Cron Job to routinely check your upstream OpenAPI server, sync the models, and publish a new version. |
| 56 | + |
| 57 | +### Setup the GitHub Action Workflow |
| 58 | + |
| 59 | +Create a file in your client repository at `.github/workflows/sync-and-publish.yml`: |
| 60 | + |
| 61 | +```yaml |
| 62 | +name: Sync OpenAPI Client |
| 63 | + |
| 64 | +on: |
| 65 | + schedule: |
| 66 | + # Run every day at midnight UTC |
| 67 | + - cron: '0 0 * * *' |
| 68 | + workflow_dispatch: # Allow manual trigger via GitHub UI |
| 69 | + |
| 70 | +jobs: |
| 71 | + sync_and_publish: |
| 72 | + runs-on: ubuntu-latest |
| 73 | + steps: |
| 74 | + - name: Checkout Client Repository |
| 75 | + uses: actions/checkout@v4 |
| 76 | + |
| 77 | + - name: Set up Python |
| 78 | + uses: actions/setup-python@v5 |
| 79 | + with: |
| 80 | + python-version: '3.12' |
| 81 | + |
| 82 | + - name: Install dependencies |
| 83 | + run: | |
| 84 | + python -m pip install --upgrade pip |
| 85 | + pip install cdd-python-client build twine bumpver |
| 86 | +
|
| 87 | + - name: Fetch Latest OpenAPI Spec |
| 88 | + # Replace the URL with your server's live openapi.json endpoint |
| 89 | + run: curl -s https://api.my-service.com/openapi.json > openapi.json |
| 90 | + |
| 91 | + - name: Sync Client Code |
| 92 | + # This updates the `src/my_api_client` files based on the fetched JSON |
| 93 | + run: cdd sync --from-openapi openapi.json --to-python src/my_api_client/ |
| 94 | + |
| 95 | + - name: Check for changes |
| 96 | + id: git-check |
| 97 | + run: | |
| 98 | + git diff --exit-code || echo "changes=true" >> $GITHUB_OUTPUT |
| 99 | +
|
| 100 | + - name: Bump Version and Commit |
| 101 | + if: steps.git-check.outputs.changes == 'true' |
| 102 | + run: | |
| 103 | + git config --global user.name "github-actions[bot]" |
| 104 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 105 | + |
| 106 | + # Automatically bump the patch version in pyproject.toml |
| 107 | + bumpver update --patch |
| 108 | + |
| 109 | + git add src/my_api_client/ openapi.json pyproject.toml |
| 110 | + git commit -m "chore(openapi): auto-sync client with upstream spec" |
| 111 | + git push |
| 112 | +
|
| 113 | + - name: Build and Publish to PyPI |
| 114 | + if: steps.git-check.outputs.changes == 'true' |
| 115 | + env: |
| 116 | + TWINE_USERNAME: __token__ |
| 117 | + TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} |
| 118 | + run: | |
| 119 | + python -m build |
| 120 | + twine upload dist/* |
| 121 | +``` |
| 122 | +
|
| 123 | +### How it Works: |
| 124 | +1. **Cron Schedule**: The action runs daily at midnight (or manually). |
| 125 | +2. **Fetch**: It pulls the absolute latest `openapi.json` from the upstream server. |
| 126 | +3. **CDD Sync**: It runs the `cdd sync` CLI tool to regenerate the client, mock, and test files using the new spec. |
| 127 | +4. **Diff Check**: It uses `git diff` to see if the generated AST code differs from what is currently checked in. |
| 128 | +5. **Bump & Commit**: If the spec changed, it bumps the version in `pyproject.toml` and commits the code. |
| 129 | +6. **Publish**: It builds the new wheel and uploads it to PyPI automatically (requires a `PYPI_API_TOKEN` secret to be set in your GitHub repository settings). |
0 commit comments