caller

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

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

Go to latest
Published: May 26, 2021 License: MIT Imports: 13 Imported by: 0

README

Caller

A tool can help coder reduce many duplicate code of requesting http api, and support optional config to modify the param of request and http client. What's more, this tool support that the response body of restful api is parsed by function and received into the pointer of struct or map, slice.

一个工具可以帮助减少许多用于请求http api的重复代码,并支持可选的配置来修改请求和http client的参数。 此外,该工具支持接收struct、map、slice的指针来函数解析(默认是json)api的响应体。

Usages

Default
func TestCaller(t *testing.T) {
	ctx := context.Background()
	result := map[string]interface{}{}
	err := Get(ctx, "http://127.0.0.1:8888/ping").Parse(&result)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(result)
}
Optional
func TestOptionalCaller(t *testing.T) {
    ctx := context.Background()
    result := map[string]interface{}{}
    header := map[string]string{"key": "value"}
    config := &Config{
        Timeout:       30 * time.Second,
        ConnTimeout:   10 * time.Second,
        KeepAlive:     10 * time.Second,
        RetryTime:     3,
        RetryInternal: time.Second,
    }
    caller := NewCaller(config.Options()...)
    err := caller.Do(ctx, "http://127.0.0.1:8888/ping", WithMethod("get"), WithHeader(header)).Parse(&result)
    if err != nil { 
    	t.Fatal(err)
    }
    t.Log(result)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRequest  = errors.New("request is wrong")
	ErrServer   = errors.New("server is wrong")
	ErrNotMatch = errors.New("not match error type")
)

Functions

This section is empty.

Types

type Caller

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

func New

func New(opts ...Option) *Caller

func (*Caller) Connect

func (c *Caller) Connect(ctx context.Context, url string, opts ...RequestFunc) Result

func (*Caller) Delete

func (c *Caller) Delete(ctx context.Context, url string, opts ...RequestFunc) Result

func (*Caller) Do

func (c *Caller) Do(ctx context.Context, method, url string, opts ...RequestFunc) Result

func (*Caller) Get

func (c *Caller) Get(ctx context.Context, url string, opts ...RequestFunc) Result

func (*Caller) Head

func (c *Caller) Head(ctx context.Context, url string, opts ...RequestFunc) Result

func (*Caller) Options

func (c *Caller) Options(ctx context.Context, url string, opts ...RequestFunc) Result

func (*Caller) Patch

func (c *Caller) Patch(ctx context.Context, url string, opts ...RequestFunc) Result

func (*Caller) Post

func (c *Caller) Post(ctx context.Context, url string, opts ...RequestFunc) Result

func (*Caller) Put

func (c *Caller) Put(ctx context.Context, url string, opts ...RequestFunc) Result

func (*Caller) Trace

func (c *Caller) Trace(ctx context.Context, url string, opts ...RequestFunc) Result

type Core

type Core interface {
	Do(req *http.Request) (resp *http.Response, err error)
}

type NoRetryError

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

func (*NoRetryError) Unwrap

func (n *NoRetryError) Unwrap() error

type Option

type Option func(config *option)

func WithConnTimeout

func WithConnTimeout(timeout time.Duration) Option

func WithCookieJar

func WithCookieJar(cookiejar http.CookieJar) Option

func WithKeepAlive

func WithKeepAlive(alive time.Duration) Option

func WithMaxIdleConn

func WithMaxIdleConn(conn int) Option

func WithParseFunc

func WithParseFunc(parseFunc ParseFunc) Option

func WithProxy

func WithProxy(proxy func(*http.Request) (*url.URL, error)) Option

func WithProxyURL

func WithProxyURL(proxyURL string) Option

func WithReadBuffer

func WithReadBuffer(buffer int) Option

func WithRedirect

func WithRedirect(redirect func(req *http.Request, via []*http.Request) error) Option

func WithRetry

func WithRetry(retries int, internal time.Duration) Option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

func WithWriteBuffer

func WithWriteBuffer(buffer int) Option

type ParseFunc

type ParseFunc func(data []byte, receive interface{}) error

type RequestFunc

type RequestFunc func(req *request)

func WithBody

func WithBody(body io.Reader) RequestFunc

func WithHeader

func WithHeader(header map[string]string) RequestFunc

func WithQuery

func WithQuery(query map[string]string) RequestFunc

type Result

type Result interface {
	Err() error
	Byte() ([]byte, error)
	String() (string, error)
	Raw() (io.ReadCloser, error)
	Parse(receive interface{}) error
	ParseWithFunc(receive interface{}, parse ParseFunc) error
}

func Connect

func Connect(ctx context.Context, url string, opts ...RequestFunc) Result

func Delete

func Delete(ctx context.Context, url string, opts ...RequestFunc) Result

func Get

func Get(ctx context.Context, url string, opts ...RequestFunc) Result
func Head(ctx context.Context, url string, opts ...RequestFunc) Result

func Options

func Options(ctx context.Context, url string, opts ...RequestFunc) Result

func Patch

func Patch(ctx context.Context, url string, opts ...RequestFunc) Result

func Post

func Post(ctx context.Context, url string, opts ...RequestFunc) Result

func Put

func Put(ctx context.Context, url string, opts ...RequestFunc) Result

func Trace

func Trace(ctx context.Context, url string, opts ...RequestFunc) Result

type ResultError

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

func UnWarpError

func UnWarpError(err error) (*ResultError, error)

func (*ResultError) Error

func (h *ResultError) Error() string

func (*ResultError) Message

func (h *ResultError) Message() string

func (*ResultError) StatusCode

func (h *ResultError) StatusCode() int

func (*ResultError) Unwrap

func (h *ResultError) Unwrap() error

type Retry

type Retry interface {
	Do(ctx context.Context, f func(ctx context.Context) error) error
}

type RetryError

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

func (*RetryError) Error

func (e *RetryError) Error() string

func (*RetryError) Unwrap

func (e *RetryError) Unwrap() error

Jump to

Keyboard shortcuts

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