Skip to content

implementations: auth, HTTP client, executor, JSON formatter (PRINFRA-122) - #2

Merged
somanshreddy merged 2 commits into
mainfrom
03-30-m0_client_executor
Mar 31, 2026
Merged

somanshreddy merged 2 commits into
mainfrom
03-30-m0_client_executor

Conversation

@somanshreddy

@somanshreddy somanshreddy commented Mar 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Implements the interfaces from PR #1 — this is where the actual behavior lives.

Components:

  • EnvCredentialResolver — reads HEYGEN_API_KEY from the environment. If missing, returns an auth error (exit 3) with a hint telling the user what to set.
  • EnvProvider — reads HEYGEN_API_BASE from the environment, defaults to https://api.heygen.com. Used to override the API URL in tests and staging.
  • Client — wraps Go's net/http with automatic x-api-key and User-Agent header injection on every request. The WithHTTPClient option is how tests swap in a mock server.
  • Executor — takes a RequestSpec and turns it into a real HTTP call. Handles URL construction, path parameter substitution (e.g. /v3/videos/{video_id}), query param encoding, JSON body marshaling, and error parsing. Returns raw JSON on success or a CLIError on failure, with the API's request ID attached when available.
  • JSONFormatter — the default output renderer. Pretty-prints successful JSON to stdout, renders errors as {"error": {...}} envelopes to stderr. Rejects non-JSON responses as errors (the CLI's contract is structured output).

How they connect: The root command's startup hook creates an EnvProvider, resolves credentials via EnvCredentialResolver, then builds a Client configured with the provider's base URL. Commands use the Client's Executor to run their RequestSpecs, and the Formatter to write the result.

Testing

All tests use Go's httptest.Server — no real API calls. Coverage includes header injection, retry scenarios, query param encoding with repeated keys, path param substitution, POST body marshaling, error envelope parsing for 400/401/500, network errors, and invalid JSON rejection.

Linear: PRINFRA-122

somanshreddy commented Mar 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

@somanshreddy
somanshreddy marked this pull request as ready for review March 30, 2026 21:00
@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from efe76e6 to 9bde38a Compare March 30, 2026 21:05
@somanshreddy somanshreddy changed the title [M0] implementations: auth, HTTP client, executor, JSON formatter implementations: auth, HTTP client, executor, JSON formatter Mar 30, 2026
Comment thread internal/client/client.go Outdated
// auth header injection, base URL, and User-Agent.
//
// Retries with exponential backoff and Retry-After handling are not
// yet implemented. See internal/client/ for future additions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No retry logic yet. Retries with exponential backoff, Retry-After header handling, and injectable clock for deterministic tests will be added alongside the HTTP client hardening track.


clierrors "github.com/heygen-com/heygen-cli/internal/errors"
)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multipart upload (for asset upload) returns a usage error for now. The RequestSpec already has BodyEncoding and FilePath fields — the executor just needs the implementation when asset upload ships.

