http

package module
v0.0.0-...-a660862 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

README

gohttp

Simpler go http client

Sample usage:

GET with parameters:

cli := http.NewClient()

var ret map[string]interface{}
err := cli.Get(context.Background(),
    "http://example.com",
    http.WithParam("debug", "1),
    http.WithJSONResponse(&ret))

POST with body:

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

body = map[string]string{
   "hello": "world",
}
err := cli.Post(ctx, "http://example.com",
    http.WithJSONBody(body),
    http.WithHeader("Authorization", "Bearer my-token"))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BadStatusError

type BadStatusError struct {
	Code int
	Body []byte
}

func (*BadStatusError) Error

func (bse *BadStatusError) Error() string

type Client

type Client interface {
	Get(ctx context.Context, url string, options ...RequestOption) error
	Post(ctx context.Context, url string, options ...RequestOption) error
}

Client provides simpler high level interfaces for http querying.

Unfortunately, go does not currently have good interfaces for doing http requests, requiring a lot of boilerplate and not being very testable (https://github.com/golang/go/issues/23707).

The goal of this client is:

  • Simple to use (e.g. a semantic api that handles common errors)
  • Simple to test (e.g. a small interface that provides a semantic api)

func NewClient

func NewClient() Client

NewClient constructs a Client.

func NewMockClient

func NewMockClient(handleRequest func(context.Context, *Request) error) Client

NewMockClient constructs a Client that calls handleRequest instead of actually doing a network request.

func NewTLSClient

func NewTLSClient(config *tls.Config) Client

NewTLSClient constructs a Client from the given tls.Config.

type Request

type Request struct {
	Method     string
	URL        string
	Params     url.Values
	Body       interface{}
	JSONOutput interface{}
	Output     io.Writer
	Header     http.Header
}

Request contains the requested behavior of an HTTP request.

It is an internal object primarly exported for testing with NewMockClient.

type RequestOption

type RequestOption func(*Request)

RequestOption controls the behavior of the HTTP request.

func WithHeader

func WithHeader(k, v string) RequestOption

WithHeader will set the HTTP Header on the request.

func WithJSONBody

func WithJSONBody(b interface{}) RequestOption

WithJSONBody will JSON marshal this object as the HTTP request body.

func WithJSONResponse

func WithJSONResponse(o interface{}) RequestOption

WithJSONResponse will JSON Unmarshal the HTTP response body into this object.

func WithParam

func WithParam(k, v string) RequestOption

WithParam will set the query parameter on the HTTP request url.

func WithResponse

func WithResponse(w io.Writer) RequestOption

WithResponse will write the HTTP response to this writer.

Jump to

Keyboard shortcuts

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