request

package
v0.0.0-...-2b7dcb4 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: BSD-2-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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

type Handler struct {
	Name string
	Fn   func(*Request)
}

Handler defines a request handler.

func BasicAuther

func BasicAuther(username, password string) Handler

BasicAuther sets basic auth on a request.

func HeadersFromContext

func HeadersFromContext(headers ...string) Handler

HeadersFromContext adds headers with values from the request context. This is useful for forwarding headers to upstream services.

func RequestSigner

func RequestSigner(id, key string) Handler

RequestSigner signs requests.

func WithTracing

func WithTracing(h Handler) Handler

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) Clear

func (hl *HandlerList) Clear()

Clear truncates a handler 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.

func (Handlers) Copy

func (h Handlers) Copy() Handlers

Copy returns a copy of a Handlers instance.

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.

func (*Request) Build

func (r *Request) Build()

Build runs build handlers and then runs sign handlers.

func (*Request) Send

func (r *Request) Send() error

Send sends a request. Send manages the execution of the Handlers. If an error occurs during any phase, request processing stops.

Jump to

Keyboard shortcuts

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