Documentation
¶
Overview ¶
Package httpx is the SDK's single HTTP transport: one bounded POST attempt per call, no redirects, no retries, no replays, and failures that carry no URLs, headers of the request, credentials, causes or bodies.
It is neutral: it never imports the root batchdata package, and it returns a *Failure that the root maps onto its public errors.
Index ¶
- Constants
- func Do(ctx context.Context, hc *http.Client, cfg Config, path string, body []byte) (Response, *Failure)
- func NewClient(hc *http.Client) *http.Client
- func RetryAfter(h http.Header, now time.Time) (time.Duration, bool)
- func TestEndpoint(raw string) (host string, ok bool)
- func ValidCredential(s string) bool
- func ValidUserAgent(s string) bool
- type Config
- type Failure
- type FailureKind
- type Response
Constants ¶
const ( // MaxCredentialBytes bounds an API key. MaxCredentialBytes = 4096 // MaxUserAgentBytes bounds a User-Agent. MaxUserAgentBytes = 256 )
Limits on the values NewClient accepts.
const ( // MaxErrorBodyBytes bounds the excerpt read from a response whose status // is not 200. MaxErrorBodyBytes = 64 << 10 // MaxResponseHeaderBytes bounds the response headers on the transport the // SDK builds itself. MaxResponseHeaderBytes = 64 << 10 // MaxRetryAfter is the longest retry hint reported; larger hints are // clamped. MaxRetryAfter = 24 * time.Hour )
Transport limits.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
func Do(ctx context.Context, hc *http.Client, cfg Config, path string, body []byte) (Response, *Failure)
Do makes exactly one POST attempt of body to path on cfg's origin and reads the whole response, bounded by cfg.MaxBytes. It checks ctx immediately before the attempt and makes none when ctx is done. The timeout covers the request and the full body read; the caller's deadline still applies. The request has no GetBody, so the standard library cannot resend it, and the response body is closed on every path.
Exactly one of the results is meaningful: a Response for status 200, or a non-nil *Failure. hc is a client from NewClient.
func NewClient ¶
NewClient returns the client every call uses. A non-nil hc is copied with redirects disabled and no cookie jar: its Transport is shared and never mutated, its Timeout still applies, and hc itself is never modified. A nil hc yields a client on a new transport owned by the SDK, which mirrors http.DefaultTransport's settings, proxy variables included, and bounds response headers to MaxResponseHeaderBytes.
func RetryAfter ¶
RetryAfter parses the standard Retry-After header of h: integer delta-seconds or an HTTP-date relative to now. False means no usable hint: absent, empty or unparseable. Negative values and past dates clamp to 0, and hints above MaxRetryAfter, including counts too large to represent, clamp to MaxRetryAfter. An explicit "0" returns (0, true).
func TestEndpoint ¶
TestEndpoint validates a loopback base URL for tests and returns its host, such as "127.0.0.1:8443" or "[::1]:8443".
Only https is accepted, with a host that is a canonical IPv4 literal in 127.0.0.0/8 or exactly [::1], an optional numeric port, and no userinfo, query, fragment or path other than "/". Names such as "localhost" are rejected: they resolve through the system resolver and could leave the machine. ok is false for any other value.
func ValidCredential ¶
ValidCredential reports whether s is 1 to MaxCredentialBytes bytes of visible ASCII (0x21 to 0x7E): safe in a header, with no spaces or controls.
func ValidUserAgent ¶
ValidUserAgent reports whether s is 1 to MaxUserAgentBytes bytes of visible ASCII or spaces, starting and ending with a visible byte.
Types ¶
type Config ¶
type Config struct {
// Scheme and Host form the origin, such as "https" and
// "api.batchdata.com". Do builds the URL from them and the path as a
// url.URL, never by concatenating strings.
Scheme string
Host string
// Authorization is the full header value, such as "Bearer <token>".
Authorization string
// UserAgent is the User-Agent header value.
UserAgent string
// Timeout bounds the call, including reading the whole body. It must be
// positive.
Timeout time.Duration
// MaxBytes is the response body limit for a 200 response. It must be
// positive.
MaxBytes int64
}
Config is the immutable per-client setting of every call.
type Failure ¶
type Failure struct {
Kind FailureKind
// Status is the HTTP status, or 0 when no response arrived.
Status int
// Header is the response header, or nil when no response arrived.
Header http.Header
// BodyExcerpt is the body of a FailureStatus response when it was read
// completely within MaxErrorBodyBytes, and nil otherwise.
BodyExcerpt []byte
}
Failure is a call that did not produce a usable 200 response. It holds no cause: the underlying error, which may quote URLs, proxy credentials or network addresses, is dropped.
type FailureKind ¶
type FailureKind uint8
FailureKind classifies a Failure.
const ( // FailureCanceled: the caller's context was canceled, before or during // the call. FailureCanceled FailureKind = iota + 1 // FailureDeadlineExceeded: the caller's context deadline expired. FailureDeadlineExceeded // FailureTimeout: the call's own timeout, or the http.Client's Timeout, // expired first. FailureTimeout // FailureTransport: any other failure to complete the exchange. FailureTransport // FailureStatus: the response status was not 200. Header and // BodyExcerpt are set. FailureStatus // FailureTooLarge: a 200 response body exceeded MaxBytes, by its // declared length or by what was read. FailureTooLarge // FailureEncoding: a 200 response carried a Content-Encoding the // transport did not decode. FailureEncoding )
Failure kinds.