client

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHttpClient

func NewHttpClient(timeout time.Duration) *http.Client

func NewHttpTransport

func NewHttpTransport() http.RoundTripper

Types

type Client

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

func MustClient

func MustClient(opt Option) *Client

func NewClient

func NewClient(opt Option) (*Client, error)

func (*Client) Create

func (c *Client) Create(method, url string, args ...any) Requester

func (*Client) Delete

func (c *Client) Delete(url string, args ...any) Requester

func (*Client) Do

func (c *Client) Do(r *http.Request, out any) error

Do 发送请求 有些情况下比较特殊 需要完全自定义request r,_:=http.NewRequestWithContext(ctx...) client.Do(r, &responseData)

func (*Client) Get

func (c *Client) Get(url string, args ...any) Requester

func (*Client) Patch

func (c *Client) Patch(url string, args ...any) Requester

func (*Client) Post

func (c *Client) Post(url string, args ...any) Requester

func (*Client) Put

func (c *Client) Put(url string, args ...any) Requester

type Option

type Option struct {
	// 默认使用 http.DefaultClient
	Client *http.Client
	// 解析相应 **必须包含**
	// 无需手动response.Body.Close() 会自动调用
	// func(res *http.Response, out any) error {
	// 	  dec:=json.NewDecode(res.Body)
	//    return dec.Decode(out)
	// }
	ParseResponse func(*http.Response, any) error
	// 修改请求
	// 比如统一添加auth 或 签名认证等信息
	ModfityRequest func(*http.Request)
	// 基础url
	BaseURL string
	// 熔断器配置
	BreakerSetting *gobreaker.Settings
}

type Requester

type Requester interface {
	Header(...string) Requester
	// Body request body
	// eq:string/[]bytes/io.Reader/url.Values/any(tojson)
	Body(any) Requester
	BuildRawContextRequest(context.Context) (*http.Request, error)
	Do(context.Context, any) error
}

Jump to

Keyboard shortcuts

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