http

package
v0.0.0-...-f3120f0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2016 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package http provides a general purpose HTTP binding for endpoints.

Index

Constants

View Source
const (
	// DomainNewRequest is an error during request generation.
	DomainNewRequest = "NewRequest"

	// DomainEncode is an error during request or response encoding.
	DomainEncode = "Encode"

	// DomainDo is an error during the execution phase of the request.
	DomainDo = "Do"

	// DomainDecode is an error during request or response decoding.
	DomainDecode = "Decode"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps a URL and provides a method that implements endpoint.Endpoint.

func NewClient

func NewClient(
	method string,
	tgt *url.URL,
	enc EncodeRequestFunc,
	dec DecodeResponseFunc,
	options ...ClientOption,
) *Client

NewClient constructs a usable Client for a single remote method.

func (Client) Endpoint

func (c Client) Endpoint() endpoint.Endpoint

Endpoint returns a usable endpoint that invokes the remote endpoint.

type ClientOption

type ClientOption func(*Client)

ClientOption sets an optional parameter for clients.

func BufferedStream

func BufferedStream(buffered bool) ClientOption

BufferedStream sets whether the Response.Body is left open, allowing it to be read from later. Useful for transporting a file as a buffered stream.

func ClientAfter

func ClientAfter(after ...ClientResponseFunc) ClientOption

ClientAfter sets the ClientResponseFuncs applied to the incoming HTTP request prior to it being decoded. This is useful for obtaining anything off of the response and adding onto the context prior to decoding.

func ClientBefore

func ClientBefore(before ...RequestFunc) ClientOption

ClientBefore sets the RequestFuncs that are applied to the outgoing HTTP request before it's invoked.

func SetClient

func SetClient(client *http.Client) ClientOption

SetClient sets the underlying HTTP client used for requests. By default, http.DefaultClient is used.

type ClientResponseFunc

type ClientResponseFunc func(context.Context, *http.Response) context.Context

ClientResponseFunc may take information from an HTTP request and make the response available for consumption. ClientResponseFuncs are only executed in clients, after a request has been made, but prior to it being decoded.

type DecodeRequestFunc

type DecodeRequestFunc func(context.Context, *http.Request) (request interface{}, err error)

DecodeRequestFunc extracts a user-domain request object from an HTTP request object. It's designed to be used in HTTP servers, for server-side endpoints. One straightforward DecodeRequestFunc could be something that JSON decodes from the request body to the concrete response type.

type DecodeResponseFunc

type DecodeResponseFunc func(context.Context, *http.Response) (response interface{}, err error)

DecodeResponseFunc extracts a user-domain response object from an HTTP response object. It's designed to be used in HTTP clients, for client-side endpoints. One straightforward DecodeResponseFunc could be something that JSON decodes from the response body to the concrete response type.

type EncodeRequestFunc

type EncodeRequestFunc func(context.Context, *http.Request, interface{}) error

EncodeRequestFunc encodes the passed request object into the HTTP request object. It's designed to be used in HTTP clients, for client-side endpoints. One straightforward EncodeRequestFunc could something that JSON encodes the object directly to the request body.

type EncodeResponseFunc

type EncodeResponseFunc func(context.Context, http.ResponseWriter, interface{}) error

EncodeResponseFunc encodes the passed response object to the HTTP response writer. It's designed to be used in HTTP servers, for server-side endpoints. One straightforward EncodeResponseFunc could be something that JSON encodes the object directly to the response body.

type Error

type Error struct {
	// Domain is the phase in which the error was generated.
	Domain string

	// Err is the concrete error.
	Err error
}

Error is an error that occurred at some phase within the transport.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

type ErrorEncoder

type ErrorEncoder func(ctx context.Context, err error, w http.ResponseWriter)

ErrorEncoder is responsible for encoding an error to the ResponseWriter.

In the server implementation, only kit/transport/http.Error values are ever passed to this function, so you might be tempted to have this function take one of those directly. But, users are encouraged to use custom ErrorEncoders to encode all HTTP errors to their clients, and so may want to pass and check for their own error types. See the example shipping/handling service.

type RequestFunc

type RequestFunc func(context.Context, *http.Request) context.Context

RequestFunc may take information from an HTTP request and put it into a request context. In Servers, RequestFuncs are executed prior to invoking the endpoint. In Clients, RequestFuncs are executed after creating the request but prior to invoking the HTTP client.

func SetRequestHeader

func SetRequestHeader(key, val string) RequestFunc

SetRequestHeader returns a RequestFunc that sets the specified header.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server wraps an endpoint and implements http.Handler.

func NewServer

func NewServer(
	ctx context.Context,
	e endpoint.Endpoint,
	dec DecodeRequestFunc,
	enc EncodeResponseFunc,
	options ...ServerOption,
) *Server

NewServer constructs a new server, which implements http.Server and wraps the provided endpoint.

func (Server) ServeHTTP

func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type ServerOption

type ServerOption func(*Server)

ServerOption sets an optional parameter for servers.

func ServerAfter

func ServerAfter(after ...ServerResponseFunc) ServerOption

ServerAfter functions are executed on the HTTP response writer after the endpoint is invoked, but before anything is written to the client.

func ServerBefore

func ServerBefore(before ...RequestFunc) ServerOption

ServerBefore functions are executed on the HTTP request object before the request is decoded.

func ServerErrorEncoder

func ServerErrorEncoder(ee ErrorEncoder) ServerOption

ServerErrorEncoder is used to encode errors to the http.ResponseWriter whenever they're encountered in the processing of a request. Clients can use this to provide custom error formatting and response codes. By default, errors will be written as plain text with an appropriate, if generic, status code.

func ServerErrorLogger

func ServerErrorLogger(logger log.Logger) ServerOption

ServerErrorLogger is used to log non-terminal errors. By default, no errors are logged.

type ServerResponseFunc

type ServerResponseFunc func(context.Context, http.ResponseWriter) context.Context

ServerResponseFunc may take information from a request context and use it to manipulate a ResponseWriter. ServerResponseFuncs are only executed in servers, after invoking the endpoint but prior to writing a response.

func SetContentType

func SetContentType(contentType string) ServerResponseFunc

SetContentType returns a ResponseFunc that sets the Content-Type header to the provided value.

func SetResponseHeader

func SetResponseHeader(key, val string) ServerResponseFunc

SetResponseHeader returns a ResponseFunc that sets the specified header.

Jump to

Keyboard shortcuts

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