refactor(api): consolidate HTTP boilerplate, thread &Client through#15
Merged
Conversation
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>
a0a62e3 to
ed53987
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three related internal cleanups in `src/api.rs` (filed as #12 during /simplify on #11):
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.
`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.
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
Closes