Documentation
¶
Overview ¶
Package httputil provides HTTP transport and client utilities for LangChainGo.
The package offers several key features:
User-Agent Management ¶
All HTTP clients and transports in this package automatically add a User-Agent header that identifies the LangChainGo library, the calling program, and system information. This helps API providers understand client usage patterns and aids in debugging.
The User-Agent format is:
program/version langchaingo/version Go/version (GOOS GOARCH)
For example:
openai-chat-example/devel langchaingo/v0.1.8 Go/go1.21.0 (darwin arm64)
Default HTTP Client ¶
The package provides DefaultClient, which is a pre-configured http.Client that includes the User-Agent header:
resp, err := httputil.DefaultClient.Get("https://api.example.com/data")
Logging and Debugging ¶
For development and debugging, the package provides logging clients:
// LoggingClient logs full HTTP requests and responses using slog client := httputil.LoggingClient // JSONDebugClient pretty-prints JSON payloads with ANSI colors client := httputil.JSONDebugClient
Custom Transports ¶
The Transport type implements http.RoundTripper and can be used to add the LangChainGo User-Agent to any HTTP client:
client := &http.Client{
Transport: &httputil.Transport{
Transport: myCustomTransport,
},
}
Integration with httprr ¶
The transports in this package are designed to work with the httprr HTTP record/replay system used in tests. When using httprr, pass httputil.DefaultTransport to ensure proper request interception.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultTransport is the default HTTP transport for LangChainGo. // It wraps [http.DefaultTransport] and adds a User-Agent header containing // the LangChainGo version, program information, and system details. // This transport is suitable for use with httprr in tests. DefaultTransport http.RoundTripper = &Transport{ Transport: http.DefaultTransport, } // DefaultClient is the default HTTP client for LangChainGo. // It uses [DefaultTransport] to automatically include proper User-Agent // headers in all requests. This client is recommended for all LangChainGo // HTTP operations unless custom transport behavior is required. DefaultClient = &http.Client{ Transport: DefaultTransport, } )
var DebugHTTPClient = LoggingClient
DebugHTTPClient is a deprecated alias for LoggingClient.
Deprecated: Use LoggingClient instead.
var DebugHTTPColorJSON = JSONDebugClient //nolint:gochecknoglobals
DebugHTTPColorJSON is a deprecated alias for JSONDebugClient.
Deprecated: Use JSONDebugClient instead.
var JSONDebugClient = &http.Client{ Transport: &Transport{ Transport: &jsonDebugTransport{}, }, }
JSONDebugClient is an http.Client designed for debugging JSON APIs. It pretty-prints JSON request and response bodies to stdout with ANSI colors: requests are shown in blue, responses in green. This client is intended for development and debugging purposes only.
Unlike LoggingClient, this client writes directly to stdout rather than using structured logging.
var LoggingClient = &http.Client{ Transport: &Transport{ Transport: &LoggingTransport{}, }, }
LoggingClient is an http.Client that logs complete HTTP requests and responses using structured logging via slog. This client is useful for debugging API interactions, as it captures the full request and response including headers and bodies. The logs are emitted at DEBUG level.
Example:
slog.SetLogLoggerLevel(slog.LevelDebug)
resp, err := httputil.LoggingClient.Get("https://api.example.com/data")
Functions ¶
Types ¶
type LoggingTransport ¶
type LoggingTransport struct {
// Transport is the underlying [http.RoundTripper] to use.
// If nil, [http.DefaultTransport] is used.
Transport http.RoundTripper
// Logger is the [slog.Logger] to use for logging.
// If nil, [slog.Default] is used.
Logger *slog.Logger
}
LoggingTransport is an http.RoundTripper that logs complete HTTP requests and responses using structured logging. It's designed for debugging and development purposes.
The transport logs at DEBUG level, so ensure your logger is configured appropriately to see the output.
func (*LoggingTransport) RoundTrip ¶
RoundTrip implements the http.RoundTripper interface. It logs the complete HTTP request (including headers and body) before sending it, executes the request using the underlying transport, then logs the complete response. Both request and response are logged at DEBUG level.
type Transport ¶
type Transport struct {
// Transport is the underlying [http.RoundTripper] to use.
// If nil, [http.DefaultTransport] is used.
Transport http.RoundTripper
}
Transport is an http.RoundTripper that adds LangChainGo User-Agent headers to outgoing HTTP requests. It wraps another RoundTripper (typically http.DefaultTransport) and can be used to add User-Agent headers to any HTTP client.
If the wrapped request already has a User-Agent header, the LangChainGo User-Agent is appended to it rather than replacing it.
func (*Transport) RoundTrip ¶
RoundTrip implements the http.RoundTripper interface. It adds the LangChainGo User-Agent header to the request and then delegates to the underlying transport. If the request already has a User-Agent header, the LangChainGo information is appended to preserve existing client identification.