transport

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package transport contains utility methods for API interfaces that speak to clients

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoRangeHeader is the error used when attempting to parse a non-existent
	// range header.
	ErrNoRangeHeader = errors.New("no range header provided")

	// ErrInvalidRange is the error used when a range value cannot be parsed.
	ErrInvalidRange = errors.New("invalid range")
)

Functions

func MethodNotAllowed

func MethodNotAllowed() http.Handler

MethodNotAllowed returns an http.Handler implementation to be used when an HTTP call for an existing resource is using an invalid method.

func NotFound

func NotFound() http.Handler

NotFound returns an http.Handler implementation to be used when a requested HTTP resource does not exist.

func RangeFromHeader

func RangeFromHeader(r *http.Request) (int64, int64, error)

RangeFromHeader determines the range of bytes to stream from the Range header.

func RangeFromValues

func RangeFromValues(start, end, size int64) string

RangeFromValues generates a Content-Range header using the provided values.

Types

type Error

type Error struct {
	Message string   `json:"message"`
	Stack   []string `json:"stack,omitempty"`
}

The Error type represents an error returned by the API.

func (Error) Error

func (e Error) Error() string

type HTTP

type HTTP struct{}

The HTTP type contains utilities for interacting with HTTP requests.

func (HTTP) Decode

func (t HTTP) Decode(r *http.Request, out interface{}) error

Decode the contents of the request body into the given interface and perform validation on the struct.

func (HTTP) Error

func (t HTTP) Error(ctx context.Context, w http.ResponseWriter, code int, msg string, args ...interface{})

Error returns a JSON-encoded error to the HTTP client.

func (HTTP) ErrorWithStack

func (t HTTP) ErrorWithStack(ctx context.Context, w http.ResponseWriter, code int, msg string, args ...interface{})

ErrorWithStack returns a JSON-encoded error to the HTTP client that contains a stack trace. Should be used for debugging and handling panics.

func (HTTP) Respond

func (t HTTP) Respond(ctx context.Context, w http.ResponseWriter, body interface{}, code int)

Respond to an HTTP request with the provided code and JSON-encoded response body.

Jump to

Keyboard shortcuts

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