rest

package
v0.0.0-...-9491308 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2020 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Overview

Package rest contains helpers for rest clients and servers.

Index

Constants

This section is empty.

Variables

View Source
var ErrRequest = errors.New("invalid request")

ErrRequest happens when a HTTP request is invalid.

Functions

func Dir

func Dir(useLocal bool, name string) http.FileSystem

Dir returns a http.Filesystem for the embedded assets on a given prefix dir. If useLocal is true, the filesystem's contents are instead used.

func FS

func FS(useLocal bool) http.FileSystem

FS returns a http.Filesystem for the embedded assets. If useLocal is true, the filesystem's contents are instead used.

func FSByte

func FSByte(useLocal bool, name string) ([]byte, error)

FSByte returns the named file from the embedded assets. If useLocal is true, the filesystem's contents are instead used.

func FSMustByte

func FSMustByte(useLocal bool, name string) []byte

FSMustByte is the same as FSByte, but panics if name is not present.

func FSMustString

func FSMustString(useLocal bool, name string) string

FSMustString is the string version of FSMustByte.

func FSString

func FSString(useLocal bool, name string) (string, error)

FSString is the string version of FSByte.

func GoSendAll

func GoSendAll(okCode int, log func(string, ...interface{}), clients ...ClientRequest)

GoSendAll sends all ClientRequests. The log parameter is expected to have fmt.Printf like functionality and log errors somewhere is not nil.

func SendAll

func SendAll(okCode int, clients ...ClientRequest) error

SendAll sends all ClientRequests and returns the error.

Types

type Client

type Client interface {
	// Request begins a new HTTP request.
	Request(ctx context.Context, url string, method string) ClientRequest

	// RoundTripper returns the used transport for the Client.
	RoundTripper() http.RoundTripper
}

Client is an improved http client.

func NewClient

func NewClient(tls *tls.Config) Client

NewClient creates a new improved http client.

type ClientRequest

type ClientRequest interface {
	// JSONBody includes a JSON string into a HTTP body request.
	JSONBody(data interface{}) ClientRequest

	// StringBody includes a string into a HTTP body request.
	StringBody(data string) ClientRequest

	// Header adds a header to the request.
	Header(key string, value ...string) ClientRequest

	// Send a request on its way.
	Send(okCode ...int) ClientResponse
}

ClientRequest is an improved HTTP client request.

type ClientResponse

type ClientResponse interface {
	// JSONBody extracts a JSON string from a HTTP body request.
	JSONBody(data interface{}) ClientResponse

	// ByteSliceBody extracts a byte slice from a HTTP body request.
	ByteSliceBody(data *[]byte) ClientResponse

	// StringBody extracts a string from a HTTP body request.
	StringBody(dst *string) ClientResponse

	// Check finalizes the request and returns an error on failure or unexpected status code.
	Check() error
}

ClientResponse represents an received HTTP response.

type HTTPError

type HTTPError struct {
	URL        string
	StatusCode int
	Text       string
}

HTTPError represents an HTTP error in combination with an HTTP status code.

func (HTTPError) Error

func (h HTTPError) Error() string

Error returns the error as string.

type Mux

type Mux interface {
	// Forward a path to some other host.
	Forward(client Client, path, host, redirect string)
	// AddDefaultResponseHeader to the given header.
	AddDefaultResponseHeader(header http.Header)
	// Endpoint provides a server end point for a rest application. The given handler is called on each invoction.
	Endpoint(path string, queryHandler func(query *Request))
	// ServeHTTP just calls net/http.ServeMux.ServeHTTP.
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	// Handle just calls net/http.ServeMux.Handle.
	Handle(pattern string, handler http.Handler)
	// HandleFunc just calls net/http.ServeMux.HandleFunc.
	HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}

Mux is an http ServeMux with extra methods.

func NewMux

func NewMux() Mux

NewMux creates a new improves mux.

type Request

type Request struct {
	Ctx                                 context.Context
	URL                                 url.URL
	RequestBody                         []byte
	ResponseBody                        []byte
	Status                              int
	RequestErr, InternalErr, GatewayErr error
	HasArgs                             bool
}

Request contains information from a request and how to handle it.

func (*Request) Args

func (r *Request) Args(target interface{}) error

Args are parsed from the url into the given struct.

Jump to

Keyboard shortcuts

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