httpclient

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2021 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 = 30 * 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

func NewNopRateLimit added in v0.6.0

func NewNopRateLimit() *nopRateLimit

func NewRateLimit added in v0.6.0

func NewRateLimit(requests int64, period time.Duration) *rateLimit

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, body []byte, err error)
	Get(
		ctx context.Context,
		path string,
		param map[string]string,
		header http.Header,
	) (statusCode int, body []byte, err error)
	Patch(
		ctx context.Context,
		path string,
		param json.Marshaler,
		header http.Header,
	) (statusCode int, body []byte, err error)
	Post(
		ctx context.Context,
		path string,
		param json.Marshaler,
		header http.Header,
	) (statusCode int, body []byte, err error)
	Put(
		ctx context.Context,
		path string,
		param json.Marshaler,
		header http.Header,
	) (statusCode int, body []byte, err error)
}

type Logger added in v0.2.0

type Logger interface {
	Printf(format string, v ...interface{})
}

type LoggerLevel added in v0.7.1

type LoggerLevel int
const (
	LoggerLevelNop   LoggerLevel = 0
	LoggerLevelError LoggerLevel = 1
	LoggerLevelInfo  LoggerLevel = 2
)

func (LoggerLevel) IsAllowedForLevel added in v0.7.2

func (l LoggerLevel) IsAllowedForLevel(level LoggerLevel) bool

type M added in v0.10.0

type M map[string]interface{}

func (M) MarshalJSON added in v0.10.0

func (m M) MarshalJSON() ([]byte, error)

type Option added in v0.2.0

type Option func(*Options)

func LatencyMetricHandler added in v0.9.0

func LatencyMetricHandler(
	metricHandler func(
		req *http.Request, header http.Header,
		h func(req *http.Request, header http.Header) (statusCode int, body []byte, err error),
	) (statusCode int, body []byte, err error),
) Option

func Log added in v0.2.0

func Log(log Logger) Option

func LogLevel added in v0.7.1

func LogLevel(level LoggerLevel) Option

func RateLimit added in v0.6.0

func RateLimit(requests int64, period time.Duration) Option

func RequestMetricHandler added in v0.9.0

func RequestMetricHandler(
	metricHandler func(
		req *http.Request, header http.Header,
		h func(req *http.Request, header http.Header) (statusCode int, body []byte, err error),
	) (statusCode int, body []byte, err error),
) Option

func RoundTripper added in v0.9.1

func RoundTripper(roundTripper http.RoundTripper) 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
}

type RateLimiter added in v0.6.0

type RateLimiter interface {
	Take()
}

Jump to

Keyboard shortcuts

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