From 38c5d0313777b855c2fbdf4137d083c9e1b9f28f Mon Sep 17 00:00:00 2001 From: Tomas Aparicio Date: Wed, 24 Feb 2016 17:35:11 +0000 Subject: [PATCH] feat(#7): support mux composition, add examples --- README.md | 3 +- _examples/auth/auth.go | 2 +- _examples/body/body.go | 2 +- _examples/bodytype/bodytype.go | 2 +- _examples/compression/compression.go | 2 +- _examples/cookies/cookies.go | 2 +- _examples/headers/headers.go | 2 +- _examples/multipart/multipart.go | 2 +- _examples/multiplexer/composition.go | 39 +++++++++++++++++++++++ _examples/multiplexer/multiplexer.go | 2 +- _examples/query/query.go | 2 +- _examples/redirect/redirect.go | 2 +- _examples/timeout/timeout.go | 2 +- _examples/transport/transport.go | 2 +- _examples/url/url.go | 2 +- mux/README.md | 46 +++++++++++++++++++++++++++- mux/compose.go | 18 ++++++----- mux/compose_test.go | 44 ++++++++++++++++++++++++-- mux/mux.go | 2 +- plugins/auth/README.md | 4 +-- plugins/body/README.md | 4 +-- plugins/bodytype/README.md | 4 +-- plugins/compression/README.md | 4 +-- plugins/cookies/README.md | 4 +-- plugins/headers/README.md | 4 +-- plugins/multipart/README.md | 4 +-- plugins/proxy/README.md | 4 +-- plugins/query/README.md | 4 +-- plugins/redirect/README.md | 4 +-- plugins/timeout/README.md | 4 +-- plugins/tls/README.md | 4 +-- plugins/transport/README.md | 4 +-- plugins/url/README.md | 4 +-- request.go | 4 ++- 34 files changed, 183 insertions(+), 55 deletions(-) create mode 100644 _examples/multiplexer/composition.go diff --git a/README.md b/README.md index 007d01d..0501a88 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,7 @@ Plugin-driven, middleware-oriented library to easily create rich, versatile and go get -u gopkg.in/h2non/gentleman.v0 ``` -Note: I strongly recommend you to use `gopkg.in` when depending on third-party packages to prevent unexpected breaks of the interface contract in upcoming major versions of the package. -In minor versions the contract will be strictly maintained. +Note: I strongly recommend you to use `gopkg.in` when depending on third-party packages to prevent unexpected breaks of the interface contract in upcoming major versions of the package. ## Goals diff --git a/_examples/auth/auth.go b/_examples/auth/auth.go index 4724cec..ef5518b 100644 --- a/_examples/auth/auth.go +++ b/_examples/auth/auth.go @@ -14,7 +14,7 @@ func main() { cli.Use(auth.Basic("user", "pas$w0rd")) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s", err) return diff --git a/_examples/body/body.go b/_examples/body/body.go index 76df59e..48e6cb0 100644 --- a/_examples/body/body.go +++ b/_examples/body/body.go @@ -15,7 +15,7 @@ func main() { cli.Use(body.JSON(data)) // Perform the request - res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").End() + res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/bodytype/bodytype.go b/_examples/bodytype/bodytype.go index 26216b8..2361314 100644 --- a/_examples/bodytype/bodytype.go +++ b/_examples/bodytype/bodytype.go @@ -19,7 +19,7 @@ func main() { cli.Use(bodytype.Type("json")) // Perform the request - res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").End() + res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/compression/compression.go b/_examples/compression/compression.go index 206c94b..0d5aa52 100644 --- a/_examples/compression/compression.go +++ b/_examples/compression/compression.go @@ -14,7 +14,7 @@ func main() { cli.Use(compression.Disable()) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/gzip").End() + res, err := cli.Request().URL("http://httpbin.org/gzip").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/cookies/cookies.go b/_examples/cookies/cookies.go index 3626934..5171156 100644 --- a/_examples/cookies/cookies.go +++ b/_examples/cookies/cookies.go @@ -17,7 +17,7 @@ func main() { cli.Use(cookies.Jar()) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/cookies").End() + res, err := cli.Request().URL("http://httpbin.org/cookies").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/headers/headers.go b/_examples/headers/headers.go index aa4e55c..c210b75 100644 --- a/_examples/headers/headers.go +++ b/_examples/headers/headers.go @@ -17,7 +17,7 @@ func main() { cli.Use(headers.Del("User-Agent")) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/multipart/multipart.go b/_examples/multipart/multipart.go index 4b75483..c66a5da 100644 --- a/_examples/multipart/multipart.go +++ b/_examples/multipart/multipart.go @@ -15,7 +15,7 @@ func main() { cli.Use(multipart.Fields(fields)) // Perform the request - res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").End() + res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/multiplexer/composition.go b/_examples/multiplexer/composition.go new file mode 100644 index 0000000..37dbc05 --- /dev/null +++ b/_examples/multiplexer/composition.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "gopkg.in/h2non/gentleman.v0" + "gopkg.in/h2non/gentleman.v0/mux" + "gopkg.in/h2non/gentleman.v0/plugins/url" +) + +func main() { + // Create a new client + cli := gentleman.New() + + // Define the server url (must be first) + cli.Use(url.URL("http://httpbin.org")) + + // Create a new multiplexer based on multiple matchers + mx := mux.If(mux.Method("GET"), mux.Host("httpbin.org")) + + // Attach a custom plugin on the multiplexer that will be executed if the matchers passes + mx.Use(url.Path("/headers")) + + // Attach the multiplexer on the main client + cli.Use(mx) + + // 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()) +} diff --git a/_examples/multiplexer/multiplexer.go b/_examples/multiplexer/multiplexer.go index 858e52d..c2cfbfa 100644 --- a/_examples/multiplexer/multiplexer.go +++ b/_examples/multiplexer/multiplexer.go @@ -18,7 +18,7 @@ func main() { }).Use(url.URL("http://httpbin.org/headers"))) // Perform the request - res, err := cli.Request().End() + res, err := cli.Request().Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/query/query.go b/_examples/query/query.go index 6d60923..3d12d0b 100644 --- a/_examples/query/query.go +++ b/_examples/query/query.go @@ -22,7 +22,7 @@ func main() { cli.Use(query.Del("bar")) // Perform the request - res, err := cli.Request().End() + res, err := cli.Request().Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/redirect/redirect.go b/_examples/redirect/redirect.go index d476dd6..e0d99ab 100644 --- a/_examples/redirect/redirect.go +++ b/_examples/redirect/redirect.go @@ -14,7 +14,7 @@ func main() { cli.Use(redirect.Limit(20)) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/timeout/timeout.go b/_examples/timeout/timeout.go index 784c443..35f3eb1 100644 --- a/_examples/timeout/timeout.go +++ b/_examples/timeout/timeout.go @@ -18,7 +18,7 @@ func main() { cli.Use(timeout.Dial(5*time.Second, 30*time.Second)) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/transport/transport.go b/_examples/transport/transport.go index 2e49992..5751fbf 100644 --- a/_examples/transport/transport.go +++ b/_examples/transport/transport.go @@ -15,7 +15,7 @@ func main() { cli.Use(transport.Set(http.DefaultTransport)) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/_examples/url/url.go b/_examples/url/url.go index 7053b86..cccbe6f 100644 --- a/_examples/url/url.go +++ b/_examples/url/url.go @@ -20,7 +20,7 @@ func main() { cli.Use(url.Param("resource", "headers")) // Perform the request - res, err := cli.Request().End() + res, err := cli.Request().Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/mux/README.md b/mux/README.md index 0687e9c..3b621a7 100644 --- a/mux/README.md +++ b/mux/README.md @@ -14,6 +14,7 @@ See [godoc](https://godoc.org/github.com/h2non/gentleman/mux) reference. ## Example +Create a multiplexer with a custom matcher function: ```go package main @@ -35,7 +36,50 @@ func main() { }).Use(url.URL("http://httpbin.org/headers"))) // Perform the request - res, err := cli.Request().End() + 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()) +} +``` + +Multiplexer composition: +```go +package main + +import ( + "fmt" + "gopkg.in/h2non/gentleman.v0" + "gopkg.in/h2non/gentleman.v0/mux" + "gopkg.in/h2non/gentleman.v0/plugins/url" +) + +func main() { + // Create a new client + cli := gentleman.New() + + // Define the server url (must be first) + cli.Use(url.URL("http://httpbin.org")) + + // Create a new multiplexer based on multiple matchers + mx := mux.If(mux.Method("GET"), mux.Host("httpbin.org")) + + // Attach a custom plugin on the multiplexer that will be executed if the matchers passes + mx.Use(url.Path("/headers")) + + // Attach the multiplexer on the main client + cli.Use(mx) + + // Perform the request + res, err := cli.Request().Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/mux/compose.go b/mux/compose.go index 6ee5b9b..a874488 100644 --- a/mux/compose.go +++ b/mux/compose.go @@ -4,16 +4,20 @@ import ( c "gopkg.in/h2non/gentleman.v0/context" ) -// If creates a new multiplexer that will be executed if all the matchers passes. -func If(matchers ...Matcher) *Mux { - return Match(matchers...) +// If creates a new multiplexer that will be executed if all the mux matchers passes. +func If(muxes ...*Mux) *Mux { + mx := New() + for _, mm := range muxes { + mx.AddMatcher(mm.Matchers...) + } + return mx } -// Or creates a new multiplexer that will be executed if at least one matcher passes. -func Or(matchers ...Matcher) *Mux { +// Or creates a new multiplexer that will be executed if at least one mux matcher passes. +func Or(muxes ...*Mux) *Mux { return Match(func(ctx *c.Context) bool { - for _, matcher := range matchers { - if matcher(ctx) { + for _, mm := range muxes { + if mm.Match(ctx) { return true } } diff --git a/mux/compose_test.go b/mux/compose_test.go index 96445b8..7fa3a8b 100644 --- a/mux/compose_test.go +++ b/mux/compose_test.go @@ -6,14 +6,54 @@ import ( "testing" ) -func testMuxMethod(t *testing.T) { +func TestMuxComposeIfMatches(t *testing.T) { mx := New() - mx.Use(Method("GET").UseRequest(func(ctx *context.Context, h context.Handler) { + mx.Use(If(Method("GET"), Host("foo.com")).UseRequest(func(ctx *context.Context, h context.Handler) { ctx.Request.Header.Set("foo", "bar") h.Next(ctx) })) ctx := context.New() ctx.Request.Method = "GET" + ctx.Request.URL.Host = "foo.com" mx.Run("request", ctx) st.Expect(t, ctx.Request.Header.Get("foo"), "bar") } + +func TestMuxComposeIfUnmatch(t *testing.T) { + mx := New() + mx.Use(If(Method("GET"), Host("bar.com")).UseRequest(func(ctx *context.Context, h context.Handler) { + ctx.Request.Header.Set("foo", "bar") + h.Next(ctx) + })) + ctx := context.New() + ctx.Request.Method = "GET" + ctx.Request.URL.Host = "foo.com" + mx.Run("request", ctx) + st.Expect(t, ctx.Request.Header.Get("foo"), "") +} + +func TestMuxComposeOrMatch(t *testing.T) { + mx := New() + mx.Use(Or(Method("GET"), Host("bar.com")).UseRequest(func(ctx *context.Context, h context.Handler) { + ctx.Request.Header.Set("foo", "bar") + h.Next(ctx) + })) + ctx := context.New() + ctx.Request.Method = "GET" + ctx.Request.URL.Host = "foo.com" + mx.Run("request", ctx) + st.Expect(t, ctx.Request.Header.Get("foo"), "bar") +} + +func TestMuxComposeOrUnMatch(t *testing.T) { + mx := New() + mx.Use(Or(Method("GET"), Host("bar.com")).UseRequest(func(ctx *context.Context, h context.Handler) { + ctx.Request.Header.Set("foo", "bar") + h.Next(ctx) + })) + ctx := context.New() + ctx.Request.Method = "POST" + ctx.Request.URL.Host = "foo.com" + mx.Run("request", ctx) + st.Expect(t, ctx.Request.Header.Get("foo"), "") +} diff --git a/mux/mux.go b/mux/mux.go index 0d2df9e..bc61ae5 100644 --- a/mux/mux.go +++ b/mux/mux.go @@ -49,7 +49,7 @@ func (m *Mux) Match(ctx *c.Context) bool { return true } -// AddMatcher adds a new matcher function in the current multiplexer matchers stack. +// AddMatcher adds a new matcher function in the current mumultiplexer matchers stack. func (m *Mux) AddMatcher(matchers ...Matcher) *Mux { m.Matchers = append(m.Matchers, matchers...) return m diff --git a/plugins/auth/README.md b/plugins/auth/README.md index 8b2922b..7b2d169 100644 --- a/plugins/auth/README.md +++ b/plugins/auth/README.md @@ -1,4 +1,4 @@ -# gentleman/auth [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/auth) [![API](https://img.shields.io/badge/api-stable-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/auth) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/auth [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/auth) [![API](https://img.shields.io/badge/api-stable-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/auth) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easily define HTTP authorization headers based on multiple schemas. @@ -31,7 +31,7 @@ func main() { cli.Use(auth.Basic("user", "pas$w0rd")) // Perform the request - res, err := cli.Request().Method().URL("http://httpbin.org/headers").End() + res, err := cli.Request().Method().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/body/README.md b/plugins/body/README.md index 9aab418..e83c9ce 100644 --- a/plugins/body/README.md +++ b/plugins/body/README.md @@ -1,4 +1,4 @@ -# gentleman/body [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/body?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/body) [![API](https://img.shields.io/badge/api-stable-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/body [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/body?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/body) [![API](https://img.shields.io/badge/api-stable-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easy define HTTP bodies. Supports JSON, XML, strings or streams with interface polymorphism. @@ -32,7 +32,7 @@ func main() { cli.Use(body.JSON(data)) // Perform the request - res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").End() + res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/bodytype/README.md b/plugins/bodytype/README.md index 5a6c111..eb2fe8e 100644 --- a/plugins/bodytype/README.md +++ b/plugins/bodytype/README.md @@ -1,4 +1,4 @@ -# gentleman/bodytype [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/bodytype?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/bodytype) [![API](https://img.shields.io/badge/api-stable-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/bodytype) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/bodytype [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/bodytype?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/bodytype) [![API](https://img.shields.io/badge/api-stable-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/bodytype) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easy define HTTP bodies. Supports JSON, XML, strings or streams with interface polymorphism. @@ -36,7 +36,7 @@ func main() { cli.Use(bodytype.Type("json")) // Perform the request - res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").End() + res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/compression/README.md b/plugins/compression/README.md index 2451b3c..7107d8f 100644 --- a/plugins/compression/README.md +++ b/plugins/compression/README.md @@ -1,4 +1,4 @@ -# gentleman/compression [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/compression?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/compression) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/compression) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/compression [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/compression?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/compression) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/compression) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to disable and customize data compression in HTTP requests/responses. @@ -31,7 +31,7 @@ func main() { cli.Use(compression.Disable()) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/gzip").End() + res, err := cli.Request().URL("http://httpbin.org/gzip").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/cookies/README.md b/plugins/cookies/README.md index 2d1cc6b..66c9bf8 100644 --- a/plugins/cookies/README.md +++ b/plugins/cookies/README.md @@ -1,4 +1,4 @@ -# gentleman/cookies [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/cookies?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/cookies) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/cookies) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/cookies [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/cookies?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/cookies) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/cookies) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easily deal and manage cookies HTTP clients. @@ -34,7 +34,7 @@ func main() { cli.Use(cookies.Jar()) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/cookies").End() + res, err := cli.Request().URL("http://httpbin.org/cookies").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/headers/README.md b/plugins/headers/README.md index c618dcd..4dfb9b1 100644 --- a/plugins/headers/README.md +++ b/plugins/headers/README.md @@ -1,4 +1,4 @@ -# gentleman/headers [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/headers?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/headers) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/headers) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/headers [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/headers?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/headers) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/headers) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easily manage HTTP headers. @@ -34,7 +34,7 @@ func main() { cli.Use(headers.Del("User-Agent")) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/multipart/README.md b/plugins/multipart/README.md index 8d49f3f..1d9f441 100644 --- a/plugins/multipart/README.md +++ b/plugins/multipart/README.md @@ -1,4 +1,4 @@ -# gentleman/multipart [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/multipart?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/multipart) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/multipart) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/multipart [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/multipart?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/multipart) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/multipart) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easily define `multipart/form-data` bodies supporting files and string based fields. @@ -32,7 +32,7 @@ func main() { cli.Use(multipart.Fields(fields)) // Perform the request - res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").End() + res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/proxy/README.md b/plugins/proxy/README.md index d3a6a41..f7accc2 100644 --- a/plugins/proxy/README.md +++ b/plugins/proxy/README.md @@ -1,4 +1,4 @@ -# gentleman/proxy [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/proxy?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/proxy) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/proxy) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/proxy [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/proxy?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/proxy) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/proxy) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easily manage HTTP proxies used by clients. @@ -31,7 +31,7 @@ func main() { cli.Use(proxy.Set([]string{"http://proxy:3128", http://proxy2:3128})) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/query/README.md b/plugins/query/README.md index 7b49a0a..097c438 100644 --- a/plugins/query/README.md +++ b/plugins/query/README.md @@ -1,4 +1,4 @@ -# gentleman/query [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/query?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/query) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/query) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/query [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/query?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/query) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/query) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easily manage HTTP query params. @@ -39,7 +39,7 @@ func main() { cli.Use(query.Del("bar")) // Perform the request - res, err := cli.Request().End() + res, err := cli.Request().Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/redirect/README.md b/plugins/redirect/README.md index 332fa1a..2642716 100644 --- a/plugins/redirect/README.md +++ b/plugins/redirect/README.md @@ -1,4 +1,4 @@ -# gentleman/redirect [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman?status.svg)](https://godoc.org/github.com/h2non/gentleman) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/redirect [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman?status.svg)](https://godoc.org/github.com/h2non/gentleman) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easily define HTTP request redirect policy and settings. @@ -31,7 +31,7 @@ func main() { cli.Use(redirect.Limit(20)) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/timeout/README.md b/plugins/timeout/README.md index 40d8693..9c0274b 100644 --- a/plugins/timeout/README.md +++ b/plugins/timeout/README.md @@ -1,4 +1,4 @@ -# gentleman/timeout [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/timeout?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/timeout) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/timeout) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman/plugins/timeout)](https://goreportcard.com/report/github.com/h2non/gentleman/plugins/timeout) +# gentleman/timeout [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/timeout?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/timeout) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/timeout) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman/plugins/timeout)](https://goreportcard.com/report/github.com/h2non/gentleman/plugins/timeout) gentleman's plugin to easily define HTTP timeouts per specific phase in the HTTP connection live cycle. @@ -35,7 +35,7 @@ func main() { cli.Use(timeout.Dial(5*time.Second, 30*time.Second)) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/tls/README.md b/plugins/tls/README.md index ef254bd..77ac363 100644 --- a/plugins/tls/README.md +++ b/plugins/tls/README.md @@ -1,4 +1,4 @@ -# gentleman/tls [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/tls?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/tls) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/tls) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) +# gentleman/tls [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/tls?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/tls) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/tls) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) gentleman's plugin to easily define TLS config used by `http.Transport`/`RoundTripper` interface. @@ -32,7 +32,7 @@ func main() { cli.Use(tls.Config(&tls.Config{ServerName: "foo.com"})) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/transport/README.md b/plugins/transport/README.md index 082aac6..44c7061 100644 --- a/plugins/transport/README.md +++ b/plugins/transport/README.md @@ -1,4 +1,4 @@ -# gentleman/transport [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/transport?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/transport) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/transport) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman/plugins/transport)](https://goreportcard.com/report/github.com/h2non/gentleman/plugins/transport) +# gentleman/transport [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/transport?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/transport) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/transport) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman/plugins/transport)](https://goreportcard.com/report/github.com/h2non/gentleman/plugins/transport) gentleman's plugin to easily define the HTTP transport to be used by `http.Client`. @@ -32,7 +32,7 @@ func main() { cli.Use(transport.Set(http.DefaultTransport)) // Perform the request - res, err := cli.Request().URL("http://httpbin.org/headers").End() + res, err := cli.Request().URL("http://httpbin.org/headers").Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/plugins/url/README.md b/plugins/url/README.md index eeea285..759e480 100644 --- a/plugins/url/README.md +++ b/plugins/url/README.md @@ -1,4 +1,4 @@ -# gentleman/url [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/github/tag/h2non/gentleman.svg)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/url?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/url) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/url) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman/plugins/url)](https://goreportcard.com/report/github.com/h2non/gentleman/plugins/url) +# gentleman/url [![Build Status](https://travis-ci.org/h2non/gentleman.png)](https://travis-ci.org/h2non/gentleman) [![GoDoc](https://godoc.org/github.com/h2non/gentleman/plugins/url?status.svg)](https://godoc.org/github.com/h2non/gentleman/plugins/url) [![API](https://img.shields.io/badge/api-beta-green.svg?style=flat)](https://godoc.org/github.com/h2non/gentleman/plugins/url) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman/plugins/url)](https://goreportcard.com/report/github.com/h2non/gentleman/plugins/url) gentleman's plugin to define and manage URL values in HTTP requests. @@ -39,7 +39,7 @@ func main() { cli.Use(url.Param("resource", "headers")) // Perform the request - res, err := cli.Request().End() + res, err := cli.Request().Send() if err != nil { fmt.Printf("Request error: %s\n", err) return diff --git a/request.go b/request.go index 29be66a..8f04b46 100644 --- a/request.go +++ b/request.go @@ -44,7 +44,9 @@ var ( DefaultTransport = NewDefaultTransport(DefaultDialer) ) -// Request represents an basic HTTP structure entity +// Request HTTP entity for gentleman. +// Provides middleware capabilities, built-in context +// and convenient methods to easily setup request params. type Request struct { // Stores if the request was already dispatched dispatched bool