Skip to content

Allow injecting an HttpClient into ApiClient and Api classes - #681

Open
ZacMarcus wants to merge 1 commit into
XeroAPI:masterfrom
ZacMarcus:feature/inject-httpclient
Open

ZacMarcus wants to merge 1 commit into
XeroAPI:masterfrom
ZacMarcus:feature/inject-httpclient

Conversation

@ZacMarcus

@ZacMarcus ZacMarcus commented Nov 14, 2026 •

Copy link
Copy Markdown

Closes #682

What

  • ApiClient: new ApiClient(HttpClient) and ApiClient(HttpClient, string basePath) constructors. The injected client is owned by the caller and is not disposed by the SDK. RequestOptions.Cookies is not supported in this mode; the existing guard in Exec throws a clear error.
  • All 12 Api classes (Accounting, AppStore, Asset, BankFeeds, Files, Finance, Identity, PayrollAu, PayrollAuV2, PayrollNz, PayrollUk, Project): new XxxApi(HttpClient httpClient, Configuration configuration = null) constructor. Without a configuration, each class keeps its own default base path.
  • New HttpClientInjectionTests. The test project now references Microsoft.Extensions.Http 8.0.1 for the dependency-injection test.

Why

ApiClient always created its own private HttpClient, and nothing let callers pass one in. So handlers registered through IHttpClientFactory never applied to Xero API calls, e.g. Microsoft.Extensions.Http.Resilience (AddStandardResilienceHandler), logging or telemetry. The only workaround was setting the private _httpClient field by reflection. XeroClient in OAuth2Client already accepts an HttpClient; this gives the API calls the same option.

services.AddHttpClient<AccountingApi>()
    .AddStandardResilienceHandler();

Notes for reviewers

  • There is deliberately one constructor that takes an HttpClient, with an optional Configuration, rather than two overloads. ActivatorUtilities, which typed clients use, throws when more than one constructor accepts the given argument types. A test covers registering the class with AddHttpClient<AccountingApi>().
  • The change only adds code. Existing constructors and the ownership of internally created clients are unchanged.
  • The Api classes are generated. The same change needs to go into the generator templates (api.mustache, ApiClient.mustache), or the next spec release will remove these constructors.
  • Separate existing bug, not fixed here: ApiClient.Exec builds a token that includes Configuration.Timeout but passes the original cancellationToken to SendAsync, so the timeout is never applied.

Testing

  • The new client tests pass (7/7 in Test.Api.Client).
  • Full suite: 1877 passed. 184 AccountingApiTests/PayrollAuApiTests/BankFeedsApiTests failed with "connection refused" because the local mock server wasn't running. None of those failures relate to this change.

🤖 Generated with Claude Code

ApiClient always created its own private HttpClient, so handlers
registered via IHttpClientFactory (e.g. Microsoft.Extensions.Http.Resilience)
never applied to Xero API calls.

- ApiClient: add ApiClient(HttpClient) and ApiClient(HttpClient, basePath).
  The injected client is caller-owned and not disposed by the SDK.
- Api classes: add XxxApi(HttpClient, Configuration = null), defaulting to
  each API's base path. A single HttpClient constructor keeps the classes
  usable as typed clients via services.AddHttpClient<XxxApi>().
- Tests for routing, base paths, disposal ownership and typed-client DI.

The Api classes are generated; the matching template change is still needed
so the next spec release doesn't drop these constructors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Allow injecting an HttpClient (IHttpClientFactory) into ApiClient and the Api classes

1 participant