Overview of gentleman
masternet/http package and is designed for extensibility through a hierarchical middleware layer and a plugin system. It is particularly well-suited for building domain-specific HTTP API clients.repository·master·Indexed 22 days ago
https://github.com/h2non/gentlemanA plugin-driven, middleware-oriented toolkit for building highly composable and extensible HTTP clients in Go, built on top of the standard net/http library. It includes packages for request-aware context, middleware management, and a multiplexer (mux) for conditional plugin composition. The toolkit provides various plugins for authentication, body handling, content-type definition, compression, and cookie management.
net/http package and is designed for extensibility through a hierarchical middleware layer and a plugin system. It is particularly well-suited for building domain-specific HTTP API clients.The context package provides a request-aware HTTP context designed to share polymorphic data across different plugins within the middleware call chain.
Key characteristics:
context package and implements the valid context.Context interface. This means you can use it wherever a standard Go context is expected.gentleman uses a hierarchical middleware layer based on plugins to provide custom logic during the HTTP request/response lifecycle. Plugins execute function handlers that can intercept, modify, or stop requests and responses.
Key behaviors:
Gentleman provides two high-level HTTP entities: Client and Request. Both are middleware-capable, allowing you to plug in custom logic into any of them.
Client: Designed for reusability. A Client can inherit from another Client and can create multiple Request entities.Request: Designed for specific HTTP request logic that is typically not reused. A Request can inherit from a Client.Gentleman uses a hierarchical, inheritance-based middleware layer to achieve strong reusability:
Client can inherit configuration and middleware from another Client.Request created by a Client implicitly inherits the middleware and configuration of that Client.Client and Request entities can be cloned to produce a side-effect-free copy of the entity.The bodytype plugin allows you to easily define the Content-Type for your HTTP requests. It supports various type aliases that map to specific MIME types. Use bodytype.Type("alias") to set the content type.
Supported type aliases:
html -> text/htmljson -> application/jsonxml -> application/xmltext -> text/plainurlencoded -> application/x-www-form-urlencodedform -> application/x-www-form-urlencodedform-data -> application/x-www-form-urlencodedPlugins are sets of middleware function handlers for one or multiple HTTP lifecycle phases. They are consumed by the gentleman middleware layer and can be used for tasks like server discovery, custom HTTP transport, modifying request/response parameters, intercepting traffic, or authentication.
For implementation details, refer to the plugin package and the provided examples in the repository.
The mux package provides an HTTP client multiplexer that allows you to compose plugins based on specific conditions. It supports both request and response phases. You can use matchers to filter which plugins are executed during a request lifecycle.
Key capabilities include:
*context.Context.mux.Method or mux.Host to filter requests.// Example of a multiplexer with a custom matcher
cli.Use(mux.New().AddMatcher(func (ctx *context.Context) bool {
return ctx.GetString("$phase") == "request" && ctx.Request.Method == "GET"
}).Use(url.URL("http://httpbin.org/headers")))The gentleman/url plugin allows you to build complex URLs by composing different parts of the request. It supports:
/:resource) to define placeholders in the path.// Define the base URL
cli.Use(url.BaseURL("http://httpbin.org"))
// Define the path with dynamic value
cli.Use(url.Path("/:resource"))
// Define the path value to be replaced
cli.Use(url.Param("resource", "get"))To use the multiplexer functionality in your Go project, install the mux package using go get.
go get -u gopkg.in/h2non/gentleman.v2/muxThe gentleman/query plugin provides middleware to easily manipulate query parameters on your requests. You can use query.Set to add or update parameters and query.Del to remove them. These are applied to the client or specific requests using the .Use() method.
package main
import (
"fmt"
"gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman.v2/plugins/query"
"gopkg.in/h2non/gentleman.v2/plugins/url"
)
func main() {
// Create a new client
cli := gentleman.New()
// Define the base URL to use
cli.Use(url.BaseURL("http://httpbin.org"))
cli.Use(url.Path("/get"))
// Define a custom query param
cli.Use(query.Set("foo", "bar"))
// Remove a query param
cli.Use(query.Del("bar"))
// Perform the request
res, err := cli.Request().Send()
if err != nil {
fmt.Printf("Request error: %s\n", err)
return
}
if !res.Ok {
fmt.Printf("Invalid server response: %d\n", res.StatusCode)
return
}
fmt.Printf("Status: %d\n", res.StatusCode)
fmt.Printf("Body: %s", res.String())
}To use the middleware-based plugin layer for controlling the HTTP request/response lifecycle, install the plugin package using go get.
go get -u gopkg.in/h2non/gentleman.v2/pluginTo use the request-aware HTTP context for sharing data across plugins, install the package using go get:
go get -u gopkg.in/h2non/gentleman.v2/context