apihttp

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeInternalError         = "internal_error"
	CodeInvalidRequestBody    = "invalid_request_body"
	CodeRequestTooLarge       = "request_too_large"
	CodeInvalidQueryParam     = "invalid_query_param"
	CodeMethodNotAllowed      = "method_not_allowed"
	CodeUnauthorized          = "unauthorized"
	CodeForbidden             = "forbidden"
	CodeNotFound              = "not_found"
	CodeConflict              = "conflict"
	CodeServiceUnavailable    = "service_unavailable"
	CodeQueryFailed           = "query_failed"
	CodeAuthFailed            = "auth_failed"
	CodePlatformUnavailable   = "platform_unavailable"
	CodeKubernetesUnavailable = "kubernetes_unavailable"
	CodeInvalidVersionTag     = "invalid_version_tag"
	CodeTooManyRequests       = "too_many_requests"
)

Stable machine-readable error codes. Wording in human messages may change; codes must not be derived from message text at runtime.

View Source
const (
	DefaultListLimit = 50
	MaxListLimit     = 200
)
View Source
const ApplyMaxBytes = 64 * 1024

Variables

This section is empty.

Functions

func EncodeCursor

func EncodeCursor(offset int) string

EncodeCursor returns the next opaque cursor for a list page.

func NextLink(r *http.Request, nextCursor string, limit int) string

NextLink builds an absolute or path-only next URL with cursor and limit query params.

func ParseCursor

func ParseCursor(raw string) (int, error)

ParseCursor decodes an opaque cursor or returns offset 0 when absent.

func ParseLimit

func ParseLimit(r *http.Request, defaultLimit, maxLimit int) (int, error)

ParseLimit reads limit with defaults and strict bounds. Invalid values return an *Error.

func QueryInt

func QueryInt(r *http.Request, key string, fallback, minValue, maxValue int) (int, error)

QueryInt parses a required-shape integer query param. Empty uses fallback. Non-integer or out-of-range values return *Error with CodeInvalidQueryParam.

func QueryRFC3339Time

func QueryRFC3339Time(r *http.Request, key string) (time.Time, error)

QueryRFC3339Time parses an optional RFC3339 timestamp query param.

func WriteBodyDecodeError

func WriteBodyDecodeError(w http.ResponseWriter, err error)

WriteBodyDecodeError maps decode failures to the standard error envelope.

func WriteEnvelope

func WriteEnvelope(w http.ResponseWriter, status int, code, message string)

WriteEnvelope writes {"error":"<code>","message":"<description>"}.

func WriteError

func WriteError(w http.ResponseWriter, logger *zap.Logger, err error)

WriteError renders err as the standard JSON error envelope.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, payload any)

WriteJSON writes a success payload with Content-Type application/json.

func WriteMethodNotAllowed

func WriteMethodNotAllowed(w http.ResponseWriter, methods ...string)

WriteMethodNotAllowed sets Allow and returns method_not_allowed.

Types

type CursorPayload

type CursorPayload struct {
	Offset int `json:"offset"`
}

CursorPayload is the opaque cursor encoding (base64url JSON).

type Error

type Error struct {
	Status  int
	Code    string
	Message string
	Cause   error
}

Error is a safe, typed HTTP error for JSON API responses.

func BadRequest

func BadRequest(code, message string, cause ...error) *Error

func Conflict

func Conflict(message string, cause ...error) *Error

func Forbidden

func Forbidden(message string, cause ...error) *Error

func Internal

func Internal(message string, cause ...error) *Error

func NotFound

func NotFound(message string, cause ...error) *Error

func ServiceUnavailable

func ServiceUnavailable(code, message string, cause ...error) *Error

func Unauthorized

func Unauthorized(message string, cause ...error) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Meta

type Meta struct {
	Limit      int    `json:"limit"`
	NextCursor string `json:"next_cursor,omitempty"`
	HasMore    bool   `json:"has_more"`
}

Meta accompanies list responses.

func ListMeta

func ListMeta(limit, offset, returned int) Meta

ListMeta builds pagination metadata for handlers that fetch exactly `limit` rows. HasMore is therefore optimistic on exact-fit pages unless the caller uses a limit+1 sentinel query upstream.

Jump to

Keyboard shortcuts

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