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 ¶
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:
- Extracts the request body (if present) from the builder
- Creates appropriate request constructor (with or without body)
- Applies the target URL from the builder
- Applies the HTTP method from the builder
- Merges headers from the builder into the request
- 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())()