Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var BaseSender = Handler{ Name: "BaseSender", Fn: func(r *Request) { var err error r.HTTPResponse, err = r.HTTPClient.Do(r.HTTPRequest) if err != nil { handleSendError(r, err) } }, }
BaseSender sends a request using the http.Client.
var JSONBuilder = Handler{ Name: "JSONBuilder", Fn: func(r *Request) { r.HTTPRequest.Header.Set("Content-Type", "application/json") r.HTTPRequest.Header.Set("Accept", "application/json") if r.HTTPRequest.Method != "GET" && r.Params != nil { raw, err := json.Marshal(r.Params) if err != nil { r.Error = err return } r.HTTPRequest.ContentLength = int64(len(raw)) r.HTTPRequest.Body = ioutil.NopCloser(bytes.NewReader(raw)) } }, }
JSONBuilder adds standard JSON headers to the request and encodes Params as JSON to the request body if the request is not a GET.
var JSONDecoder = Handler{ Name: "JSONDecoder", Fn: func(r *Request) { if r.HTTPResponse == nil { return } if r.HTTPResponse.Body != nil { defer func() { io.Copy(ioutil.Discard, r.HTTPResponse.Body) r.HTTPResponse.Body.Close() }() } if r.Data == nil { return } decoder := json.NewDecoder(r.HTTPResponse.Body) r.Error = decoder.Decode(r.Data) if decoder.More() && r.Error == nil { r.Error = fmt.Errorf("Response includes more than one JSON object") } }, }
JSONDecoder decodes a response as JSON.
var RequestLogger = Handler{ Name: "RequestLogger", Fn: func(r *Request) { b, err := httputil.DumpRequestOut(r.HTTPRequest, true) if err != nil { fmt.Printf("error dumping request: %s\n", err.Error()) return } fmt.Println(string(b)) }, }
RequestLogger dumps the entire request to stdout.
var ResponseLogger = Handler{ Name: "ResponseLogger", Fn: func(r *Request) { b, err := httputil.DumpResponse(r.HTTPResponse, true) if err != nil { fmt.Printf("error dumping response: %s\n", err.Error()) return } fmt.Println(string(b)) }, }
ResponseLogger dumps the entire response to stdout.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
Handler defines a request handler.
func BasicAuther ¶
BasicAuther sets basic auth on a request.
func HeadersFromContext ¶
HeadersFromContext adds headers with values from the request context. This is useful for forwarding headers to upstream services.
func WithTracing ¶
WithTracing returns a Send Handler that wraps another Send Handler in a trace span.
type HandlerList ¶
type HandlerList struct {
// contains filtered or unexported fields
}
HandlerList manages a list of request handlers.
func NewHandlerList ¶
func NewHandlerList(hh ...Handler) HandlerList
NewHandlerList constructs a new HandlerList with the given handlers.
func (*HandlerList) Append ¶
func (hl *HandlerList) Append(h Handler)
Append adds a handler to the end of the list.
func (*HandlerList) Prepend ¶
func (hl *HandlerList) Prepend(h Handler)
Prepend adds a handler to the front of the list.
func (*HandlerList) Run ¶
func (hl *HandlerList) Run(r *Request)
Run calls each request handler in order.
type Handlers ¶
type Handlers struct { Build HandlerList Sign HandlerList Send HandlerList ValidateResponse HandlerList Decode HandlerList DecodeError HandlerList Complete HandlerList }
Handlers represents lists of request handlers for each phase in the request lifecycle.
func DefaultHandlers ¶
func DefaultHandlers() Handlers
DefaultHandlers defines a basic request configuration that assumes JSON requests and responses.
type Request ¶
type Request struct { ClientInfo metadata.ClientInfo // Metadata about the client generating this request. Time time.Time // The time the request was created. HTTPClient *http.Client // The underlying http.Client that will make the request. Handlers Handlers // Handlers contains the logic that manages the lifecycle of the request. HTTPRequest *http.Request // The http.Request object HTTPResponse *http.Response // The http.Response object, should be populated after Handlers.Send has run. Params interface{} // The input value to encode into the request. Data interface{} // The output value to decode the response into. Error error // Holds any error that occurs during request sending. // contains filtered or unexported fields }
Request manages the lifecycle of a client request.
func New ¶
func New(httpReq *http.Request, info metadata.ClientInfo, handlers Handlers, params interface{}, data interface{}) *Request
New creates a new Request.