Skip to content

Improve HttpRequestBuilder fluent API, retries, and per-request logging#17

Merged
mr-kg merged 1 commit into
masterfrom
add-retry
Jun 1, 2026
Merged

Improve HttpRequestBuilder fluent API, retries, and per-request logging#17
mr-kg merged 1 commit into
masterfrom
add-retry

Conversation

@mr-kg

@mr-kg mr-kg commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Improve HttpRequestBuilder fluent API, retries, and per-request logging

Summary

This PR expands HttpRequestBuilder into a more complete fluent request builder while keeping the existing Add* methods available for compatibility.

The main additions are:

  • Polly retry support via WithRetry(...)
  • URI constructor and WithUri(...)
  • no-URI HTTP verb overloads, plus Patch, Head, and Options
  • retry-safe HttpContent factories
  • per-request headers, authorization, cancellation, body, JSON serializer settings, and logging helpers
  • per-request ILogger support with WithLogger(...) and optional WithLogScope(...)

Notable Changes

Fluent API

  • Added HttpRequestBuilder(string uri)
  • Added WithUri(string uri)
  • Added no-URI overloads for Get, Post, Put, Patch, and Delete
  • Added Patch, Head, and Options verb methods
  • Added WithBody, WithContent, WithHeader, WithBearerToken, WithAuthorization, WithCancellationToken, and WithJsonSerializerSettings

Retry Support

  • Added Polly package dependency
  • Added WithRetry(ResiliencePipeline<Response>)
  • Retry policies work for both MakeRequestAsync() and MakeRequestAsync<T>()
  • Added WithContent(Func<HttpContent>) / AddContent(Func<HttpContent>) so retries can create fresh request content per attempt

Logging

  • Added per-request WithLogger(ILogger logger)
  • Added per-request WithLogScope(Enums.LogScope logScope)
  • WithLogger(...) defaults to logging all request activity
  • WithLogScope(Enums.LogScope.OnlyFailedRequests) limits that request to failed response logging
  • Per-request logger config overrides global HttpConfig.Logger for that request

Request Handling

  • HttpResponseMessage is now disposed after response materialization
  • Header values now replace existing values instead of throwing on duplicate keys
  • Authorization is routed through typed AuthenticationHeaderValue handling
  • Query/form parameters now allow duplicate keys instead of silently ignoring later values
  • AddQueryParams now accepts IEnumerable<KeyValuePair<string, string>>

Documentation

  • README updated with examples for:
    • constructor URI usage
    • WithUri
    • Patch
    • Head / Options
    • retry with Polly
    • retry-safe content factories
    • per-request logging
    • serializer settings
    • single-use builder guidance

Compatibility Notes

Existing builder methods such as AddBody, AddHeader, AddContent, and AddCancellationToken remain available. They now delegate to the newer With* methods where appropriate.

The behavior of duplicate query/form parameters changed: duplicate keys are now preserved. This better matches common HTTP query string behavior, but it is worth calling out for consumers who relied on duplicate suppression.

Tests

Ran:

dotnet test

Result:

Passed! - Failed: 0, Passed: 52, Skipped: 0

@mr-kg mr-kg merged commit 98b7c49 into master Jun 1, 2026
1 check 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