client

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserAgent1         = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"
	UserAgentChrome95  = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
	UserAgentChrome117 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
	UserAgentIphone    = "" /* 139-byte string literal not displayed */
)
View Source
const DownloadKey = fs.DownloadKey

Variables

View Source
var (
	ErrNotFound = fmt.Errorf("not found")
)

Functions

func DefaultLogger

func DefaultLogger(url, method, auth string, reqBody, respBody *Body, status int, process time.Duration, err error)

func DisableLog

func DisableLog()

func DownloadFile

func DownloadFile(filepath, url string) error

func DownloadImage

func DownloadImage(filepath, url string) error

func GetFile

func GetFile(url string) (io.ReadCloser, error)

func GetFileWithReqOption

func GetFileWithReqOption(url string, opts ...RequestOption) (io.ReadCloser, error)

func GetImage

func GetImage(url string) (io.ReadCloser, error)

func GetRandUserAgent

func GetRandUserAgent() string

func ImageOption

func ImageOption(req *http.Request)

func NewReplaceHttpRequest

func NewReplaceHttpRequest(r *http.Request) *replaceHttpRequest

func ReplaceHttpRequest

func ReplaceHttpRequest(r *http.Request, url, method string, body io.ReadCloser) *http.Request

func ResetProxy

func ResetProxy()

func ResolveURL

func ResolveURL(u *url.URL, p string) string

func SetDefaultClient

func SetDefaultClient(client *http.Client)

func SetDefaultLogger

func SetDefaultLogger(logger LogCallback)

func SetProxy

func SetProxy(url string)

func SetProxyEnv

func SetProxyEnv(url string)

func SetTimeout

func SetTimeout(timeout time.Duration)

func SimpleDelete

func SimpleDelete(url string, param, response interface{}) error

func SimpleGet

func SimpleGet(url string, response any) error

func SimpleGetStream

func SimpleGetStream(url string) (io.ReadCloser, error)

func SimplePost

func SimplePost(url string, param, response interface{}) error

func SimplePut

func SimplePut(url string, param, response interface{}) error

func UrlAppendParam

func UrlAppendParam(url string, param interface{}) string

func UrlParam

func UrlParam(param interface{}) string

func UserAgentChrome

func UserAgentChrome(version string) string

Types

type Body

type Body struct {
	Data        []byte
	ContentType ContentType
}

func NewBody

func NewBody(data []byte, contentType ContentType) *Body

func (*Body) IsJson

func (b *Body) IsJson() bool

func (*Body) IsProtobuf

func (b *Body) IsProtobuf() bool

type ContentType

type ContentType uint8
const (
	ContentTypeJson ContentType = iota
	ContentTypeForm
	ContentTypeFormData
	ContentTypeProtobuf
	ContentTypeText
	ContentTypeImage
	ContentTypeBinary
)

type Downloader

type Downloader struct {
	Client          *http.Client
	Request         *http.Request
	Mode            uint8 // 模式,0-强制覆盖,1-不存在下载,3-断续下载
	ResponseHandler func(response []byte) ([]byte, error)
}

TODO: Range StatusPartialContent 下载

func NewDownloader

func NewDownloader(url string) (*Downloader, error)

func (*Downloader) AddHeader

func (d *Downloader) AddHeader(header, value string) *Downloader

func (*Downloader) ConcurrencyDownloadFile

func (d *Downloader) ConcurrencyDownloadFile(filepath string, concurrencyNum int) error

TODO: 利用简单任务调度实现

func (*Downloader) ContinuationDownloadFile

func (d *Downloader) ContinuationDownloadFile(filepath string) error

func (*Downloader) DownloadFile

func (d *Downloader) DownloadFile(filepath string) error

func (*Downloader) GetResponse

func (d *Downloader) GetResponse() (*http.Response, error)

func (*Downloader) OverwriteMode

func (d *Downloader) OverwriteMode() *Downloader

强制覆盖,如果文件已存在,不下载覆盖

func (*Downloader) SetClient

func (d *Downloader) SetClient(set func(*http.Client)) *Downloader

func (*Downloader) SetHeader

func (d *Downloader) SetHeader(header Header) *Downloader

func (*Downloader) SetRequest

func (d *Downloader) SetRequest(set RequestOption) *Downloader

func (*Downloader) WithClient

func (d *Downloader) WithClient(c *http.Client) *Downloader

func (*Downloader) WithMode

func (d *Downloader) WithMode(mode uint8) *Downloader

func (*Downloader) WithOptions

func (d *Downloader) WithOptions(opts ...RequestOption) *Downloader

func (*Downloader) WithRange

func (d *Downloader) WithRange(begin, end string) *Downloader

func (*Downloader) WithRequest

func (d *Downloader) WithRequest(c *http.Request) *Downloader

func (*Downloader) WithResponseHandler

func (d *Downloader) WithResponseHandler(responseHandler func(response []byte) ([]byte, error)) *Downloader
type Header []string

func DefaultHeader

