httpclient

package
v0.0.0-...-820be1f Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 23 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	StreamPresetOpenAI    = "openai"
	StreamPresetAnthropic = "anthropic"
	StreamPresetVercelAI  = "vercel-ai"
	// StreamPresetSSE is raw Server-Sent Events: the text is every data payload.
	StreamPresetSSE = "sse"
)

Stream presets: how a streamed response's text is assembled from its events.

View Source
const DefaultStreamTimeout = 30 * time.Second

DefaultStreamTimeout bounds how long a stream may stay open.

View Source
const MaxStreamEvents = 10000

MaxStreamEvents bounds the events kept per response; EventCount still counts all of them.

View Source
const TimeoutRequest = 60 * time.Second

Variables

StreamPresets lists every preset.

Functions

func ConvertHeadersToHttp

func ConvertHeadersToHttp(headers []Header) http.Header

func ConvertQueriesToUrl

func ConvertQueriesToUrl(queries []Query, url url.Values) url.Values

func Cookies

func Cookies(client HttpClient) map[string]string

Cookies returns the latest value of every cookie the client has received, by name. Empty for a client that doesn't use a recording jar.

func IsStreamPreset

func IsStreamPreset(p string) bool

IsStreamPreset reports whether p names a preset.

func New

func New() *http.Client

func SendRequest

func SendRequest(client HttpClient, req *Request) (*http.Response, error)

func SendRequestWithContext

func SendRequestWithContext(ctx context.Context, client HttpClient, req *Request) (*http.Response, error)

func WithoutRedirects

func WithoutRedirects(ctx context.Context) context.Context

WithoutRedirects marks a request context so a client from New returns a 30x response as is instead of following it (the yamlflow request option follow_redirects: false). Other requests on the same client still follow redirects.

Types

type Header struct {
	HeaderKey string
	Value     string
}

func ConvertHttpHeaderToHeaders

func ConvertHttpHeaderToHeaders(headers http.Header) []Header

type HttpClient

type HttpClient interface {
	Do(req *http.Request) (*http.Response, error)
}

type Query

type Query struct {
	QueryKey string
	Value    string
}

type Request

type Request struct {
	Method  string
	URL     string
	Queries []Query
	Headers []Header
	Body    []byte
}

type Response

type Response struct {
	StatusCode int      `json:"statusCode"`
	Body       []byte   `json:"body"`
	Headers    []Header `json:"headers"`
	// URL is the URL the response came from, after any redirects; Redirects is how many
	// redirects were followed to get there.
	URL       string `json:"url,omitempty"`
	Redirects int    `json:"redirects,omitempty"`
	// Stream is set when the response was read as a stream (see SendRequestAndConvertStream).
	Stream *StreamResult `json:"stream,omitempty"`
}

func SendRequestAndConvert

func SendRequestAndConvert(client HttpClient, req *Request, exampleID idwrap.IDWrap) (Response, error)

func SendRequestAndConvertStream

func SendRequestAndConvertStream(ctx context.Context, client HttpClient, req *Request, exampleID idwrap.IDWrap, opts *StreamOptions) (Response, error)

SendRequestAndConvertStream sends a request and converts the response. The body is read as a stream when opts is set or the response is text/event-stream; otherwise it is read whole, as SendRequestAndConvertWithContext does.

func SendRequestAndConvertWithContext

func SendRequestAndConvertWithContext(ctx context.Context, client HttpClient, req *Request, exampleID idwrap.IDWrap) (Response, error)

SendRequestAndConvertWithContext sends a request and converts the response. A text/event-stream response is read as a raw SSE stream (see SendRequestAndConvertStream).

type ResponseVar

type ResponseVar struct {
	StatusCode int               `json:"status"`
	Body       any               `json:"body"`
	Headers    map[string]string `json:"headers"`
	Duration   int32             `json:"duration"`
	// URL is the final URL after redirects (response.url); Redirects how many were followed
	// (response.redirects).
	URL       string `json:"url"`
	Redirects int    `json:"redirects"`
	// Stream is the streamed response's result; StreamFields exposes it to expressions.
	Stream *StreamResult `json:"-"`
}

func ConvertResponseToVar

func ConvertResponseToVar(r Response) ResponseVar

func (ResponseVar) StreamFields

func (r ResponseVar) StreamFields() map[string]any

StreamFields are the response fields a streamed response adds: text, events, event_count, ttft_ms (when a token arrived) and usage (when the stream reported it). Nil when the response was not streamed.

type StreamEvent

type StreamEvent struct {
	Event string `json:"event,omitempty"`
	Data  any    `json:"data"`
	ID    string `json:"id,omitempty"`
}

StreamEvent is one event of a streamed response. Data is the parsed JSON when the payload is JSON, else the text.

type StreamOptions

type StreamOptions struct {
	// Preset assembles the text; "" is raw sse.
	Preset string
	// Timeout is the longest the stream may stay open; 0 is DefaultStreamTimeout.
	Timeout time.Duration
}

StreamOptions asks for a response to be read as a stream.

type StreamResult

type StreamResult struct {
	Preset string `json:"preset"`
	// Text is the message assembled by the preset.
	Text string `json:"text"`
	// Events are the first MaxStreamEvents events; EventCount counts all of them.
	Events     []StreamEvent `json:"events,omitempty"`
	EventCount int           `json:"event_count"`
	// TTFT is the time from sending the request to the first event that added text (raw sse:
	// the first data event). Nil when none did.
	TTFT *time.Duration `json:"ttft,omitempty"`
	// Usage is the token usage the stream reported, merged across events.
	Usage map[string]any `json:"usage,omitempty"`
}

StreamResult is what a streamed response assembled.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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