httpclient

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

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

Go to latest
Published: Jan 28, 2023 License: MIT Imports: 13 Imported by: 0

README

XGo/httpClient

Usage

Basic
resp, _ := httpclient.Default.Get(context.Background(), "https://www.baidu.com/sugrec")

// 打印返回值
log.Println(resp.String()) 

// 反序列化到对象
resp.To(&obj)

// 转为 interface{},复合对象为 map/array
resp.ToInterface()

// POST
httpclient.Default.PostJSON(ctx, "http://foo.com/bar", struct{})
httpclient.Default.PostForm(ctx, "http://foo.com/bar", map[string][]string{})
Custom Header
resp, _ := httpclient.PostRequest("http://foo.com/bar").
	Header("key", "value").
	Form(map[string][]string{}). // set form-urlencode body
	JSON(struct{}). // set json body
	Body("content-type", io.Reader). // set other body
	Do(ctx)
Custom Response Wrapper
resp, _ := ...
// {
//     "code": -1,
//     "err_msg": "some error",
//     "data": {
//         "id": 1
//     }
// }

err := resp.UnwrapTo(FooWrapper{}, &v)
/* or */ 
v, err := resp.UnwrapToInterface(FooWrapper{})

println(err)	// "some error"
println(v)	// {"id":1}
Dynamic Request Host
c := httpclient.NewClient().SetHost(`http://foo.com`)
resp, _ := c.Get("/abc")

c := httpclient.NewClient().SetHostGetter(func() string { 
	hosts := []string{"http://192.168.0.1", "http://192.168.0.2"}
	return hosts[rand.Int()]
}})
resp, _ := c.Get("/abc")
Retries

todo

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Unmarshalers = map[string]func([]byte, interface{}) error{
		`application/json`: JsonUnmarshaler,
		`application/xml`:  XmlUnmarshaler,
	}

	DefaultUnmarshaler = JsonUnmarshaler

	JsonUnmarshaler = func(data []byte, v interface{}) error { return json.Unmarshal(data, v) }
	XmlUnmarshaler  = func(data []byte, v interface{}) error { return xml.Unmarshal(data, v) }
)
View Source
var Default = NewClient()
View Source
var (
	DefaultHttpClient = &http.Client{Timeout: 15 * time.Second}
)

Functions

This section is empty.

Types

type CEDResponseWrapper

type CEDResponseWrapper struct {
	Code   int    `json:"code,omitempty"`
	ErrMsg string `json:"err_msg,omitempty"`
	HasData
}

func (*CEDResponseWrapper) Error

func (ced *CEDResponseWrapper) Error() error

type Client

type Client struct {
	*http.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient() *Client

func (*Client) DoRequest

func (c *Client) DoRequest(ctx context.Context, req *Request) (resp *Response, err error)

func (*Client) Get

func (c *Client) Get(ctx context.Context, uri string, query ...map[string]string) (resp *Response, err error)

func (*Client) Post

func (c *Client) Post(ctx context.Context, url, contentType string, body io.Reader) (resp *Response, err error)

func (*Client) PostForm

func (c *Client) PostForm(ctx context.Context, url string, form url.Values) (*Response, error)

func (*Client) PostJSON

func (c *Client) PostJSON(ctx context.Context, url string, body interface{}) (*Response, error)

func (*Client) SetHost

func (c *Client) SetHost(host string) *Client

func (*Client) SetHostGetter

func (c *Client) SetHostGetter(hg HostGetter) *Client

type HasData

type HasData struct {
	Data interface{} `json:"data,omitempty"`
}

func (*HasData) GetData

func (w *HasData) GetData() interface{}

func (*HasData) SetData

func (w *HasData) SetData(data interface{})

type HostGetter

type HostGetter func() string

type Request

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

Request is builder for http.Request

func NewRequest

func NewRequest(method string, url string) *Request

NewRequest

func (*Request) Body

func (r *Request) Body(contentType string, body io.Reader) *Request

Body set body with content-type

func (*Request) Build

func (r *Request) Build(ctx context.Context) (*http.Request, error)

Build to http.Request

func (*Request) Do

func (r *Request) Do(ctx context.Context, clients ...*http.Client) (resp *Response, err error)

Do client.do

func (*Request) Form

func (r *Request) Form(form url.Values) *Request

Form set body with application/x-www-form-urlencoded

func (*Request) Header

func (r *Request) Header(key, value string) *Request

Header set header

func (*Request) JSON

func (r *Request) JSON(body interface{}) *Request

JSON set body with application/json

func (*Request) JSONOrError

func (r *Request) JSONOrError(body interface{}) (*Request, error)

JSONOrError set body with application/json return error when marshal failed

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

func (*Response) Bytes

func (r *Response) Bytes() (bytes []byte, err error)

func (*Response) String

func (r *Response) String() (string, error)

func (*Response) To

func (r *Response) To(v interface{}) error

func (*Response) ToInterface

func (r *Response) ToInterface() (interface{}, error)

func (*Response) UnwrapTo

func (r *Response) UnwrapTo(w ResponseWrapper, v interface{}) error

func (*Response) UnwrapToInterface

func (r *Response) UnwrapToInterface(w ResponseWrapper) (interface{}, error)

type ResponseWrapper

type ResponseWrapper interface {
	Error() error
	SetData(interface{})
	GetData() interface{}
}

see wrapper_test.go

Jump to

Keyboard shortcuts

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