builder

package
v2.2.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package builder provides utilities for building HTTP requests in a functional way using the ReaderIOResult monad. It integrates with the http/builder package to create composable, type-safe HTTP request builders with proper error handling and context support.

The main function, Requester, converts a Builder from the http/builder package into a ReaderIOResult that produces HTTP requests. This allows for:

  • Immutable request building with method chaining
  • Automatic header management including Content-Length
  • Support for requests with and without bodies
  • Proper error handling wrapped in Either
  • Context propagation for cancellation and timeouts

Example usage:

import (
    "context"
    B "github.com/IBM/fp-go/v2/http/builder"
    RB "github.com/IBM/fp-go/v2/context/readerioresult/http/builder"
)

builder := F.Pipe3(
    B.Default,
    B.WithURL("https://api.example.com/users"),
    B.WithMethod("POST"),
    B.WithJSONBody(userData),
)

requester := RB.Requester(builder)
result := requester(t.Context())()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Requester

func Requester(builder *R.Builder) RIOEH.Requester

Requester converts an http/builder.Builder into a ReaderIOResult that produces HTTP requests. It handles both requests with and without bodies, automatically managing headers including Content-Length for requests with bodies.

The function performs the following operations:

  1. Extracts the request body (if present) from the builder
  2. Creates appropriate request constructor (with or without body)
  3. Applies the target URL from the builder
  4. Applies the HTTP method from the builder
  5. Merges headers from the builder into the request
  6. Handles any errors that occur during request construction

For requests with a body:

  • Sets the Content-Length header automatically
  • Uses bytes.NewReader to create the request body
  • Merges builder headers into the request

For requests without a body:

  • Creates a request with nil body
  • Merges builder headers into the request

Parameters:

  • builder: A pointer to an http/builder.Builder containing request configuration

Returns:

  • A Requester (ReaderIOResult[*http.Request]) that, when executed with a context, produces either an error or a configured *http.Request

Example with body:

import (
    B "github.com/IBM/fp-go/v2/http/builder"
    RB "github.com/IBM/fp-go/v2/context/readerioresult/http/builder"
)

builder := F.Pipe3(
    B.Default,
    B.WithURL("https://api.example.com/users"),
    B.WithMethod("POST"),
    B.WithJSONBody(map[string]string{"name": "John"}),
)
requester := RB.Requester(builder)
result := requester(t.Context())()

Example without body:

builder := F.Pipe2(
    B.Default,
    B.WithURL("https://api.example.com/users"),
    B.WithMethod("GET"),
)
requester := RB.Requester(builder)
result := requester(t.Context())()

Types

type Void added in v2.2.2

type Void = function.Void

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL