v1.0.4 Latest Latest

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

Go to latest
Published: May 31, 2017 License: MIT, MIT Imports: 1 Imported by: 52


gentleman/plugin Build Status GoDoc API Go Report Card

middleware package implements a simple middleware layer especially designed for HTTP client domain and full HTTP request/response live cycle.

Provides multiple factory functions to create plugins easily.


go get -u


See godoc reference.


Create a request plugin
package main

import (

func main() {
  // Create a new client
  cli := gentleman.New()

  // Create a request plugin to define the URL
  cli.Use(plugin.NewRequestPlugin(func(ctx *context.Context, h context.Handler) {
    u, _ := url.Parse("")
    ctx.Request.URL = u

  // Perform the request
  res, err := cli.Request().Send()
  if err != nil {
    fmt.Printf("Request error: %s\n", err)
  if !res.Ok {
    fmt.Printf("Invalid server response: %d\n", res.StatusCode)

  fmt.Printf("Status: %d\n", res.StatusCode)
  fmt.Printf("Body: %s", res.String())


MIT - Tomas Aparicio



Package plugin implements a plugin layer for gentleman components. Exports the required interface that must be implemented by plugins.

Plugins are phase-oriented middleware function handlers encapsulated in a simple interface that will be consumed by the middleware layer in order to trigger the plugin handler.

Plugin implementors can decide to build a plugin to handle a unique middleware phase or instead handle multiple phases: request, response, error...



This section is empty.


This section is empty.


This section is empty.


type Handlers

type Handlers map[string]context.HandlerFunc

Handlers represents a map to store middleware handler functions per phase.

type Layer

type Layer struct {

	// Handlers defines the required handlers
	Handlers Handlers

	// DefaultHandler is an optional field used to store
	// a default handler for any middleware phase.
	DefaultHandler context.HandlerFunc
	// contains filtered or unexported fields

Layer encapsulates an Error, Request and Response function handlers

func New

func New() *Layer

New creates a new plugin layer.

func (*Layer) Disable

func (p *Layer) Disable()

Disable will disable the current plugin

func (*Layer) Disabled

func (p *Layer) Disabled() bool

Disabled returns true if the plugin is enabled

func (*Layer) Enable

func (p *Layer) Enable()

Enable will enable the current plugin

func (*Layer) Exec

func (p *Layer) Exec(phase string, ctx *context.Context, h context.Handler)

Exec executes the plugin handler for the given middleware phase passing the given context.

func (*Layer) Remove

func (p *Layer) Remove()

Remove will remove the plugin from the middleware stack

func (*Layer) Removed

func (p *Layer) Removed() bool

Removed returns true if the plugin Was removed

func (*Layer) SetHandler

func (p *Layer) SetHandler(phase string, handler context.HandlerFunc)

SetHandler uses a new handler function for the given middleware phase.

func (*Layer) SetHandlers

func (p *Layer) SetHandlers(handlers Handlers)

SetHandlers uses a new map of handler functions.

type Plugin

type Plugin interface {
	// Enable enabled the plugin

	// Disable disables the plugin

	// Disabled returns true if the plugin is enabled
	Disabled() bool

	// Remove will remove the plugin from the middleware stack

	// Enabled returns true if the plugin was removed
	Removed() bool

	// Exec executes the plugin handler for a specific middleware phase.
	Exec(string, *context.Context, context.Handler)

Plugin interface that must be implemented by plugins

func NewErrorPlugin

func NewErrorPlugin(handler context.HandlerFunc) Plugin

NewErrorPlugin creates a new plugin layer to handle error middleware phase

func NewPhasePlugin

func NewPhasePlugin(phase string, handler context.HandlerFunc) Plugin

NewPhasePlugin creates a new plugin layer to handle a given middleware phase.

func NewRequestPlugin

func NewRequestPlugin(handler context.HandlerFunc) Plugin

NewRequestPlugin creates a new plugin layer to handle request middleware phase

func NewResponsePlugin

func NewResponsePlugin(handler context.HandlerFunc) Plugin

NewResponsePlugin creates a new plugin layer to handle response middleware phase

Jump to

Keyboard shortcuts

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