httpclient

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2019 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

httpclient

golang net/http wrapper

HttpClient

Examples
client := httpclient.NewHttpClient("jsonplaceholder.typicode.com")
	statusCode, res, err := client.Request(context.Background(), http.MethodGet, "/posts", nil, nil)

	fmt.Println(statusCode, res, err) // 200 [<bytes>] <nil>

JsonClient

Examples

Post request

client := httpclient.NewJsonClient("jsonplaceholder.typicode.com")
	statusCode, res, err := client.Post(context.Background(), "/posts",
		&map[string]interface{}{
			"test": 101,
		}, nil)

	if r, ok := res.(map[string]interface{}); ok {
		fmt.Println(statusCode, r, err)
	}

Returning json object

client := httpclient.NewJsonClient("jsonplaceholder.typicode.com")
	statusCode, res, err := client.Get(context.Background(), "/posts/1", nil, nil)

	if r, ok := res.(map[string]interface{}); ok {
		fmt.Println(statusCode, r, err) // 200 <map> <nil>
	}

Returning json array

client := httpclient.NewJsonClient("jsonplaceholder.typicode.com")
	statusCode, res, err := client.Get(context.Background(), "/posts", nil, nil)

	if r, ok := res.([]interface{}); ok {
		fmt.Println(statusCode, r, err) // 200 <slice> <nil>
	}

Documentation

Index

Constants

View Source
const (
	TimeoutDefault = 5 * time.Second
	SchemeDefault  = "https"
)

Variables

This section is empty.

Functions

func NewHttpClient

func NewHttpClient(host string, options ...Option) *httpClient

func NewJsonClient

func NewJsonClient(host string, options ...Option) *jsonClient

func NewNopLogger added in v0.2.0

func NewNopLogger() *nopLogger

Types

type HttpClient

type HttpClient interface {
	RequestNoBody(
		ctx context.Context,
		method string,
		path string,
		param map[string]string,
		header http.Header,
	) (statusCode int, body []byte, err error)
	Request(
		ctx context.Context,
		method string,
		path string,
		param []byte,
		header http.Header,
	) (statusCode int, body []byte, err error)
}

type JsonClient

type JsonClient interface {
	Delete(
		ctx context.Context,
		path string,
		param map[string]string,
		header http.Header,
	) (statusCode int, res interface{}, err error)
	Get(
		ctx context.Context,
		path string,
		param map[string]string,
		header http.Header,
	) (statusCode int, res interface{}, err error)
	Patch(
		ctx context.Context,
		path string,
		param map[string]interface{},
		header http.Header,
	) (statusCode int, res interface{}, err error)
	Post(
		ctx context.Context,
		path string,
		param map[string]interface{},
		header http.Header,
	) (statusCode int, res interface{}, err error)
	Put(
		ctx context.Context,
		path string,
		param map[string]interface{},
		header http.Header,
	) (statusCode int, res interface{}, err error)
}

type Logger added in v0.2.0

type Logger interface {
	Println(v ...interface{})
}

type Option added in v0.2.0

type Option func(*Options)

func Log added in v0.2.0

func Log(log Logger) Option

func Scheme added in v0.3.0

func Scheme(scheme string) Option

func Timeout added in v0.3.0

func Timeout(timeout time.Duration) Option

type Options added in v0.2.0

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

Jump to

Keyboard shortcuts

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