client

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: MIT Imports: 34 Imported by: 1

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
View Source
const RangeFormat = "bytes=%d-%d/%d"
View Source
const RangeFormat2 = "bytes=%d-%d/*"

Variables

View Source
var (
	DefaultHttpClient = newHttpClient(ClientTypeApi)
	DefaultLogLevel   = LogLevelError
)
View Source
var (
	ErrNotFound            = fmt.Errorf("not found")
	ErrRangeNotSatisfiable = fmt.Errorf("range not satisfiable")
)
View Source
var (
	ContentTypeKey = http.CanonicalHeaderKey("Content-Type")
)
View Source
var (
	DefaultClient = New().DisableLog()
)
View Source
var DefaultDownloadHttpClient = newDownloadHttpClient()
View Source
var DefaultDownloader = NewDownloader()
View Source
var DefaultUploader = NewUploader()

Functions

func CloseReaderWrap added in v0.0.27

func CloseReaderWrap(err error) error

func DefaultHeader

func DefaultHeader() httpi.MapHeader

func DefaultLogger

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

func Delete

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

func Download

func Download(filepath, url string) error

func DownloadImage

func DownloadImage(filepath, url string) error

func DownloadToDir

func DownloadToDir(dir, url string) error

func Get

func Get(url string, param, response any) error

func GetImage

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

func GetRandUserAgent

func GetRandUserAgent() string

func GetReader

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

func GetReaderWithHttpRequestOption added in v0.1.4

func GetReaderWithHttpRequestOption(url string, opts ...HttpRequestOption) (io.ReadCloser, error)

func GetStream

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

func GetStreamX

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

func GetX

func GetX(url string, response any) error

func ImageOption

func ImageOption(req *http.Request)

func NewReplaceHttpRequest

func NewReplaceHttpRequest(r *http.Request) *replaceHttpRequest

func Post

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

func Put

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

func ReplaceHttpRequest

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

func SetProxyEnv

func SetProxyEnv(url string)

func SetTag

func SetTag(t string)

func UserAgentChrome

func UserAgentChrome(version string) string

Types

type AccessLog

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

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

Client ...

func DefaultHeaderClient

func DefaultHeaderClient() *Client

func New

func New() *Client

func (*Client) AddHeader

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

func (*Client) BasicAuth

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

func (*Client) Clone

func (c *Client) Clone() *Client

func (*Client) Delete

func (c *Client) Delete(url string, param, response any) error

func (*Client) DeleteRequest

func (c *Client) DeleteRequest(url string) *Request

func (*Client) DisableLog

func (c *Client) DisableLog() *Client

func (*Client) Do

func (c *Client) Do(r *Request, param, response any) error

func (*Client) Get

func (c *Client) Get(url string, param, response any) error

func (*Client) GetRaw added in v0.0.39

func (c *Client) GetRaw(url string, param any) (RawBytes, error)

func (*Client) GetRawX added in v0.0.39

func (c *Client) GetRawX(url string) (RawBytes, error)

func (*Client) GetRequest

func (c *Client) GetRequest(url string) *Request

func (*Client) GetStream

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

func (*Client) GetStreamX

func (c *Client) GetStreamX(url string) (io.ReadCloser, error)

func (*Client) GetX

func (c *Client) GetX(url string, response any) error

func (*Client) Header

func (c *Client) Header(header httpi.Header) *Client

func (*Client) HttpClient

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

func (*Client) HttpRequestOption added in v0.1.4

func (c *Client) HttpRequestOption(opts ...HttpRequestOption) *Downloader

func (*Client) LogLevel

func (c *Client) LogLevel(lvl LogLevel) *Client

func (*Client) Logger

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

func (*Client) ParseTag

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

func (*Client) Post

func (c *Client) Post(url string, param, response any) error

func (*Client) PostRequest

func (c *Client) PostRequest(url string) *Request

func (*Client) Proxy

func (c *Client) Proxy(proxyUrl string) *Client

func (*Client) Put

func (c *Client) Put(url string, param, response any) error

func (*Client) PutRequest

func (c *Client) PutRequest(url string) *Request

