Skip to content

refactor(api): consolidate HTTP boilerplate, thread &Client through#15

Merged
I-am-nothing merged 1 commit into
mainfrom
refactor/12-api-http-cleanup
May 4, 2026
Merged

refactor(api): consolidate HTTP boilerplate, thread &Client through#15
I-am-nothing merged 1 commit into
mainfrom
refactor/12-api-http-cleanup

Conversation

@I-am-nothing

Copy link
Copy Markdown
Contributor

Summary

Three related internal cleanups in `src/api.rs` (filed as #12 during /simplify on #11):

  1. Bounded body read — lift `MAX_RESPONSE_BYTES` and `read_capped` into `src/http.rs`. The same constant + `resp.take(...).read_to_end` pattern was duplicated in `api.rs` and `auth/mod.rs`. Now both call `http::read_capped`; auth's typed wrapper drops to two lines.

  2. `unexpected_body_error` helper — `me`, `get_module`, and `create_module` had the same fall-through-to-Unexpected tail. One helper, three single-line call sites.

  3. Thread `&Client` through `api::*` — previously each call built its own `reqwest::blocking::Client`. `module init` was constructing 3 per invocation (rustls + connection pool init each time). Functions now take `&Client`; `whoami.rs` and `module.rs` build one upfront. Matches the existing `auth::exchange_code` pattern.

Behaviour

No user-visible changes. Same wire calls, same error mapping, same exit codes.

Test plan

  • `cargo fmt --check`
  • `cargo clippy --all-targets -- -D warnings`
  • `cargo test` — 25/25 (was 22; +3 new for paths the prior diff didn't cover)
  • `cargo build --release`

Closes

Three related cleanups in src/api.rs that were repeated boilerplate:

1. **Bounded body read**: \`MAX_RESPONSE_BYTES = 64 * 1024\` was duplicated
   in api.rs and auth/mod.rs, with the same \`resp.take(...).read_to_end\`
   pattern reimplemented 4 times. Lift into \`http::read_capped\` returning
   \`io::Result<Vec<u8>>\` so each caller maps to its own error type. The
   auth-side wrapper is now two lines.

2. **\`unexpected_body_error\` helper**: \`me\`, \`get_module\`, and
   \`create_module\` all had the same "fall through to ApiError::Unexpected
   with bounded body" tail. One helper, three call sites collapse to a
   single line each.

3. **Reuse one reqwest::blocking::Client per command**: previously each
   \`api::*\` built its own client (rustls + connection pool init).
   \`module init\` was constructing 3 clients per invocation. Functions now
   take \`&Client\`; \`whoami.rs\` and \`module.rs\` build one upfront and
   thread it through. Matches the existing \`auth::exchange_code\` pattern.

- 25/25 passing (was 22). Added:
  - \`me_5xx_is_unexpected_with_body\` — covers the \`unexpected_body_error\`
    path through \`me\`
  - \`get_module_401_is_unauthenticated\` — fills the missing 401 test for
    \`get_module\` that the rust-reviewer flagged for parity
  - \`create_module_4xx_without_envelope_is_unexpected\` — covers the
    fallback when the 4xx body isn't a parseable error envelope

Closes #12

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@I-am-nothing I-am-nothing force-pushed the refactor/12-api-http-cleanup branch from a0a62e3 to ed53987 Compare May 4, 2026 06:14
@I-am-nothing I-am-nothing merged commit f0205fc into main May 4, 2026
5 checks passed
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.

1 participant