diff --git a/.github/workflows/test_prod_snippets___prod_api_server___prod_sdk.yml b/.github/workflows/test_prod_snippets___prod_api_server___prod_sdk.yml new file mode 100644 index 00000000..5d05701c --- /dev/null +++ b/.github/workflows/test_prod_snippets___prod_api_server___prod_sdk.yml @@ -0,0 +1,135 @@ +name: test_prod_snippets___prod_api_server___prod_sdk +# not sure how to test release candidate snippets + +on: + pull_request: + branches: [ main ] + push: + branches: + - 'conor/com-5939-create-workflow-for-sdk-snippets-test' + workflow_dispatch: + +jobs: + + discover_sdk_snippets: + runs-on: ubuntu-latest + env: + COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} + + outputs: + items: ${{ steps.collect.outputs.items }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup uv and Python + uses: astral-sh/setup-uv@v5 + with: + python-version: "3.12" + + - name: Install dependencies (uv) + working-directory: v1/test_sdk_snippets + run: uv sync + + - id: collect + name: Collect endpoints (writes items=... to $GITHUB_OUTPUT) + working-directory: v1/test_sdk_snippets + run: uv run python discover_endpoints.py + + - name: Print collected items + run: | + cat > items.json <<'JSON' + ${{ steps.collect.outputs.items }} + JSON + echo "Collected items ✅ (pretty):" + jq . items.json | tee items.pretty.json + + python: + needs: discover_sdk_snippets # + if: ${{ needs.discover_sdk_snippets.outputs.items != '' }} + runs-on: ubuntu-latest + env: + COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} + #SERVER_URL: ${{ secrets.STAGING_SERVER_URL }} + SERVER_URL: https://api.compasslabs.ai/ + strategy: + fail-fast: false + max-parallel: 3 # limit concurrency + matrix: ${{ fromJson(needs.discover_sdk_snippets.outputs.items) }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup uv and Python + uses: astral-sh/setup-uv@v5 + with: + python-version: "3.12" + + - name: Install dependencies (uv) + working-directory: v1/test_sdk_snippets + run: uv sync + + - name: Print compass-api-sdk version (uv) + working-directory: v1/test_sdk_snippets + run: uv pip freeze | grep -i '^compass-api-sdk' || echo "compass-api-sdk not installed" + + - name: Print pyproject.toml after installing dependencies + working-directory: v1/test_sdk_snippets + run: cat pyproject.toml + + - name: Run endpoint snippet + working-directory: v1/test_sdk_snippets + run: uv run python run_endpoint_python.py + env: + ENDPOINT: ${{ matrix.item }} + + typescript: + needs: discover_sdk_snippets # + if: ${{ needs.discover_sdk_snippets.outputs.items != '' }} + runs-on: ubuntu-latest + env: + COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} + #SERVER_URL: ${{ secrets.STAGING_SERVER_URL }} + SERVER_URL: https://api.compasslabs.ai/ + strategy: + fail-fast: false + max-parallel: 3 # limit concurrency + matrix: ${{ fromJson(needs.discover_sdk_snippets.outputs.items) }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup uv and Python + uses: astral-sh/setup-uv@v5 + with: + python-version: "3.12" + + - name: Install dependencies (uv) + working-directory: v1/test_sdk_snippets + run: uv sync + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Install Node deps for TS snippets + working-directory: v1/test_sdk_snippets + run: | + npm init -y >/dev/null 2>&1 || true + npm i -D tsx + # Install the TS SDK used by snippets + npm i @compass-labs/api-sdk + + - name: List installed @compass-labs/api-sdk + working-directory: v1/test_sdk_snippets + run: npm ls @compass-labs/api-sdk || true + + - name: Run endpoint snippet + working-directory: v1/test_sdk_snippets + run: uv run python run_endpoint_typescript.py + env: + ENDPOINT: ${{ matrix.item }} \ No newline at end of file diff --git a/.github/workflows/test_prod_snippets___staging_api_server___latest_sdk.yml b/.github/workflows/test_prod_snippets___staging_api_server___latest_sdk.yml new file mode 100644 index 00000000..711aa692 --- /dev/null +++ b/.github/workflows/test_prod_snippets___staging_api_server___latest_sdk.yml @@ -0,0 +1,135 @@ +name: test_prod_snippets___staging_api_server___latest_sdk +# not sure how to test release candidate snippets + +on: + pull_request: + branches: [ main ] + push: + branches: + - 'conor/com-5939-create-workflow-for-sdk-snippets-test' + + workflow_dispatch: + +jobs: + + discover_sdk_snippets: + runs-on: ubuntu-latest + env: + COMPASS_API_KEY: ${{ secrets.STAGING_COMPASS_API_KEY }} + + outputs: + items: ${{ steps.collect.outputs.items }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup uv and Python + uses: astral-sh/setup-uv@v5 + with: + python-version: "3.12" + + - name: Install dependencies (uv) + working-directory: v1/test_sdk_snippets + run: uv sync + + - id: collect + name: Collect endpoints (writes items=... to $GITHUB_OUTPUT) + working-directory: v1/test_sdk_snippets + run: uv run python discover_endpoints.py + + - name: Print collected items + run: | + cat > items.json <<'JSON' + ${{ steps.collect.outputs.items }} + JSON + echo "Collected items ✅ (pretty):" + jq . items.json | tee items.pretty.json + + python: + needs: discover_sdk_snippets # + if: ${{ needs.discover_sdk_snippets.outputs.items != '' }} + runs-on: ubuntu-latest + env: + COMPASS_API_KEY: ${{ secrets.STAGING_COMPASS_API_KEY }} + SERVER_URL: ${{ secrets.STAGING_SERVER_URL }} #https://api.compasslabs.ai/ + + strategy: + fail-fast: false + #max-parallel: 3 # limit concurrency + matrix: ${{ fromJson(needs.discover_sdk_snippets.outputs.items) }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup uv and Python + uses: astral-sh/setup-uv@v5 + with: + python-version: "3.12" + + - name: Install dependencies (uv) + working-directory: v1/test_sdk_snippets + run: uv sync + + - name: Print compass-api-sdk version (uv) + working-directory: v1/test_sdk_snippets + run: uv pip freeze | grep -i '^compass-api-sdk' || echo "compass-api-sdk not installed" + + - name: Print pyproject.toml after installing dependencies + working-directory: v1/test_sdk_snippets + run: cat pyproject.toml + + - name: Run endpoint snippet + working-directory: v1/test_sdk_snippets + run: uv run python run_endpoint_python.py + env: + ENDPOINT: ${{ matrix.item }} + + typescript: + needs: discover_sdk_snippets # + if: ${{ needs.discover_sdk_snippets.outputs.items != '' }} + runs-on: ubuntu-latest + env: + COMPASS_API_KEY: ${{ secrets.STAGING_COMPASS_API_KEY }} + SERVER_URL: ${{ secrets.STAGING_SERVER_URL }} #https://api.compasslabs.ai/ + strategy: + fail-fast: false + #max-parallel: 3 # limit concurrency + matrix: ${{ fromJson(needs.discover_sdk_snippets.outputs.items) }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup uv and Python + uses: astral-sh/setup-uv@v5 + with: + python-version: "3.12" + + - name: Install dependencies (uv) + working-directory: v1/test_sdk_snippets + run: uv sync + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Install Node deps for TS snippets + working-directory: v1/test_sdk_snippets + run: | + npm init -y >/dev/null 2>&1 || true + npm i -D tsx + # Install the TS SDK used by snippets + npm i @compass-labs/api-sdk + + - name: List installed @compass-labs/api-sdk + working-directory: v1/test_sdk_snippets + run: npm ls @compass-labs/api-sdk || true + + - name: Run endpoint snippet + working-directory: v1/test_sdk_snippets + run: uv run python run_endpoint_typescript.py + env: + ENDPOINT: ${{ matrix.item }} \ No newline at end of file diff --git a/.github/workflows/test_prod_snippets___staging_api_server___release_candidate_sdk.yml b/.github/workflows/test_prod_snippets___staging_api_server___release_candidate_sdk.yml new file mode 100644 index 00000000..0242cec2 --- /dev/null +++ b/.github/workflows/test_prod_snippets___staging_api_server___release_candidate_sdk.yml @@ -0,0 +1,242 @@ +name: test_production_snippets___staging_api_server___release_candidate_sdk + +on: + schedule: + # Runs at 00:00 UTC every day + - cron: "0 0 * * *" + pull_request: + branches: + - main + workflow_dispatch: + inputs: + environment: + description: "Environment to test" + required: true + type: string + + workflow_call: + inputs: + environment: + description: "Environment to test" + required: true + type: string + +permissions: + id-token: write + contents: write + +env: + AWS_ECR_STS_ROLE: arn:aws:iam::688567264391:role/DojoApiCIRole + +jobs: + validate-environment: + runs-on: ubuntu-latest + outputs: + environment: ${{ steps.validate.outputs.environment }} + steps: + - name: Validate environment input + id: validate + run: | + if [[ "${{ github.event_name }}" == "workflow_dispatch" || "${{ github.event_name }}" == "workflow_call" ]]; then + ENVIRONMENT="${{ inputs.environment }}" + else + ENVIRONMENT="local" + fi + if [[ "$ENVIRONMENT" != "staging" && "$ENVIRONMENT" != "prod" && "$ENVIRONMENT" != "local" ]]; then + echo "❌ Error: Invalid environment '$ENVIRONMENT'. Must be 'staging' or 'prod' or 'local'." + exit 1 + fi + echo "✅ Environment '$ENVIRONMENT' is valid" + echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT + + set-versions: + needs: [validate-environment] + runs-on: ubuntu-latest + outputs: + npm_package_version: ${{ steps.set-versions.outputs.npm_package_version }} + uv_package_version: ${{ steps.set-versions.outputs.uv_package_version }} + steps: + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: "20" + + - name: Install uv + run: curl -LsSf https://astral.sh/uv/install.sh | sh + + - name: Set versions + id: set-versions + run: | + if [[ "${{ needs.validate-environment.outputs.environment }}" == "local" ]]; then + NPM_PACKAGE_VERSION=$(npm view @compass-labs/api-sdk versions --json | jq -r '.[]' | grep -E 'rc|alpha|beta' | sort -V | tail -1) + UV_PACKAGE_VERSION="$(curl -s https://pypi.org/pypi/compass-api-sdk/json | jq -r '.releases | keys | .[]' | grep -E 'rc|a|b|dev' | sort -V | tail -1)" + else + NPM_PACKAGE_VERSION="latest" + UV_PACKAGE_VERSION="latest" + fi + + echo "npm_package_version=$NPM_PACKAGE_VERSION" >> $GITHUB_OUTPUT + echo "uv_package_version=$UV_PACKAGE_VERSION" >> $GITHUB_OUTPUT + + # Print environment parameter with emojis + print-environment-and-package-versions: + needs: [validate-environment, set-versions] + runs-on: ubuntu-latest + steps: + - name: Print environment parameter + run: | + echo "🚀 Starting workflow..." + echo "📋 Workflow input environment: ${{ github.event.inputs.environment }}" + echo "⚙️ Active environment: ${{ needs.validate-environment.outputs.environment }}" + echo "📦 npm package version: ${{ needs.set-versions.outputs.npm_package_version }}" + echo "🐍 uv package version: ${{ needs.set-versions.outputs.uv_package_version }}" + echo "✅ Environment validation complete!" + + + discover_sdk_snippets: + runs-on: ubuntu-latest + + outputs: + items: ${{ steps.collect.outputs.items }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup uv and Python + uses: astral-sh/setup-uv@v5 + with: + python-version: "3.12" + + - id: collect + name: Collect endpoints (writes items=... to $GITHUB_OUTPUT) + working-directory: v1/test_sdk_snippets + run: uv run python discover_endpoints.py + + - name: Print collected items + run: | + cat > items.json <<'JSON' + ${{ steps.collect.outputs.items }} + JSON + echo "Collected items ✅ (pretty):" + jq . items.json | tee items.pretty.json + + + python: # Job is called `python` so that subjobs are called `python()` + # to distinguish them from `typescript()` + needs: [validate-environment, set-versions, discover_sdk_snippets ] + if: ${{ needs.discover_sdk_snippets.outputs.items != '' }} + runs-on: ubuntu-latest + env: + COMPASS_API_KEY: ${{ secrets.STAGING_COMPASS_API_KEY }} + #SERVER_URL: https://api.compasslabs.ai/ # production server + SERVER_URL: ${{ secrets.STAGING_SERVER_URL }} + NPM_PACKAGE_VERSION: ${{ needs.set-versions.outputs.npm_package_version }} + UV_PACKAGE_VERSION: ${{ needs.set-versions.outputs.uv_package_version }} + + strategy: + fail-fast: false + #max-parallel: 3 # limit concurrency + matrix: ${{ fromJson(needs.discover_sdk_snippets.outputs.items) }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup uv and Python + uses: astral-sh/setup-uv@v5 + with: + python-version: "3.12" + + - name: Install dependencies + working-directory: v1/test_sdk_snippets + run: | + if [[ "$UV_PACKAGE_VERSION" != "latest" ]]; then + echo "Installing compass-api-sdk==$UV_PACKAGE_VERSION" + uv add compass-api-sdk==$UV_PACKAGE_VERSION + fi + uv sync + uv pip freeze + + - name: Print compass-api-sdk version (uv) + working-directory: v1/test_sdk_snippets + run: uv pip freeze | grep -i '^compass-api-sdk' || echo "compass-api-sdk not installed" + + - name: Print pyproject.toml after installing dependencies + working-directory: v1/test_sdk_snippets + run: cat pyproject.toml + + - name: Run endpoint snippet + working-directory: v1/test_sdk_snippets + run: uv run python run_endpoint_python.py + env: + ENDPOINT: ${{ matrix.item }} + + typescript: + needs: [validate-environment, set-versions, discover_sdk_snippets] + # + if: ${{ needs.discover_sdk_snippets.outputs.items != '' }} + runs-on: ubuntu-latest + env: + COMPASS_API_KEY: ${{ secrets.STAGING_COMPASS_API_KEY }} + SERVER_URL: ${{ secrets.STAGING_SERVER_URL }} #https://api.compasslabs.ai/ + + NPM_PACKAGE_VERSION: ${{ needs.set-versions.outputs.npm_package_version }} + UV_PACKAGE_VERSION: ${{ needs.set-versions.outputs.uv_package_version }} + + strategy: + fail-fast: false + #max-parallel: 3 # limit concurrency + matrix: ${{ fromJson(needs.discover_sdk_snippets.outputs.items) }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup uv and Python + uses: astral-sh/setup-uv@v5 + with: + python-version: "3.12" + + + - name: Install dependencies (uv) + working-directory: v1/test_sdk_snippets + run: | + if [[ "$UV_PACKAGE_VERSION" != "latest" ]]; then + echo "Installing compass-api-sdk==$UV_PACKAGE_VERSION" + uv add compass-api-sdk==$UV_PACKAGE_VERSION + fi + uv sync + uv pip freeze + + - name: Print compass-api-sdk version (uv) + working-directory: v1/test_sdk_snippets + run: uv pip freeze | grep -i '^compass-api-sdk' || echo "compass-api-sdk not installed" + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: "20" + + - name: Print package.json + working-directory: v1/test_sdk_snippets + run: cat package.json + + - name: Install Node deps for TS snippets + working-directory: v1/test_sdk_snippets + run: | + npm install + if [[ "$NPM_PACKAGE_VERSION" != "latest" ]]; then + npm install @compass-labs/api-sdk@$NPM_PACKAGE_VERSION + fi + npm list + + - name: Print package.json after installing dependencies + working-directory: v1/test_sdk_snippets + run: cat package.json + + - name: Run endpoint snippet + working-directory: v1/test_sdk_snippets + run: uv run python run_endpoint_typescript.py + env: + ENDPOINT: ${{ matrix.item }} \ No newline at end of file diff --git a/.github/workflows/test_usecases_v1.yml b/.github/workflows/test_usecases_v1.yml index b0d08099..060a6891 100644 --- a/.github/workflows/test_usecases_v1.yml +++ b/.github/workflows/test_usecases_v1.yml @@ -48,7 +48,7 @@ jobs: fi echo "✅ Environment '$ENVIRONMENT' is valid" echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT - + set-versions: needs: [validate-environment] runs-on: ubuntu-latest @@ -81,7 +81,7 @@ jobs: run-pendle-typescript: needs: [validate-environment, set-versions] - + runs-on: ubuntu-latest env: COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} @@ -135,7 +135,7 @@ jobs: run-pendle-python: needs: [validate-environment, set-versions] - + runs-on: ubuntu-latest env: COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} @@ -237,7 +237,7 @@ jobs: run-python----basic-examples-deposit-on-morpho: # needs: [validate-environment, set-versions] - + runs-on: ubuntu-latest env: COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} @@ -282,11 +282,11 @@ jobs: - name: Run example working-directory: v1/basic_examples/deposit_on_morpho/python run: ./.venv/bin/python main.py #src/main.py - + run-typescript-basic-examples-deposit-on-aave: needs: [validate-environment, set-versions] - + runs-on: ubuntu-latest env: COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} @@ -442,7 +442,7 @@ jobs: run-python----transaction_bundler_embed_a_fee_in_tx: # needs: [validate-environment, set-versions] - + runs-on: ubuntu-latest env: COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} @@ -491,7 +491,7 @@ jobs: run-transaction-bundler-typescript: needs: [validate-environment, set-versions] - + runs-on: ubuntu-latest env: COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} @@ -544,7 +544,7 @@ jobs: run-transaction-bundler-python: needs: [validate-environment, set-versions] - + runs-on: ubuntu-latest env: COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} @@ -592,7 +592,7 @@ jobs: run-aave-looping-typescript: needs: [validate-environment, set-versions] - + runs-on: ubuntu-latest env: COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} @@ -645,7 +645,7 @@ jobs: run-aave-looping-python: needs: [validate-environment, set-versions] - + runs-on: ubuntu-latest env: COMPASS_API_KEY: ${{ secrets.COMPASS_API_KEY }} @@ -708,4 +708,4 @@ jobs: run: | curl -X POST "https://allquiet.app/api/webhook/829f846c-4dfa-4a58-ab33-eafae34a57c9" \ -H "Content-Type: application/json" \ - -d '{"status": "Open", "severity": "Critical", "message": "GitHub workflow failed", "run_url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"}' + -d '{"status": "Open", "severity": "Critical", "message": "GitHub workflow failed", "run_url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"}' \ No newline at end of file diff --git a/v1/test_sdk_snippets/.env.example b/v1/test_sdk_snippets/.env.example new file mode 100644 index 00000000..b523d3c5 --- /dev/null +++ b/v1/test_sdk_snippets/.env.example @@ -0,0 +1,3 @@ +SERVER_URL=<> # set to staging or prod or local +COMPASS_API_KEY=<> +ENDPOINT=<> \ No newline at end of file diff --git a/v1/test_sdk_snippets/discover_endpoints.py b/v1/test_sdk_snippets/discover_endpoints.py new file mode 100644 index 00000000..f643aac6 --- /dev/null +++ b/v1/test_sdk_snippets/discover_endpoints.py @@ -0,0 +1,21 @@ +# scripts/discover_endpoints.py +import json, os, requests + +API_URL = "https://spec.speakeasy.com/compasslabs/api/compass-api-with-code-samples" + +spec = requests.get(API_URL, timeout=(5, 30)).json() + +# Collect all paths that have at least one Python sample +paths = [] +for p, methods in spec.get("paths", {}).items(): + for m in ("get", "post"): + samples = (methods.get(m, {}) or {}).get("x-codeSamples") or [] + if any((s.get("lang") or "").lower().startswith("python") for s in samples): + paths.append(p) + break + +matrix = {"item": paths} +print("Discovered:", matrix) + +with open(os.environ["GITHUB_OUTPUT"], "a") as fh: + fh.write(f"items={json.dumps(matrix)}\n") diff --git a/v1/test_sdk_snippets/package.json b/v1/test_sdk_snippets/package.json new file mode 100644 index 00000000..9017f354 --- /dev/null +++ b/v1/test_sdk_snippets/package.json @@ -0,0 +1,13 @@ +{ + "name": "test-sdk-snippets", + "version": "0.0.0", + "private": true, + "type": "module", + "dependencies": { + "@compass-labs/api-sdk": "latest" + }, + "devDependencies": { + "tsx": "^4.19.1" + } +} + diff --git a/v1/test_sdk_snippets/pyproject.toml b/v1/test_sdk_snippets/pyproject.toml new file mode 100644 index 00000000..3ec91e71 --- /dev/null +++ b/v1/test_sdk_snippets/pyproject.toml @@ -0,0 +1,14 @@ +[project] +name = "python" +version = "0.1.0" +description = "Add your description here" +readme = "README.md" +requires-python = ">=3.12" +dependencies = [ + "compass-api-sdk", + "dotenv", + "eth-account", + "pyright", + "ruff", + "web3", +] diff --git a/v1/test_sdk_snippets/run_endpoint_python.py b/v1/test_sdk_snippets/run_endpoint_python.py new file mode 100644 index 00000000..9ec90454 --- /dev/null +++ b/v1/test_sdk_snippets/run_endpoint_python.py @@ -0,0 +1,53 @@ +# v1/test_sdk_snippets/run_endpoint.py +import os +import sys +import requests +from dotenv import load_dotenv +import subprocess +import shlex + +load_dotenv() + +COMPASS_API_KEY = os.getenv("COMPASS_API_KEY") +SERVER_URL = os.getenv("SERVER_URL") +ENDPOINT = os.getenv("ENDPOINT") +print([ENDPOINT]) +API_URL = "https://spec.speakeasy.com/compasslabs/api/compass-api-with-code-samples" +SPEC = requests.get(API_URL).json() + +# get python code sample for given path +methods = SPEC["paths"].get(ENDPOINT) +if not methods: + raise ValueError(f"Endpoint not found in spec: {ENDPOINT!r}") + +SNIPPET = None +for method in ("get", "post"): + samples = (methods.get(method, {}) or {}).get("x-codeSamples") or [] + for s in samples: + if (s.get("lang") or "").lower().startswith("python"): + SNIPPET = s["source"] + break + if SNIPPET: + break + +if not SNIPPET: + raise ValueError(f"No Python code sample found for endpoint: {ENDPOINT!r}") + + +print(f"--- Running python SDK snippet for {ENDPOINT} ---") + +SNIPPET = SNIPPET.replace("api_key_auth=\"\",", f"api_key_auth='{COMPASS_API_KEY}', server_url='{SERVER_URL}'") +print(SNIPPET) +# Write snippet to a simple file and run it +script_path = os.path.join(os.getcwd(), "snippet.py") +with open(script_path, "w", encoding="utf-8") as f: + f.write(SNIPPET) + +try: + cmd = f"{shlex.quote(sys.executable)} {shlex.quote(script_path)}" + print(cmd) + subprocess.run(cmd, shell=True, check=True) + print(f"✅ PASS: {ENDPOINT}") +except subprocess.CalledProcessError as e: + print(f"❌ FAIL: {ENDPOINT} – exit code {e.returncode}", file=sys.stderr) + sys.exit(e.returncode) diff --git a/v1/test_sdk_snippets/run_endpoint_typescript.py b/v1/test_sdk_snippets/run_endpoint_typescript.py new file mode 100644 index 00000000..f18fbd80 --- /dev/null +++ b/v1/test_sdk_snippets/run_endpoint_typescript.py @@ -0,0 +1,55 @@ +# v1/test_sdk_snippets/run_endpoint.py +import os +import sys +import requests +from dotenv import load_dotenv +import subprocess +import shlex + +load_dotenv() + +COMPASS_API_KEY = os.getenv("COMPASS_API_KEY") +SERVER_URL = os.getenv("SERVER_URL") +ENDPOINT = os.getenv("ENDPOINT") +print([ENDPOINT]) +API_URL = "https://spec.speakeasy.com/compasslabs/api/compass-api-with-code-samples" +SPEC = requests.get(API_URL).json() + +# get python code sample for given path +methods = SPEC["paths"].get(ENDPOINT) +if not methods: + raise ValueError(f"Endpoint not found in spec: {ENDPOINT!r}") + +SNIPPET = None +for method in ("get", "post"): + samples = (methods.get(method, {}) or {}).get("x-codeSamples") or [] + for s in samples: + if (s.get("lang") or "").lower().startswith("typescript"): + SNIPPET = s["source"] + break + if SNIPPET: + break + +if not SNIPPET: + raise ValueError(f"No Typescript code sample found for endpoint: {ENDPOINT!r}") + + +print(f"--- Running typescript SDK snippet for {ENDPOINT} ---") + +SNIPPET = SNIPPET.replace( + 'apiKeyAuth: "",', + f'apiKeyAuth: "{COMPASS_API_KEY}",\n serverURL: "{SERVER_URL}",' +) +# Write snippet to a simple file and run it +script_path = os.path.join(os.getcwd(), "snippet.ts") +with open(script_path, "w", encoding="utf-8") as f: + f.write(SNIPPET) + +try: + # Use tsx to run TS without config; -y auto-installs if needed in CI + cmd = f"npx -y tsx {shlex.quote(script_path)}" + subprocess.run(cmd, shell=True, check=True) + print(f":white_check_mark: PASS: {ENDPOINT}") +except subprocess.CalledProcessError as e: + print(f":x: FAIL: {ENDPOINT} – exit code {e.returncode}", file=sys.stderr) + sys.exit(e.returncode)