Documentation ¶
Overview ¶
package httpx provides an extra layer of convenience over package http.
Index ¶
- Variables
- func EncodeError(err error, rw http.ResponseWriter)
- func Error(ctx context.Context, err error, rw http.ResponseWriter, r *http.Request)
- func ErrorStatusCode(err error) int
- func Header(ctx context.Context, headerKey string) string
- func NewJSONRequest(method, path string, v interface{}) (*http.Request, error)
- func NotFound(ctx context.Context, w http.ResponseWriter, r *http.Request) error
- func ParseURL(baseURL *url.URL, path string) string
- func RequestID(ctx context.Context) string
- func URLWithoutCreds(u url.URL) string
- func Vars(ctx context.Context) map[string]string
- func WithHeader(ctx context.Context, headerKey string, headerValue string) context.Context
- func WithRequestID(ctx context.Context, requestID string) context.Context
- func WithRoute(ctx context.Context, r *Route) context.Context
- func WithVars(ctx context.Context, vars map[string]string) context.Context
- type Client
- type HTTPError
- type Handler
- type HandlerFunc
- type RequestIDTransport
- type RetryTransport
- type RetryableHTTPError
- type RoundTripper
- type Route
- func (r *Route) GetName() string
- func (r *Route) GetPathTemplate() string
- func (r *Route) Handler(h Handler) *Route
- func (r *Route) HandlerFunc(f func(context.Context, http.ResponseWriter, *http.Request) error) *Route
- func (r *Route) Methods(methods ...string) *Route
- func (r *Route) Name(name string) *Route
- func (r *Route) URL(pairs ...string) (*url.URL, error)
- func (r *Route) URLPath(pairs ...string) (*url.URL, error)
- type Router
- func (r *Router) Handle(path string, h Handler) *Route
- func (r *Router) HandleFunc(path string, f func(context.Context, http.ResponseWriter, *http.Request) error) *Route
- func (r *Router) Handler(req *http.Request) (route *Route, h Handler, vars map[string]string)
- func (r *Router) Headers(pairs ...string) *Route
- func (r *Router) Match(f func(*http.Request) bool, h Handler)
- func (r *Router) Path(path string) *Route
- func (r *Router) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, req *http.Request) error
- type Transport
Constants ¶
This section is empty.
Variables ¶
var DefaultHTTPClient = &http.Client{ Transport: DefaultHTTPTransport, }
Functions ¶
func EncodeError ¶
func EncodeError(err error, rw http.ResponseWriter)
func ErrorStatusCode ¶
func NewJSONRequest ¶
NewJSONRequest generates a new http.Request with the body set to the json encoding of v.
func URLWithoutCreds ¶
Shows the URL without information about the current username and password
func WithHeader ¶
func WithRequestID ¶
WithRequestID inserts a RequestID into the context.
Types ¶
type Client ¶
type Client struct {
Transport RoundTripper
}
Client is an extension of http.Client with context.Context support.
func NewClient ¶
NewClient returns a new Client instance that will use the given http.Client to perform round trips
func NewDefaultServiceClient ¶
func NewServiceClient ¶
NewServiceClient returns an httpx.Client that has the following behavior:
- Request ids will be added to outgoing requests within the X-Request-Id header.
- Any 500 errors will be retried.
The optional *http.Client parameter can be used to override the default client.
type Handler ¶
type Handler interface {
ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request) error
}
Handler is represents a Handler that can take a context.Context as the first argument.
type HandlerFunc ¶
The HandlerFunc type is an adapter to allow the use of ordinary functions as httpx handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
func (HandlerFunc) ServeHTTPContext ¶
func (f HandlerFunc) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error
ServeHTTPContext calls f(ctx, w, r)
type RequestIDTransport ¶
type RequestIDTransport struct {
Transport RoundTripper
}
RequestIDTransport is an http.RoundTripper implementation that adds the embedded request id to outgoing http requests.
type RetryTransport ¶
type RetryTransport struct { *retry.Retrier MethodsToRetry map[string]bool Transport RoundTripper }
RetryTransport is an implementation of the RoundTripper interface that retries requests.
func NewRetryTransport ¶
func NewRetryTransport(retrier *retry.Retrier, transport RoundTripper) *RetryTransport
NewRetryTransport returns a RetryTransport that will retry idempotent HTTP requests (GET/HEAD) using the given retrier.
type RetryableHTTPError ¶
RetryableHTTPError is used to represent error codes that can be allowed to retry
func (*RetryableHTTPError) Error ¶
func (e *RetryableHTTPError) Error() string
type RoundTripper ¶
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route wraps a mux.Route.
func RouteFromContext ¶
RouteFromContext extracts the current Route from a context.Context.
func (*Route) GetPathTemplate ¶
Returns the path template for this route, if any.
func (*Route) HandlerFunc ¶
func (r *Route) HandlerFunc(f func(context.Context, http.ResponseWriter, *http.Request) error) *Route
HandlerFunc sets the httpx.Handler for this route.
func (*Route) Methods ¶
Methods adds a matcher for HTTP methods. It accepts a sequence of one or more methods to be matched, e.g.: "GET", "POST", "PUT".
type Router ¶
type Router struct { // NotFoundHandler is a Handler that will be called when a route is not // found. NotFoundHandler Handler // contains filtered or unexported fields }
Router is an httpx.Handler router.
func (*Router) HandleFunc ¶
func (r *Router) HandleFunc(path string, f func(context.Context, http.ResponseWriter, *http.Request) error) *Route
HandleFunc registers a new route with a matcher for the URL path
func (*Router) Handler ¶
Handler returns a Handler that can be used to serve the request. Most of this is pulled from http://goo.gl/tyxad8.
func (*Router) ServeHTTPContext ¶
func (r *Router) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, req *http.Request) error
ServeHTTPContext implements the Handler interface.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
package errors provides error handling primitives in a request context.
|
package errors provides error handling primitives in a request context. |