func (*Client) ReqDataHandler added in v0.1.4

func (c *Client) ReqDataHandler(handler func(data []byte) ([]byte, error)) *Client

func (*Client) Request

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

func (*Client) ResDataHandler

func (c *Client) ResDataHandler(handler func(data []byte) ([]byte, error)) *Client

func (*Client) ResetProxy added in v0.0.31

func (c *Client) ResetProxy() *Client

func (*Client) ResponseHandler

func (c *Client) ResponseHandler(handler func(response *http.Response) (retry bool, reader io.Reader, err error)) *Client

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

func (*Client) RetryHandler

func (c *Client) RetryHandler(handle func(r *http.Request)) *Client

func (*Client) RetryTimes

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

func (*Client) RetryTimesWithInterval

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

func (*Client) SetHttpClient

func (c *Client) SetHttpClient(opt HttpClientOption) *Client

func (*Client) Timeout

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

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

type ClientType added in v0.1.4

type ClientType uint8
const (
	ClientTypeApi      ClientType = iota
	ClientTypeDownload ClientType = iota
	ClientTypeUpload   ClientType = iota
)

type ContentType

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

func (*ContentType) Decode

func (c *ContentType) Decode(contentType string)

func (ContentType) String

func (c ContentType) String() string

type DownloadMode

type DownloadMode uint16
const (
	DModeOverwrite DownloadMode = 1 << iota
	DModeContinue
	DModeMultipart   // TODO 分块下载后合并
	DModeMultiThread // TODO 暂时没找到并发写文件的方法,可以并发下载,顺序写入
)

type DownloadReq

type DownloadReq struct {
	Url string
	// contains filtered or unexported fields
}

func NewDownloadReq

func NewDownloadReq(url string) *DownloadReq

func (*DownloadReq) AddHeader

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

func (*DownloadReq) ConcurrencyDownload

func (c *DownloadReq) ConcurrencyDownload(filepath string, url string, concurrencyNum int) error

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

func (*DownloadReq) ContinuationDownload

func (c *DownloadReq) ContinuationDownload(filepath string) error

func (*DownloadReq) Download

func (c *DownloadReq) Download(filepath string) error

func (*DownloadReq) Downloader

func (req *DownloadReq) Downloader(c *Downloader) *DownloadReq

func (*DownloadReq) GetMode

func (c *DownloadReq) GetMode() DownloadMode

func (*DownloadReq) GetReader

func (c *DownloadReq) GetReader() (io.ReadCloser, error)

func (*DownloadReq) GetResponse

func (c *DownloadReq) GetResponse() (*http.Response, error)

func (*DownloadReq) Mode

func (c *DownloadReq) Mode(mode DownloadMode) *DownloadReq

func (*DownloadReq) OverwriteMode

func (c *DownloadReq) OverwriteMode() *DownloadReq

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

func (*DownloadReq) SetDownloader

func (req *DownloadReq) SetDownloader(set func(c *Downloader)) *DownloadReq

type Downloader

type Downloader = Client

TODO: Range Status(206) PartialContent 下载

func NewDownloader

func NewDownloader() *Downloader

func (*Downloader) Download

func (c *Downloader) Download(filepath string, r *DownloadReq) error

func (*Downloader) DownloadReq

func (c *Downloader) DownloadReq(url string) *DownloadReq

type File added in v0.1.5

type File struct {
	Path string
	*os.File
}

func NewFile added in v0.1.11

func NewFile(path string) (*File, error)

func (*File) ToMutilPart added in v0.1.11

func (f *File) ToMutilPart(param string) *Multipart

type FromResponse

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

type HttpClientOption

type HttpClientOption func(client *http.Client)

type HttpRequestOption

type HttpRequestOption func(req *http.Request)

func AddHeader

func AddHeader(k, v string) HttpRequestOption

func SetAccept

func SetAccept(refer string) HttpRequestOption

func SetCookie

func SetCookie(cookie string) HttpRequestOption

func SetRefer

func SetRefer(refer string) HttpRequestOption

type LogLevel

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

type MarshalBody

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

type MarshalHeader

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

