client

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DModeOverwrite DownloadMode = iota
	DModeNotExist
	DModeContinue          = DModeNotExist << 1
	DModeMultipartDownload = DModeNotExist << 2 // TODO
)
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 (
	DefaultHttpClient = newHttpClient()
	DefaultLogLevel   = LogLevelError
)
View Source
var (
	ErrNotFound = fmt.Errorf("not found")
)

Functions

func DefaultHeader

func DefaultHeader() http.Header

func DefaultLogger

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

func Delete added in v1.5.11

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

func DisableLog

func DisableLog()

func Download added in v1.5.11

func Download(filepath, url string) error

func DownloadImage

func DownloadImage(filepath, url string) error

func DownloadToDir added in v1.5.11

func DownloadToDir(dir, url string) error

func Get added in v1.5.11

func Get(url string, param, response any) 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 GetNP added in v1.14.0

func GetNP(url string, response any) error

func GetRandUserAgent

func GetRandUserAgent() string

func GetStream added in v1.5.11

func GetStream(url string, param any) (io.ReadCloser, error)

func GetStreamNP added in v1.14.0

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

func ImageOption

func ImageOption(req *http.Request)

func NewReplaceHttpRequest

func NewReplaceHttpRequest(r *http.Request) *replaceHttpRequest

func Post added in v1.5.11

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

func Put added in v1.5.11

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

func ReplaceHttpRequest

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

func ResetProxy

func ResetProxy()

func SetAccessLog added in v1.5.11

func SetAccessLog(log AccessLog)

func SetHttpClient added in v1.5.11

func SetHttpClient(client *http.Client)

func SetProxy

func SetProxy(url string)

func SetProxyEnv

func SetProxyEnv(url string)

func SetTag added in v1.5.11

func SetTag(t string)

func SetTimeout

func SetTimeout(timeout time.Duration)

func UserAgentChrome

func UserAgentChrome(version string) string

Types

type AccessLog added in v1.5.11

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

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 Client added in v1.5.11

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

Client ...

func DefaultHeaderClient added in v1.5.11

func DefaultHeaderClient() *Client

func New

func New() *Client

func (*Client) AddHeader added in v1.5.11

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

func (*Client) BasicAuth added in v1.5.11

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

func (*Client) Clone added in v1.5.11

func (req *Client) Clone() *Client

func (*Client) ContentType added in v1.14.0

func (req *Client) ContentType(contentType ContentType) *Client

func (*Client) Context added in v1.14.0

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

func (*Client) Delete added in v1.5.11

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

func (*Client) DisableLog added in v1.5.11

func (req *Client) DisableLog() *Client

func (*Client) Do added in v1.5.11

func (req *Client) Do(method, url string, param, response interface{}) error

Do create a HTTP request param: 请求参数 目前只支持编码为json 或 url-encoded

func (*Client) DoEmpty added in v1.14.0

func (req *Client) DoEmpty(method, url string) error

func (*Client) DoNoParam added in v1.14.0

func (req *Client) DoNoParam(method, url string, response interface{}) error

func (*Client) DoNoResponse added in v1.14.0

func (req *Client) DoNoResponse(method, url string, param interface{}) error

func (*Client) DoRaw added in v1.14.0

func (req *Client) DoRaw(method, url string, param interface{}) (RawBytes, error)

func (*Client) DoStream added in v1.14.0

func (req *Client) DoStream(method, url string, param interface{}) (io.ReadCloser, error)

func (*Client) Get added in v1.5.11

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

func (*Client) GetNP added in v1.14.0

func (req *Client) GetNP(url string, response interface{}) error

func (*Client) Header added in v1.5.11

func (req *Client) Header(header http.Header) *Client

func (*Client) HttpClient added in v1.5.11

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

func (*Client) LogLevel added in v1.5.11

func (req *Client) LogLevel(lvl LogLevel) *Client

func (*Client) Logger added in v1.5.11

func (req *Client) Logger(logger AccessLog) *Client

func (*Client) ParseTag added in v1.5.11

func (req *Client) ParseTag(tag string) *Client

func (*Client) Post added in v1.5.11

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

func (*Client) Proxy added in v1.5.11

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

func (*Client) Put added in v1.5.11

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

func (*Client) Request added in v1.5.11

func (req *Client) Request(method, url string) *Request

func (*Client) ResponseHandler added in v1.5.11

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

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

func (*Client) RetryHandler added in v1.5.11

func (req *Client) RetryHandler(handle func(*Client)) *Client

func (*Client) RetryTimes added in v1.5.11

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

func (*Client) RetryTimesWithInterval added in v1.5.11

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

func (*Client) Timeout added in v1.5.11

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

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

type ClientOption added in v1.14.0

type ClientOption func(client *http.Client)

type ContentType

type ContentType uint8
const (
	ContentTypeJson ContentType = iota
	ContentTypeForm
	ContentTypeFormData
	ContentTypeGrpc
	ContentTypeGrpcWeb
	ContentTypeXml
	ContentTypeText
	ContentTypeBinary
	ContentTypeApplication
	ContentTypeImage
	ContentTypeAudio
	ContentTypeVideo
)

