Skip to content

Export HTTPClient field for custom http.Client injection#46

Open
heardbefore wants to merge 1 commit intokothar:masterfrom
heardbefore:add-http-client-option
Open

Export HTTPClient field for custom http.Client injection#46
heardbefore wants to merge 1 commit intokothar:masterfrom
heardbefore:add-http-client-option

Conversation

@heardbefore
Copy link
Copy Markdown

@heardbefore heardbefore commented Apr 17, 2026

Summary

  • Replace the unexported httpClient http.Client value field on the B2 struct with an exported HTTPClient *http.Client pointer field
  • When HTTPClient is nil, the client falls back to http.DefaultClient, preserving existing behavior for all current users
  • Add an internal httpClient() helper that returns the effective client

Motivation

Callers currently have no way to customize the HTTP client used by B2. This makes it impossible to:

  • Set a custom User-Agent header (needed for Backblaze's integration checklist)
  • Configure custom TLS settings
  • Add observability middleware (tracing, metrics)
  • Use a custom transport for proxying

This is a minimal, backwards-compatible change — the only breaking aspect is that code directly initializing the unexported httpClient field in struct literals (which is only possible within this package) now uses the exported name.

Test plan

  • Updated all existing tests to use the new HTTPClient field
  • Verify go test ./... passes (all tests use httptest.Server with a custom client, which now goes through the exported field)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Users can now provide a custom HTTP client configuration to customize request behavior (timeouts, proxies, transports, etc.).
    • When no custom client is provided, the library automatically falls back to the default HTTP client, ensuring backward compatibility.

Replace the unexported httpClient http.Client value field with an
exported HTTPClient *http.Client pointer field. When nil, the client
falls back to http.DefaultClient, preserving existing behavior.

This enables callers to inject a custom *http.Client with a wrapped
Transport, which is useful for setting custom User-Agent headers,
configuring TLS, or adding observability middleware.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7ca7ca7b-e662-4b35-a918-907c2581b083

📥 Commits

Reviewing files that changed from the base of the PR and between 35409b8 and 3c8a23a.

📒 Files selected for processing (5)
  • backblaze.go
  • backblaze_test.go
  • buckets_test.go
  • files.go
  • files_test.go

📝 Walkthrough

Walkthrough

Refactors the B2 struct's HTTP client management by introducing a configurable exported HTTPClient field and an accessor method that defaults to http.DefaultClient. Removes the previous internal client field and updates all request execution call sites to use the accessor method instead of direct field access.

Changes

Cohort / File(s) Summary
Core HTTP Client Refactoring
backblaze.go
Added exported HTTPClient field to B2 struct, introduced unexported httpClient() accessor method that returns HTTPClient or http.DefaultClient, removed internal httpClient field, and updated three request execution points in internalAuthorizeAccount, authGet, and authPost to call the accessor method.
Test Updates
backblaze_test.go, buckets_test.go, files_test.go
Updated B2 struct initialization in six test cases (TestAuth, TestReAuth, test in buckets_test.go, TestDownloadFile, TestDownloadFileRange, TestDownloadReAuth) to assign HTTP client via HTTPClient field instead of httpClient field.
Client Method Calls
files.go
Updated three call sites (file upload, file download by ID, file download by name) to invoke httpClient() accessor method instead of accessing stored field directly.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A client now flexible, configurable, keen,
No longer bound to a single HTTP scene,
With methods so clever and fields so sublime,
The B2 struct shines—refactored just fine!
Defaults and customs, a harmonious blend,
On this refactoring, the bunny approves, friend! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: exporting the HTTPClient field to enable custom http.Client injection, which is the primary objective of the pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.11.4)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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