client

package
v2.3.131 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package client provides functional lenses for reading and updating fields of an *http.Client in an immutable, composable way.

Each lens follows the standard Lens[S, A] contract:

  • Get: extracts the focused field from the source
  • Set: returns a copy of the source with the focused field replaced

Both lenses in this package operate on *http.Client (a pointer type). The Set operation always copies the struct before mutating it, so the original pointer is never modified.

Lenses:

TimeoutLens()   — focuses on the Timeout field (time.Duration)
TransportLens() — focuses on the Transport field (http.RoundTripper)

Example — compose two lenses to build a configured client:

import (
    C  "github.com/IBM/fp-go/v2/http/client"
    EN "github.com/IBM/fp-go/v2/endomorphism"
)

configure := EN.Concat(
    C.TimeoutLens().Set(30 * time.Second),
    C.TransportLens().Set(myTransport),
)
client := configure(&http.Client{})

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Endomorphism

type Endomorphism[A any] = endomorphism.Endomorphism[A]

Endomorphism is a function from a type to itself (A → A). It represents transformations that preserve the type.

Used as the return type of Lens.Set: applying a lens setter produces an Endomorphism that can be composed with other transformations via endomorphism.Concat.

type Lens

type Lens[S, A any] = common.Lens[S, A]

Lens is an optic that focuses on a single field A inside a larger structure S. It provides two operations:

  • Get(s S) A — extract the focused field
  • Set(a A) func(S) S — return a copy of s with the field replaced by a

Both lenses in this package use *http.Client as S, so Set always copies the pointed-to struct before mutating it.

func TimeoutLens

func TimeoutLens() Lens[*http.Client, time.Duration]

TimeoutLens returns a Lens that focuses on the Timeout field of an *http.Client.

The lens follows the standard immutable contract: Set copies the *http.Client before updating Timeout, so the original pointer is not modified.

Returns:

  • Lens[*http.Client, time.Duration]: a lens for the Timeout field
Example (Compose)

ExampleTimeoutLens_compose demonstrates composing both lenses to configure a client in a single pipeline.

package main

import (
	"fmt"
	"net/http"
	"time"

	C "github.com/IBM/fp-go/v2/http/client"
)

func main() {
	tl := C.TimeoutLens()
	trl := C.TransportLens()

	configure := func(c *http.Client) *http.Client {
		return trl.Set(http.DefaultTransport)(tl.Set(5 * time.Second)(c))
	}

	client := configure(&http.Client{})

	fmt.Println(client.Timeout)
	fmt.Println(client.Transport != nil)
}
Output:
5s
true
Example (Get)

ExampleTimeoutLens_get demonstrates reading the Timeout from an *http.Client.

package main

import (
	"fmt"
	"net/http"
	"time"

	C "github.com/IBM/fp-go/v2/http/client"
)

func main() {
	lens := C.TimeoutLens()
	client := &http.Client{Timeout: 30 * time.Second}

	timeout := lens.Get(client)
	fmt.Println(timeout)
}
Output:
30s
Example (Set)

ExampleTimeoutLens_set demonstrates setting the Timeout on an *http.Client. Set always returns a new copy; the original is not modified.

package main

import (
	"fmt"
	"net/http"
	"time"

	C "github.com/IBM/fp-go/v2/http/client"
)

func main() {
	lens := C.TimeoutLens()
	original := &http.Client{}

	updated := lens.Set(10 * time.Second)(original)

	fmt.Println(original.Timeout) // original unchanged
	fmt.Println(updated.Timeout)  // new copy has the updated value
}
Output:
0s
10s

func TransportLens

func TransportLens() Lens[*http.Client, http.RoundTripper]

TransportLens returns a Lens that focuses on the Transport field of an *http.Client.

The lens follows the standard immutable contract: Set copies the *http.Client before updating Transport, so the original pointer is not modified.

Returns:

  • Lens[*http.Client, http.RoundTripper]: a lens for the Transport field
Example (Get)

ExampleTransportLens_get demonstrates reading the Transport from an *http.Client.

package main

import (
	"fmt"
	"net/http"

	C "github.com/IBM/fp-go/v2/http/client"
)

func main() {
	lens := C.TransportLens()
	client := &http.Client{Transport: http.DefaultTransport}

	transport := lens.Get(client)
	fmt.Println(transport != nil)
}
Output:
true
Example (Set)

ExampleTransportLens_set demonstrates setting the Transport on an *http.Client. Set always returns a new copy; the original is not modified.

package main

import (
	"fmt"
	"net/http"

	C "github.com/IBM/fp-go/v2/http/client"
)

func main() {
	lens := C.TransportLens()
	original := &http.Client{}

	updated := lens.Set(http.DefaultTransport)(original)

	fmt.Println(original.Transport == nil) // original unchanged
	fmt.Println(updated.Transport != nil)  // new copy has the transport set
}
Output:
true
true

Jump to

Keyboard shortcuts

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