Documentation ¶
Overview ¶
Package httputil provides utilities on top of the net/http package.
Index ¶
- func CheckOKResponse(r *http.Response) error
- func CheckResponse(r *http.Response) error
- func EncodeJSONResponse(encodeErr httptransport.ErrorEncoder) httptransport.EncodeResponseFunc
- func JSONErrorEncoder(statusCoder func(err error) int) httptransport.ErrorEncoder
- func NewServer(addr string, h http.Handler, opts ...Option) *http.Server
- type Middleware
- type Option
- type SerializerFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckOKResponse ¶
CheckOKResponse checks if the status code is within 2XX range
func CheckResponse ¶
CheckResponse checks the API response for errors, and returns them if present. A response is considered an error if it has a status code outside the 200 range or equal to 202 Accepted. API error responses are expected to have either no response body, or a JSON response body that maps to ErrorResponse. Any other response body will be silently ignored.
The error type will be *TwoFactorAuthError for two-factor authentication errors.
func EncodeJSONResponse ¶
func EncodeJSONResponse(encodeErr httptransport.ErrorEncoder) httptransport.EncodeResponseFunc
EncodeJSONResponse encodes a response using the appropriate serializer function
func JSONErrorEncoder ¶
func JSONErrorEncoder(statusCoder func(err error) int) httptransport.ErrorEncoder
JSONErrorEncoder takes in a status coder and returns an HTTP error encoder
Types ¶
type Middleware ¶
Middleware is a chainable decorator for HTTP Handlers.
func Chain ¶
func Chain(outer Middleware, others ...Middleware) Middleware
Chain is a helper function for composing middlewares. Requests will traverse them in the order they're declared. That is, the first middleware is treated as the outermost middleware.
Chain is identical to the go-kit helper for Endpoint Middleware.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" ) func main() { h := Chain( annotate("one"), annotate("two"), annotate("three"), )(myHandler()) srv := httptest.NewServer(h) defer srv.Close() if _, err := http.Get(srv.URL); err != nil { panic(err) } } func annotate(s string) Middleware { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Println("annotate: ", s) next.ServeHTTP(w, r) }) } } func myHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { }) }
Output: annotate: one annotate: two annotate: three
type SerializerFunc ¶
type SerializerFunc func(w http.ResponseWriter, v interface{}) error
SerializerFunc serializes a specific response