Skip to content

Latest commit

 

History

History
93 lines (71 loc) · 3.06 KB

README.md

File metadata and controls

93 lines (71 loc) · 3.06 KB

GWC (Go Web Client)

Go Report Card Build Status codecov status GoDoc

GWC is GoLang HTTP client based on Cliware client middleware management library and its middlewares.

Basic idea is to use same middleware mechanism on client side as many projects use it for server development.

Because it is based on middlewares client is very pluggable. However, even out of box it should support most common use cases.

Install

Run go get github.com/delicb/gwc in terminal.

Example

There is testable example in this repository. It basically does following:

package gwc

import (
	"fmt"
	"net/http"

    "github.com/delicb/gwc"
	"github.com/delicb/cliware-middlewares/headers"
	"github.com/delicb/cliware-middlewares/errors"
)

type HTTPBinResponse struct {
	Headers struct {
		UserAgent string `json:"User-Agent"`
	} `json:"headers"`
}

func main() {
	respBody := new(HTTPBinResponse)

	client := gwc.New(
		http.DefaultClient,
		// Add user agent header, it will be applied to all requests
		headers.Add("User-Agent", "example-client"),
		errors.Errors(),
	)
	// send request
	resp, err := client.Get().URL("https://httpbin.org/get").Send()
	
	// check errors
	// because of errors middleware included in client, ever status codes 
	// 400+ will be turned into errors.
	if err != nil {
		panic(err)
	}

	resp.JSON(respBody)
	fmt.Println(respBody.Headers.UserAgent)
	// output: example-client
}

In this example we create new GWC instance with some middlewares (in this case we set User-Agent header to each request sent using this middleware).

We use client to send request and (after some error checking) we deserialize JSON body to structure.

State

This is early development, not stable, backward compatibility not guarantied.

Currently, GWC requires GoLang 1.7 to work. However, in the future I will not hesitate from using new language features. Therefor, make sure to vendor this library if you intend to use it.

Contribution

Any contribution is welcome. If you find this code useful, please let me know. If you find bugs - feel free to open an issue, or, even better, new pull request.

Credits

Idea and bunch of implementation details were taken from cool GoLang HTTP client Gentleman.

Difference is that GWC is based on Cliware, which supports context for client requests and has more simple idea of middleware. Also, GWC is lacking some features that Gentleman has (like mux). For now I do not plan on adding them to GWC, but I might write middleware that support similar functionality in the future.