Help us shape the future of Go! Take the 2026 Go Developer Survey

httpjson

package
v0.19.7 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package httpjson is the JSON-over-HTTP round trip and retry loop shared by the chat adapters' platform API clients.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientOr

func ClientOr(client *http.Client) *http.Client

ClientOr returns client, or a client with the adapters' default 30s timeout when it is nil.

func Redact

func Redact(err error, secret string) error

Redact replaces secret in err's text so a credential embedded in a request URL never reaches logs.

func Retry

func Retry(ctx context.Context, attempts int, sleep func(context.Context, time.Duration) error, try func() error, pause func(err error, attempt int) (time.Duration, bool)) error

Retry calls try up to attempts times. After a failure, pause reports whether to retry and how long to wait first, given the failed attempt's 0-based index; a non-positive wait retries at once.

func RetryAfter

func RetryAfter(header http.Header) time.Duration

RetryAfter parses a Retry-After header given in seconds, at most an hour; zero when absent, malformed, not positive or not finite.

func Snippet

func Snippet(body []byte) string

Snippet bounds an error body quoted into an error message.

Types

type Response

type Response struct {
	Status int
	Header http.Header
	Body   []byte
}

Response is a completed exchange with its body read (at most 4 MiB).

func Do

func Do(ctx context.Context, client *http.Client, method, url string, payload, out any, header ...string) (*Response, error)

Do sends payload as a JSON body (no body when nil, a reader as is) with the given header name/value pairs, which override the default Content-Type, and reads the response. On a 2xx status a non-empty body is decoded into out when out is non-nil. Errors name the failing stage; transport errors pass through.

func Get

func Get(ctx context.Context, client *http.Client, url string, header ...string) (ok *http.Response, failed *Response, err error)

Get sends a GET with the given header pairs. A 200 OK returns the open response for the caller to stream and close; any other status is read, closed and returned as failed.

func (*Response) OK

func (r *Response) OK() bool

OK reports a 2xx status.

Jump to

Keyboard shortcuts

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