type MarshalQuery

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

type Multipart added in v0.1.11

type Multipart struct {
	Param       string
	Name        string
	ContentType string
	io.Reader
}

func NewMultipart added in v0.1.11

func NewMultipart(param, name, contentType string, reader io.Reader) *Multipart

type Option

type Option func(req *Client) *Client

type RawBytes

type RawBytes = []byte

type Request

type Request struct {
	Method, Url string
	// contains filtered or unexported fields
}

func DefaultHeaderRequest

func DefaultHeaderRequest() *Request

func DeleteRequest

func DeleteRequest(url string) *Request

func GetRequest

func GetRequest(url string) *Request

func NewRequest

func NewRequest(method, url string) *Request

func PostRequest

func PostRequest(url string) *Request

func PutRequest

func PutRequest(url string) *Request

func (*Request) AddHeader

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

func (*Request) Client

func (req *Request) Client(c *Client) *Request

func (*Request) ContentType

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

func (*Request) Context

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

func (*Request) Do

func (req *Request) Do(param, response any, opts ...RequestOption) error

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

func (*Request) DoEmpty

func (req *Request) DoEmpty() error

func (*Request) DoNoParam

func (req *Request) DoNoParam(response any) error

func (*Request) DoNoResponse

func (req *Request) DoNoResponse(param any) error

func (*Request) DoRaw

func (req *Request) DoRaw(param any) (RawBytes, error)

func (*Request) DoStream

func (req *Request) DoStream(param any) (io.ReadCloser, error)

func (*Request) Header added in v0.0.35

func (req *Request) Header(header httpi.SliceHeader) *Request

func (*Request) SetClient

func (req *Request) SetClient(set func(c *Client)) *Request

type RequestOption added in v0.0.44

type RequestOption func(req *Request) *Request

type ResponseBody

type ResponseBody httpi.ResAnyData

func (*ResponseBody) CheckError

func (res *ResponseBody) CheckError() error

type ResponseBody2

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

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

type ResponseHandler

type ResponseHandler func(client *http.Response)

type SetRequest

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

type UnmarshalBody

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

type UnmarshalHeader

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

type UnmarshalQuery

type UnmarshalQuery interface {
	UnmarshalQuery(query string) error
}

type UploadMode

type UploadMode uint16
const (
	UModeNormal UploadMode = iota
	UModeStream
	UModeChunked
	UModeChunkedConcurrent
)

type UploadReq added in v0.1.4

type UploadReq struct {
	Url string
	// contains filtered or unexported fields
}

func NewUploadReq added in v0.1.5

func NewUploadReq(url string) *UploadReq

func (*UploadReq) Boundary added in v0.1.4

func (r *UploadReq) Boundary(boundary string) *UploadReq

func (*UploadReq) ChunkSize added in v0.1.11

func (r *UploadReq) ChunkSize(chunkSize int) *UploadReq

func (*UploadReq) Mode added in v0.1.11

func (r *UploadReq) Mode(mode UploadMode) *UploadReq

func (*UploadReq) UploadMultipart added in v0.1.5

func (r *UploadReq) UploadMultipart(formData map[string]string, files ...*Multipart) error

func (*UploadReq) UploadMultipartChunked added in v0.1.11

func (r *UploadReq) UploadMultipartChunked(formData map[string]string, file Multipart) error

默认单文件

func (*UploadReq) UploadRaw added in v0.1.11

func (r *UploadReq) UploadRaw(reader io.Reader, name string) error

func (*UploadReq) UploadRawChunked added in v0.1.11

func (r *UploadReq) UploadRawChunked(reader io.Reader, name string) error

func (*UploadReq) UploadStream added in v0.1.11

func (r *UploadReq) UploadStream(oReader io.Reader) error

func (*UploadReq) Uploader added in v0.1.11

func (r *UploadReq) Uploader(u *Uploader) *UploadReq

type Uploader

type Uploader = Client

func NewUploader added in v0.1.5

func NewUploader() *Uploader

func (*Uploader) UploadReq added in v0.1.5

func (u *Uploader) UploadReq(url string) *UploadReq

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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