Skip to content

Generated Code

The HTTP Client generator produces a complete effects-native pipeline from your annotated partial classes — six artifacts from a single [HttpClient] declaration.

Interface

An interface I{TypeName} is generated for each client, enabling DI registration and test mocking:

// Generated
public interface IUsersClient
{
    Task<User> GetUser(int id, CancellationToken token = default);
    Task<User> CreateUser(CreateUserRequest request, CancellationToken token = default);
    Task<List<User>> ListUsers(int page, int perPage, string requestId, CancellationToken token = default);
    Task<Unit> DeleteUser(int id, CancellationToken token = default);
}

Capability Interface

A capability interface IHas{TypeName} is generated for effects composition:

// Generated
public interface IHasUsersClient
{
    public IUsersClient UsersClient { get; }
}

This enables compile-time capability checking — your effects can only call HTTP methods when the runtime provides the capability. See Effects for details.

Effects Module

A static effects module {TypeName}Effects wraps each HTTP method in an Eff<RT, T>:

// Generated
public static partial class UsersClientEffects
{
    public static partial class UsersClient
    {
        public static Eff<RT, User> GetUser<RT>(int id)
            where RT : IHasUsersClient => ...

        public static Eff<RT, User> CreateUser<RT>(CreateUserRequest request)
            where RT : IHasUsersClient => ...
    }
}

Each method includes activity tracing and error context. See Effects for the full pattern.

Test Client

A test client Test{TypeName} is generated with call recording and configurable responses:

// Generated
public class TestUsersClient : IUsersClient
{
    // Call recording
    public IReadOnlyList<int> GetUserCalls => _getUserCalls;
    public IReadOnlyList<CreateUserRequest> CreateUserCalls => _createUserCalls;

    // Configurable responses
    public User GetUserResult { get; set; } = default!;
    public User CreateUserResult { get; set; } = default!;

    public Task<User> GetUser(int id, CancellationToken token = default)
    {
        _getUserCalls.Add(id);
        return Task.FromResult(GetUserResult);
    }
    // ...
}

See Testing for usage patterns.

DI Registration

A generated extension method registers the client and its interface with the service collection:

services.AddUsersClient();

This registers:

  • The client implementation via IHttpClientFactory as the IUsersClient service
  • The configured base address (if specified via BaseAddress property)
  • A TestUsersClient fallback via TryAddSingleton (overridden by the real client above)
  • Authentication handlers (if any auth attributes are applied)

Configuration Binding

When using [HttpClient<TConfig>], the base address and other settings are resolved from the bound configuration type at runtime:

[HttpClient<ApiConfig>(BaseAddress = "https://api.example.com")]
public partial class UsersClient;

The BaseAddress property on the attribute serves as a compile-time default. At runtime, the configuration provider can override it — see Configuration for details.