context

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2017 License: MIT, MIT Imports: 4 Imported by: 0

README

gentleman/context Build Status GoDoc API Go Report Card

Package context implements a simple request-aware HTTP context used by plugins and exposed by the middleware layer, designed to share polymorfic data types across plugins in the middleware call chain.

context is not thread-safe by default.

Installation

go get -u gopkg.in/h2non/gentleman.v1/context

API

See godoc reference.

License

MIT - Tomas Aparicio

Documentation

Overview

Package context implements a simple request-aware HTTP context used by plugins and exposed by the middleware layer, designed to share polymorfic data types across plugins in the middleware call chain.

Context is not thread-safe by default. In case that you support multithread programming in plugins you have to use locks/mutex accordingly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// Stores the last error for the current Context
	Error error

	// Flag if the HTTP transaction was explicitly stopped in some phase
	Stopped bool

	// Context can inherit behavior and data from another Context
	Parent *Context

	// Reference to the http.Client used in the current HTTP transaction
	Client *http.Client

	// Reference to the http.Request used in the current HTTP transaction
	Request *http.Request

	// Reference to the http.Response used in the current HTTP transaction
	Response *http.Response
}

Context encapsulates required domain-specific HTTP entities to share data and entities for HTTP transactions in a middleware chain

func New

func New() *Context

New creates an empty default Context

func (*Context) Clear

func (c *Context) Clear()

Clear clears all stored values from a request’s context

func (*Context) Clone

func (c *Context) Clone() *Context

Clone returns a clone of the current context.

func (*Context) Copy

func (c *Context) Copy(req *http.Request)

Copy copies the current context data into a new http.Request

func (*Context) Delete

func (c *Context) Delete(key interface{})

Delete deletes a stored value from a request’s context

func (*Context) Get

func (c *Context) Get(key interface{}) interface{}

Get gets a value by key in the current or parent context

func (*Context) GetAll

func (c *Context) GetAll() map[interface{}]interface{}

GetAll returns all stored context values for a request. Will always return a valid map. Returns an empty map for requests context data previously set

func (*Context) GetOk

func (c *Context) GetOk(key interface{}) (interface{}, bool)

GetOk gets a context value from req. Returns (nil, false) if key not found in the request context.

func (*Context) GetString

func (c *Context) GetString(key interface{}) string

GetString gets a string context value from req. Returns an empty string if key not found in the request context, or the value does not evaluate to a string

func (*Context) Root

func (c *Context) Root() *Context

Root returns the root Context looking in the parent contexts recursively. If the current context has no parent context, it will return the Context itself.

func (*Context) Set

func (c *Context) Set(key interface{}, value interface{})

Set sets a value on the current store

func (*Context) SetRequest

func (c *Context) SetRequest(req *http.Request)

SetRequest replaces the context http.Request

func (*Context) UseParent

func (c *Context) UseParent(ctx *Context)

UseParent uses a new parent Context

func (*Context) WrapBody added in v1.0.3

func (c *Context) WrapBody(body io.ReadCloser) ReadCloser

WrapBody wraps and copies the current context data to a new request body. Since context metadata is "overloaded" as part of the request body reader calling this method is mandatory for any new request body.

type Handler

type Handler interface {
	// Next handler invokes the next plugin in the middleware
	// call chain with the given Context
	Next(*Context)

	// Stop handler stops the current middleware call chain
	// with the given Context
	Stop(*Context)

	// Error handler reports an error and stops the middleware
	// call chain with the given Context
	Error(*Context, error)
}

Handler exposes a simple interface providing control flow capabilities to middleware functions

func NewHandler

func NewHandler(fn HandlerCtx) Handler

NewHandler creates a new Handler based on a given HandlerCtx function

type HandlerCtx

type HandlerCtx func(*Context)

HandlerCtx represents a Context only function handler

type HandlerFunc

type HandlerFunc func(*Context, Handler)

HandlerFunc represents a middleware function handler interface

type ReadCloser

type ReadCloser interface {
	io.ReadCloser
	Clone() ReadCloser
	Context() map[interface{}]interface{}
	SetContext(map[interface{}]interface{})
}

ReadCloser augments the io.ReadCloser interface with a Context() method

Jump to

Keyboard shortcuts

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