client

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommonClient = &http.Client{
	CheckRedirect: func(req *http.Request, via []*http.Request) error {
		for _, v := range append(via[len(via)-1].Cookies(), req.Response.Cookies()...) {
			ck, err := req.Cookie(v.Name)
			if err != nil {
				if errors.Is(err, http.ErrNoCookie) {
					req.AddCookie(v)
				} else {
					return err
				}
				continue
			}
			ck.Value = v.Value
		}
		return nil
	},
}

CommonClient is set as DefaultClient default you maybe puzzled about CheckRedirect is used instead of cookieJar. it's because cookieJar is global, a cookie from one request may affect in another way that never be considered.

Functions

func Do

func Do(req *http.Request) (*http.Response, error)

func Get

func Get(req *http.Request, data interface{}) error

func Post

func Post(req *http.Request) ([]byte, error)

Types

type Client

type Client interface {
	Do(r *http.Request) (*http.Response, error)
}
var DefaultClient Client = CommonClient

DefaultClient do all requests, you can change it as you implement the interface.

type ErrNotOk

type ErrNotOk struct {
	StatusCode int
	Body       string
}

func (*ErrNotOk) Error

func (e *ErrNotOk) Error() string

for further err, do type assertion

type LowNetworkClient

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

LowNetworkClient example

func NewLowNetworkClient

func NewLowNetworkClient(timeout time.Duration, retry int) *LowNetworkClient

func (*LowNetworkClient) Do

Do is rewritten to retry

Jump to

Keyboard shortcuts

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