func (*ContentType) Decode added in v1.5.11

func (c *ContentType) Decode(contentType string)

func (ContentType) String added in v1.5.11

func (c ContentType) String() string

type DownloadMode added in v1.5.11

type DownloadMode uint16

type Downloader

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

TODO: Range Status(206) PartialContent 下载

func NewDownloader

func NewDownloader() *Downloader

func (*Downloader) AddHeader

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

func (*Downloader) ConcurrencyDownload added in v1.14.0

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

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

func (*Downloader) ContinuationDownload added in v1.14.0

func (d *Downloader) ContinuationDownload(filepath, url string) error

func (*Downloader) Download added in v1.5.11

func (d *Downloader) Download(filepath, url string) error

func (*Downloader) ExistsSkipMode added in v1.7.0

func (d *Downloader) ExistsSkipMode() *Downloader

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

func (*Downloader) GetMode added in v1.14.0

func (d *Downloader) GetMode() DownloadMode

func (*Downloader) GetResponse

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

func (*Downloader) Header added in v1.5.11

func (d *Downloader) Header(header http.Header) *Downloader

func (*Downloader) HttpClient added in v1.5.11

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

func (*Downloader) Mode

func (d *Downloader) Mode(mode DownloadMode) *Downloader

func (*Downloader) Options added in v1.5.11

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

func (*Downloader) Range added in v1.14.0

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

func (*Downloader) ResponseHandler

func (d *Downloader) ResponseHandler(responseHandler func(response []byte) ([]byte, error)) *Downloader

type FromResponse added in v1.5.11

type FromResponse interface {
	FromResponse(response *http.Response)
}

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 MarshalBody added in v1.5.11

type MarshalBody interface {
	MarshalBody(contentType string) ([]byte, error)
}

type MarshalHeader added in v1.5.11

type MarshalHeader interface {
	MarshalHeader() ([]string, error)
}

type MarshalQuery added in v1.5.11

type MarshalQuery interface {
	MarshalQuery() (string, error)
}

type Option

type Option func(req *Client) *Client

type RawBytes

type RawBytes = []byte

type Request

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

func DefaultHeaderRequest

func DefaultHeaderRequest() *Request

func NewDelete added in v1.12.11

func NewDelete(url string) *Request

func NewGet added in v1.12.11

func NewGet(url string) *Request

func NewPost added in v1.12.11

func NewPost(url string) *Request

func NewPut added in v1.12.11

func NewPut(url string) *Request

func NewRequest

func NewRequest(method, url string) *Request

func (*Request) Delete

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

func (*Request) Do

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

func (*Request) DoEmpty

func (req *Request) DoEmpty() error

func (*Request) DoNoParam added in v1.5.11

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

func (*Request) DoNoResponse added in v1.5.11

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

func (*Request) DoRaw

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

func (*Request) DoStream

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

func (*Request) Get

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

func (*Request) Post

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

func (*Request) Put

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

type RequestOption

type RequestOption func(req *http.Request)

func AddHeader added in v1.5.11

func AddHeader(k, v string) RequestOption

func SetAccept

func SetAccept(refer string) RequestOption

func SetCookie

func SetCookie(cookie string) RequestOption

func SetRefer

func SetRefer(refer string) RequestOption

type ResponseBody

type ResponseBody httpi.ResAnyData

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 ResponseBody3 added in v1.5.11

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

func (*ResponseBody3) CheckError added in v1.5.11

func (res *ResponseBody3) CheckError() error

type ResponseBodyCheck

type ResponseBodyCheck interface {
	CheckError() error
}

func CommonResponse

func CommonResponse(response interface{}) ResponseBodyCheck

func CommonResponse2

func CommonResponse2(response interface{}) ResponseBodyCheck

func CommonResponse3 added in v1.5.11

func CommonResponse3(response interface{}) ResponseBodyCheck

type ResponseHandler added in v1.5.11

type ResponseHandler func(client *http.Response)

type SetRequest added in v1.5.11

type SetRequest interface {
	SetRequest(*http.Request)
}

type UnmarshalBody added in v1.5.11

type UnmarshalBody interface {
	UnmarshalBody(contentType string, body []byte) error
}

type UnmarshalHeader added in v1.5.11

type UnmarshalHeader interface {
	UnmarshalHeader(headers []string) error
}

type UnmarshalQuery added in v1.5.11

type UnmarshalQuery interface {
	UnmarshalQuery(query string) error
}

type UploadMode added in v1.5.11

type UploadMode uint8

TODO

const (
	UModeNormal UploadMode = iota
	UModeChunk
	UModeStream
)

type Uploader added in v1.5.11

type Uploader struct {
	Client  *http.Client
	Request *http.Request
	Mode    UploadMode // 模式,0-强制覆盖,1-不存在下载,2-断续下载
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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