func DefaultHeader() Header

func NewHeader

func NewHeader() *Header

func (*Header) Add

func (h *Header) Add(k, v string) *Header

func (Header) Clone

func (h Header) Clone() Header

func (*Header) Set

func (h *Header) Set(k, v string) *Header

type LogCallback

type LogCallback func(url, method, auth string, reqBody, respBody *Body, status int, process time.Duration, err error)

type LogLevel

type LogLevel int8
const (
	LogLevelSilent LogLevel = iota
	LogLevelError
	LogLevelInfo
)

type Logger

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

type Option

type Option func(req *Request) *Request

type RawBytes

type RawBytes = []byte

type Request

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

Request ...

func DefaultHeaderRequest

func DefaultHeaderRequest() *Request

func New

func New() *Request

func NewDeleteRequest

func NewDeleteRequest(url string) *Request

func NewGetRequest

func NewGetRequest(url string) *Request

func NewPostRequest

func NewPostRequest(url string) *Request

func NewPutRequest

func NewPutRequest(url string) *Request

func NewRequest

func NewRequest(url, method string) *Request

func (*Request) AddHeader

func (req *Request) AddHeader(k, v string) *Request

func (*Request) BasicAuth added in v1.2.12

func (req *Request) BasicAuth(authUser, authPass string)

func (*Request) CachedHeader

func (req *Request) CachedHeader(key string) *Request

func (*Request) Client added in v1.2.12

func (req *Request) Client(client *http.Client) *Request

func (*Request) ContentType

func (req *Request) ContentType(contentType ContentType) *Request

func (*Request) Context added in v1.2.12

func (req *Request) Context(ctx context.Context) *Request

func (*Request) Delete

func (req *Request) Delete(url string, param, response interface{}) error

func (*Request) DisableLog

func (req *Request) DisableLog() *Request

func (*Request) Do

func (req *Request) Do(param, response interface{}) error

Do create a HTTP request

func (*Request) DoEmpty

func (req *Request) DoEmpty() error

func (*Request) DoRaw

func (req *Request) DoRaw(param interface{}) (RawBytes, error)

func (*Request) DoStream

func (req *Request) DoStream(param interface{}) (io.ReadCloser, error)

func (*Request) DoWithNoParam

func (req *Request) DoWithNoParam(response interface{}) error

func (*Request) DoWithNoResponse

func (req *Request) DoWithNoResponse(param interface{}) error

func (*Request) Get

func (req *Request) Get(url string, response interface{}) error

func (*Request) Header added in v1.2.12

func (req *Request) Header(header Header) *Request

func (*Request) LogLevel

func (req *Request) LogLevel(lvl LogLevel) *Request

func (*Request) Logger added in v1.2.12

func (req *Request) Logger(logger LogCallback) *Request

func (*Request) Method

func (req *Request) Method(method string) *Request

func (*Request) Post

func (req *Request) Post(url string, param, response interface{}) error

func (*Request) Proxy added in v1.2.12

func (req *Request) Proxy(url string) *Request

func (*Request) Put

func (req *Request) Put(url string, param, response interface{}) error

func (*Request) ResponseHandler

func (req *Request) ResponseHandler(handler func(response *http.Response) (retry bool, data []byte, err error)) *Request

handler 返回值:是否重试,返回数据,错误

func (*Request) RetryHandler

func (req *Request) RetryHandler(handle func(*Request)) *Request

func (*Request) RetryTimes

func (req *Request) RetryTimes(retryTimes int) *Request

func (*Request) RetryTimesWithInterval

func (req *Request) RetryTimesWithInterval(retryTimes int, retryInterval time.Duration) *Request

func (*Request) Tag added in v1.2.12

func (req *Request) Tag(tag string) *Request

func (*Request) Timeout

func (req *Request) Timeout(timeout time.Duration) *Request

设置过期时间,仅对单次请求有效

func (*Request) Url

func (req *Request) Url(url string) *Request

type RequestOption

type RequestOption func(req *http.Request)

func SetAccept

func SetAccept(refer string) RequestOption

func SetCookie

func SetCookie(cookie string) RequestOption

func SetHeader

func SetHeader(header Header) RequestOption

func SetRefer

func SetRefer(refer string) RequestOption

type ResponseBody

type ResponseBody struct {
	Status  int    `json:"status"`
	Data    any    `json:"data"`
	Message string `json:"message"`
}

func (*ResponseBody) CheckError

func (res *ResponseBody) CheckError() error

type ResponseBody2

type ResponseBody2 struct {
	Code int    `json:"code"`
	Data any    `json:"data"`
	Msg  string `json:"msg"`
}

func (*ResponseBody2) CheckError

func (res *ResponseBody2) CheckError() error

type ResponseBodyCheck

type ResponseBodyCheck interface {
	CheckError() error
}

func CommonResponse

func CommonResponse(response interface{}) ResponseBodyCheck

func CommonResponse2

func CommonResponse2(response interface{}) ResponseBodyCheck

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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