http

package
v0.3.0-20261107022942-... Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 7 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

Overview

Package http provides shared HTTP client helpers (e.g. base-URL transport) for code that calls external HTTP APIs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(rawBaseURL string) (*nethttp.Client, error)

NewClient builds an *nethttp.Client with BaseURLTransport configured. Callers are responsible for layering additional transports (e.g. auth) and setting Timeout on the returned client.

func SendRequest

func SendRequest(ctx context.Context, client *nethttp.Client, method, rawURL string, body []byte, setHeaders func(*nethttp.Request)) (statusCode int, respBody []byte, err error)

SendRequest builds a request from method, rawURL, and body, applies setHeaders to it (when non-nil), sends it via client, and returns the response status code and full body. It does not interpret the status code or decode the body — success, retry, and not-found semantics vary by API, so that judgment stays with the caller.

Types

type BaseURLTransport

type BaseURLTransport struct {
	// BaseURL is the API base URL (e.g. "https://api.github.com").
	BaseURL *url.URL
	// Next is the underlying RoundTripper. Defaults to nethttp.DefaultTransport if nil.
	Next nethttp.RoundTripper
}

BaseURLTransport is a nethttp.RoundTripper that rewrites every request URL to resolve against a fixed base URL. This allows callers to make requests with relative paths (e.g. "/graphql") and have the transport prepend the configured base URL transparently.

func (*BaseURLTransport) RoundTrip

func (t *BaseURLTransport) RoundTrip(req *nethttp.Request) (*nethttp.Response, error)

RoundTrip rewrites req.URL to resolve against BaseURL, then delegates to Next. The base URL path and request path are joined explicitly so that base URLs with a path component (e.g. "https://ghe.example.com/api") are handled correctly regardless of whether the request path starts with "/".

type StatusError

type StatusError struct {
	// StatusCode is the HTTP status code from the response.
	StatusCode int
	// Body is the response body as read from the wire, or empty when the
	// caller had no body to attach. Error() renders at most
	// _maxRenderedBodyBytes of it.
	Body string
}

StatusError reports a response whose status code the caller rejected.

SendRequest does not build this error itself: which codes count as success varies by API — a 404 may be a sentinel, a 422 may be a no-op — so the caller still decides. What the caller should not do is report the rejection with a plain fmt.Errorf. The status code is the only thing that says whether a retry has any chance of working, and a formatted string throws it away. Returning this type keeps the code in the error chain, where platform/errs/http can read it and classify the failure.

Use it for the "this status is a failure" branch of a response check:

if status < 200 || status >= 300 {
    return phttp.NewStatusError(status, respBody)
}

func NewStatusError

func NewStatusError(statusCode int, body []byte) *StatusError

NewStatusError returns a StatusError for the given code and response body. body may be nil.

func (*StatusError) Error

func (e *StatusError) Error() string

Error renders the status and, when present, the response body truncated to _maxRenderedBodyBytes. Callers are expected to wrap it with the operation that failed, giving messages like "get build org/pipeline/builds/123: unexpected status 502: proxy forward failed".

Jump to

Keyboard shortcuts

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