ezhttp

package
v0.0.0-...-cdd0c7f Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

This package aims to wrap Go HTTP Client's request-response with sane defaults:

  • You are forced to consider timeouts by having to specify Context
  • Instead of not considering non-2xx status codes as a failure, check that by default (unless explicitly asked to)
  • Sending and receiving JSON requires much less boilerplate, and on receiving JSON you are forced to think whether to "allowUnknownFields"

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultTimeout10s = 10 * time.Second
)
View Source
var InsecureTlsClient = &http.Client{
	Transport: &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	},
}
View Source
var TolerateNon2xxResponse = After(func(conf *Config) {
	conf.TolerateNon2xxResponse = true
})

Functions

func Del

func Del(ctx context.Context, url string, confPieces ...ConfigPiece) (*http.Response, error)

func Get

func Get(ctx context.Context, url string, confPieces ...ConfigPiece) (*http.Response, error)
func Head(ctx context.Context, url string, confPieces ...ConfigPiece) (*http.Response, error)

func Post

func Post(ctx context.Context, url string, confPieces ...ConfigPiece) (*http.Response, error)

func Put

func Put(ctx context.Context, url string, confPieces ...ConfigPiece) (*http.Response, error)

Types

type Config

type Config struct {
	Abort                         error // ConfigHook can set this to abort request send
	Client                        *http.Client
	Request                       *http.Request
	TolerateNon2xxResponse        bool
	RequestBody                   io.Reader
	OutputsJson                   bool
	OutputsJsonRef                interface{}
	OutputsJsonAllowUnknownFields bool
}

type ConfigHook

type ConfigHook func(conf *Config)

type ConfigPiece

type ConfigPiece struct {
	BeforeInit ConfigHook
	AfterInit  ConfigHook
}

func After

func After(fn ConfigHook) ConfigPiece

func AuthBasic

func AuthBasic(username, password string) ConfigPiece

func AuthBearer

func AuthBearer(token string) ConfigPiece

func Before

func Before(fn ConfigHook) ConfigPiece

same as After(), but Config.Request is nil. used mainly for specifying request body, which must be known on call to http.NewRequest()

func Client

func Client(client *http.Client) ConfigPiece
func Cookie(cookie http.Cookie) ConfigPiece
func Header(key, val string) ConfigPiece

func RespondsJson

func RespondsJson(ref interface{}, allowUnknownFields bool) ConfigPiece

func SendBody

func SendBody(body io.Reader, contentType string) ConfigPiece

func SendJson

func SendJson(ref interface{}) ConfigPiece

type ResponseStatusError

type ResponseStatusError struct {
	// contains filtered or unexported fields
}

func (ResponseStatusError) StatusCode

func (e ResponseStatusError) StatusCode() int

returns the (non-2xx) status code that caused the error

Jump to

Keyboard shortcuts

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