{"modulePath":"github.com/delicb/cliware","version":"v0.1.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package cliware\n\nPackage cliware defines base type for HTTP client middleware support.\n\nCliware does very little on its own. Main purpose is for other packages to implement useful HTTP client middlewares and to write clients using cliware and set of useful middlewares. That being said, cliware can be used on its own with GoLang standard library.\n\nJust as net/http package defines Handler interface that is used to write HTTP server handlers and compose them, this package defines similar concept but for HTTP client. net/http.Handler interface enabled a lot of third party middlewares to be written that are very useful. This package has the same idea. It provides type definition and some useful helper stuff around it (like ability to chain middlewares) in hope that other developer will catch up and write useful middlewares for everyone to use.\n\n## Functions\n\n```go\nfunc EmptyRequest() *http.Request\n```\nEmptyRequest creates new empty instance of \\*http.Request. It is good starting point for initial request instance for middleware chain. In contrast to http.NewRequest, this function does not require any parameters. Any value can be overridden by middlewares. Request method is set to GET, just because it is sane default.\n\n## Types\n\n```go\ntype Chain struct {\n\t// contains filtered or unexported fields\n}\n```\nChain is Middleware implementation capable of executing multiple middlewares. On top of that, chain is aware of its parent middleware and executes it during its own execution.\n\n```go\nfunc NewChain(middlewares ...Middleware) *Chain\n```\nNewChain creates and returns middleware chain with provided middlewares and no parent. If you need to create new chain with some parent, use ChildChain method.\n\n```go\nfunc (c *Chain) ChildChain(middlewares ...Middleware) *Chain\n```\nChildChain creates new Middleware chain with current chain as parent.\n\n```go\nfunc (c *Chain) Copy() *Chain\n```\nCopy creates new chain with all middlewares copied to it.\n\n```go\nfunc (c *Chain) Exec(handler Handler) Handler\n```\nExec is implementation of Middleware interface that executes all middlewares in chain, including parent middleware.\n\n```go\nfunc (c *Chain) Middlewares() []Middleware\n```\nMiddlewares returns all middlewares for this chain. Parent middlewares not included.\n\n```go\nfunc (c *Chain) Parent() Middleware\n```\nParent returns parent middleware of this chain.\n\n```go\nfunc (c *Chain) Use(m ...Middleware)\n```\nUse adds provided middleware to current middleware chain.\n\n```go\nfunc (c *Chain) UseFunc(m func(handler Handler) Handler)\n```\nUseFunc adds provided function to current middleware chain.\n\n```go\nfunc (c *Chain) UseRequest(m func(req *http.Request) error)\n```\nUseRequest adds provided function as request middleware.\n\n```go\nfunc (c *Chain) UseResponse(m func(resp *http.Response, err error) error)\n```\nUseResponse add provided function as response middleware.\n\n```go\ntype ContextProcessor func(ctx context.Context) context.Context\n```\nContextProcessor is function for managing request context. It is intended as form of simple middleware for middlewares that only need to modify context before sending request.\n\n```go\nfunc (cp ContextProcessor) Exec(handler Handler) Handler\n```\nExec is implementation of Middleware interface.\n\n```go\ntype Handler interface {\n\t// Handle executes HTTP request based on provided parameters.\n\t// Depending on implementation, provided context might or not be used.\n\t// However, if handler really performs HTTP request (as opposed to doing\n\t// some middleware work, like logging or similar), provided context SHOULD\n\t// be used on request as intended by http.Request.WithRequest method.\n\tHandle(req *http.Request) (resp *http.Response, err error)\n}\n```\nHandler is interface that defines processing HTTP client request.\n\n```go\ntype HandlerFunc func(req *http.Request) (resp *http.Response, err error)\n```\nHandlerFunc is function variant of RequestHandler interface.\n\n```go\nfunc (rhf HandlerFunc) Handle(req *http.Request) (resp *http.Response, err error)\n```\nHandle is implementation of RequestHandler interface\n\n```go\ntype Middleware interface {\n\t// Exec returns Handler that does some useful work. Implementation SHOULD\n\t// call provided next Handler somewhere in returned Handler to ensure chain\n\t// is not broken. However, implementation might choose not to call next\n\t// handler based on external influence, error checking, etc. In that case,\n\t// returned Handler MUST return error.\n\tExec(next Handler) Handler\n}\n```\nMiddleware defines interface for chaining Handlers.\n\n```go\ntype MiddlewareFunc func(handler Handler) Handler\n```\nMiddlewareFunc is function variant of Middleware interface.\n\n```go\nfunc (mf MiddlewareFunc) Exec(handler Handler) Handler\n```\nExec is implementation of Middleware interface.\n\n```go\ntype RequestProcessor func(req *http.Request) error\n```\nRequestProcessor is function for modification of HTTP request. It is intended as form of simple Middleware for middlewares that only need to change request that is being sent. Provided request can be modified as required. Returned error (if any) will stop middleware chain execution and same error will be returned to caller.\n\n```go\nfunc (rp RequestProcessor) Exec(handler Handler) Handler\n```\nExec is implementation of Middleware interface.\n\n```go\ntype ResponseProcessor func(resp *http.Response, err error) error\n```\nResponseProcessor is function for inspection of HTTP response. It is intended as form of a simple Middleware for middlewares that only need to inspect responses. E.g. they can log some information or inspect response to determine if error occurred. Provided response if one obtained from sending HTTP request and should not be modified. Provided error is original error returned after request or error returned by previous middleware. If middleware wants to change error - it should return it. Otherwise, if there is not error or existing (provided) error should not be changed, middleware should return nil.\n\n```go\nfunc (rp ResponseProcessor) Exec(handler Handler) Handler\n```\nExec is implementation of Middleware interface.\n\n","path":"github.com/delicb/cliware","name":"cliware","synopsis":"Package cliware defines base type for HTTP client middleware support.","isRedistributable":true}
