Skip to content

Latest commit

 

History

History
59 lines (45 loc) · 1.71 KB

README.md

File metadata and controls

59 lines (45 loc) · 1.71 KB

HTTPClient

Go Report Card Go Reference

WARNING: alpha quality code; use at your own risk.

Thin layer around Go's net/http library with extensible configuration options.

It extracts the http code making requests to our REST APIs to be shared among various projects.

Features include:

  • Safety for concurrent usage
  • Transparent support for HTTP/2
  • Sharing Headers between requests
  • Custom redirect policies
  • Calling GET requests with context objects
  • Ability to set and share various timeouts without diving deep into net/http internals
  • Having a better understanding regarding idle connection pools

All of the above is supported by the standard library, however this thin layer allows us to do so without having to manage our own http.Client and http.Transport objects.

For more information on HTTP Timeouts read this.

Usage

package main

import (
	"context"
	"io"
	"net/http"
	"os"
	"time"

	h2 "github.com/gadventures/httpclient"
)

func main() {
	client, err := h2.New(
		h2.DialTimeout(3*time.Second),
		h2.IdleConnTimeout(30*time.Second),
		h2.Logger(os.Stderr),
		h2.MaxIdleConns(4),
	)

	responseHandler := func(ctx context.Context, resp *http.Response, err error) error {
		if err != nil {
			return err
		}
		_, err = io.Copy(os.Stdout, resp.Body)
		return err
	}

	err = client.Get(ctx, responseHandler, "https://somewhere.foo.bar.com/page.html")
}