@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from 9bde38a to d49db0e Compare March 30, 2026 21:29
@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from d49db0e to 00a632c Compare March 30, 2026 22:10
@somanshreddy
somanshreddy force-pushed the 03-30-m0_core_types branch 2 times, most recently from 0b271fa to 5059ec9 Compare March 30, 2026 22:24
@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from 00a632c to 3cb6ba2 Compare March 30, 2026 22:24
Comment thread internal/client/executor.go Outdated
Comment on lines +41 to +44
method := spec.Method
if method == "" {
method = "GET"
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoudl this not always be passed in?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'm curious when it would ever be empty

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defensive programming. But we should actually fail-fast. I will clean this up.

Comment on lines +16 to +21
func (p *EnvProvider) BaseURL() string {
if v := os.Getenv(envBaseURL); v != "" {
return v
}
return DefaultBaseURL
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we store in a config as well? i.e. read from ENV or if not set ask user for API Key and set in a config?

I wonder if we can see what claude code or others do for this

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a section for Config management in the Notion doc -> https://www.notion.so/heygen/Design-331449792c69804993e0eb736b024ca2?source=copy_link#21522983b9974ab1aa7b6f11958b3b6b

We will have a credentials file. We will introduce profiles (dev, prod) later. This file can be manipulated by CLI commands such as heygen config set ...

@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from 3cb6ba2 to 5f62f7e Compare March 31, 2026 05:44
@somanshreddy somanshreddy changed the title implementations: auth, HTTP client, executor, JSON formatter implementations: auth, HTTP client, executor, JSON formatter (PRINFRA-122) Mar 31, 2026
@linear

linear Bot commented Mar 31, 2026

Copy link
Copy Markdown
PRINFRA-122 CLI M0: HTTP client + framework executor + JSON formatter

Implement the minimal HTTP client, framework executor, and JSON output formatter. This is the "spine" of the CLI.

Scope

  • HTTP client wrapper:
    • Auth header injection from HEYGEN_API_KEY env var
    • Base URL (https://api.heygen.com default, --api-base override)
    • User-Agent header: heygen-cli/<version> (<os>/<arch>)
    • Response parsing into JSON or CLIError
  • Framework executor:
    • Takes a RequestSpec, builds HTTP request, sends, handles response
    • Maps HTTP status codes to CLIError with correct exit codes
    • Destructive command confirmation prompt (step 3.5 in Design)
  • JSON formatter:
    • json.MarshalIndent to stdout
    • Error envelope to stderr
    • --quiet flag suppresses stderr except errors

Not in scope (M1)

  • Retries with exponential backoff
  • Retry-After rate limit handling
  • Pagination (--all)
  • Polling (--wait)

Acceptance Criteria

  • HTTP client sends correct auth header from env var
  • Framework executor takes RequestSpec and produces JSON on stdout
  • API errors produce correct CLIError with exit codes
  • HEYGEN_DEBUG=1 logs full request/response

somanshreddy commented Mar 31, 2026 •

Copy link
Copy Markdown
Collaborator Author

Merge activity

  • Mar 31, 5:53 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Mar 31, 5:54 AM UTC: Graphite rebased this pull request as part of a merge.
  • Mar 31, 5:56 AM UTC: @somanshreddy merged this pull request with Graphite.

@somanshreddy
somanshreddy changed the base branch from 03-30-m0_core_types to graphite-base/2 March 31, 2026 05:53
@somanshreddy
somanshreddy changed the base branch from graphite-base/2 to main March 31, 2026 05:54
somanshreddy and others added 2 commits March 31, 2026 05:54
Concrete implementations of the core interfaces:

- EnvCredentialResolver: reads HEYGEN_API_KEY env var, returns exit 3 with hint
- EnvProvider: reads HEYGEN_API_BASE env var, defaults to api.heygen.com
- Client: net/http wrapper with x-api-key and User-Agent injection
- Executor: RequestSpec → HTTP request with path param substitution,
  []QueryParam encoding, []FieldSpec → JSON body, error envelope parsing
- JSONFormatter: pretty-printed JSON to stdout, error envelopes to stderr

All tested via httptest — no real API calls.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Remove silent fallback to GET when Method is empty. This prevents
bugs where a command forgets to set Method and silently sends a GET
instead of failing.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from 5f62f7e to b465389 Compare March 31, 2026 05:54
@somanshreddy
somanshreddy merged commit 072b1ee into main Mar 31, 2026
9 checks passed
@somanshreddy
somanshreddy deleted the 03-30-m0_client_executor branch March 31, 2026 05:56
somanshreddy added a commit that referenced this pull request Mar 31, 2026
…RA-142) (#3)

## Description

Wires everything from PRs #1 and #2 into a working CLI. After this, `heygen video list` works end-to-end against the real HeyGen API.

**What happens when you run `heygen video list --limit 5`:**

1. `main.go` creates a JSON formatter before anything else — so even early failures (bad auth, bad flags) get structured JSON error output, never plain text.
2. Cobra parses the command and flags. Unknown commands and bad flags are caught and returned as usage errors (exit 2).
3. The root command's startup hook runs: creates a config provider, resolves the API key, and builds an HTTP client.
4. The `video list` command validates `--limit` is 1-100, builds a RequestSpec, and hands it to the executor.
5. The executor makes the HTTP call and returns raw JSON. The formatter pretty-prints it to stdout.
6. If anything fails at any step, the error flows back to `main.go` where it's classified (auth → exit 3, usage → exit 2, everything else → exit 1) and rendered as a JSON envelope to stderr.

**Test harness:** `runCommand()` in the test helper mirrors this exact flow — fresh Cobra tree, buffer-backed formatter, same error classification. Tests assert on exit codes and the JSON envelope shape, not just whether an error occurred.

## Testing

10 command tests using httptest mocks:
- Success with valid JSON response
- Missing auth (exit 3 with hint)
- API 401 and 400 responses (correct exit codes, request ID preserved)
- Flags passed correctly as query params
- Limit validation (exit 2 for 0, -5, 999)
- Unknown flag and unknown command (exit 2)
- Server error (exit 1)

Manual smoke test: `HEYGEN_API_KEY=<key> ./bin/heygen video list --limit 2` returns real video JSON.

Linear: [PRINFRA-142](https://linear.app/heygen/issue/PRINFRA-142)
@somanshreddy somanshreddy added this to the M0: Walking Skeleton milestone Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants