client

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT, Apache-2.0 Imports: 32 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
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

func CloseReaderWrap(err error) error

CloseReaderWrap closes and releases resources.

func DefaultHeader

func DefaultHeader() http.Header

DefaultHeader returns the result.

func DefaultLogger

func DefaultLogger(param *AccessLogParam, err error)

DefaultLogger performs the operation.

func Delete

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

Delete removes or resets state.

func Download

func Download(filepath, url string) error

Download executes the operation.

func DownloadImage

func DownloadImage(filepath, url string) error

DownloadImage executes the operation.

func DownloadToDir

func DownloadToDir(dir, url string) error

DownloadToDir executes the operation.

func Get

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

Get returns the value.

func GetImage

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

GetImage returns the value.

func GetRandUserAgent

func GetRandUserAgent() string

GetRandUserAgent returns the value.

func GetReader

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

GetReader returns the value.

func GetReaderWithHttpRequestOptions

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

GetReaderWithHttpRequestOptions returns the value.

func GetStream

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

GetStream returns the value.

func GetStreamX

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

GetStreamX returns the value.

func GetX

func GetX(url string, response any) error

GetX returns the value.

func ImageOption

func ImageOption(req *http.Request)

ImageOption performs the operation.

func Post

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

Post performs the operation.

func Put

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

Put updates or inserts a value.

func SetProxyEnv

func SetProxyEnv(url string)

SetProxyEnv updates or inserts a value.

func SetTag

func SetTag(t string)

SetTag updates or inserts a value.

func UserAgentChrome

func UserAgentChrome(version string) string

UserAgentChrome returns the result.

Types

type AccessLog

type AccessLog func(param *AccessLogParam, err error)

type AccessLogParam

type AccessLogParam struct {
	Method, Url       string
	Request           *http.Request
	Response          *http.Response
	ReqBody, RespBody []byte
	Duration          time.Duration
}

type Client

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

Client defines a type.

func DefaultHeaderClient

func DefaultHeaderClient() *Client

DefaultHeaderClient returns the result.

func New

func New() *Client

New creates a new instance.

func (*Client) AddHeader

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

AddHeader updates or inserts a value.

func (*Client) BaseUrl added in v1.11.4

func (d *Client) BaseUrl(url string) *Client

BaseUrl returns the result.

func (*Client) BasicAuth

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

BasicAuth returns the result.

func (*Client) Clone

func (d *Client) Clone() *Client

Clone returns the result.

func (*Client) Delete

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

Delete removes or resets state.

func (*Client) DeleteRequest

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

DeleteRequest removes or resets state.

func (*Client) DisableLog

func (d *Client) DisableLog() *Client

DisableLog returns the result.

func (*Client) Do

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

Do executes the operation.

func (*Client) Get

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

Get returns the value.

func (*Client) GetRaw

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

GetRaw returns the value.

func (*Client) GetRawX

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

GetRawX returns the value.

func (*Client) GetRequest

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

GetRequest returns the value.

func (*Client) GetStream

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

GetStream returns the value.

func (*Client) GetStreamX

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

GetStreamX returns the value.

func (*Client) GetX

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

GetX returns the value.

func (*Client) Header

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

Header returns the result.

func (*Client) HeaderX added in v1.7.12

func (d *Client) HeaderX(header httpx.Header) *Client

HeaderX returns the result.

func (*Client) HttpClient

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

HttpClient returns the result.

func (*Client) HttpRequestOptions

func (d *Client) HttpRequestOptions(opts ...HttpRequestOption) *Client

HttpRequestOptions returns the result.

func (*Client) LogLevel

func (d *Client) LogLevel(lvl LogLevel) *Client

LogLevel returns the result.

func (*Client) Logger

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

Logger returns the result.

func (*Client) NoProxy added in v1.11.2

func (d *Client) NoProxy() *Client

NoProxy returns the result.

func (*Client) Post

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

Post performs the operation.

func (*Client) PostRequest

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

PostRequest returns the result.

func (*Client) Proxy

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

Proxy returns the result.

func (*Client) Put

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

Put updates or inserts a value.

func (*Client) PutRequest

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

PutRequest updates or inserts a value.

func (*Client) ReqBodyMarshal added in v1.6.6

func (d *Client) ReqBodyMarshal(handler func(v any) ([]byte, error)) *Client

ReqBodyMarshal returns the result.

func (*Client) Request

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

Request returns the result.

func (*Client) ResetProxy

func (d *Client) ResetProxy() *Client

ResetProxy removes or resets state.

func (*Client) RespBodyHandler added in v1.6.6

func (d *Client) RespBodyHandler(handler func(data []byte) ([]byte, error)) *Client

RespBodyHandler returns the result.

func (*Client) RespBodyUnMarshal added in v1.6.6

func (d *Client) RespBodyUnMarshal(handler func(data []byte, v any) error) *Client

RespBodyUnMarshal returns the result.

func (*Client) ResponseHandler

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

ResponseHandler returns the result.

func (*Client) RetryHandler

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

RetryHandler returns the result.

func (*Client) RetryTimes

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

RetryTimes returns the result.

func (*Client) RetryTimesWithInterval

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

RetryTimesWithInterval returns the result.

func (*Client) SetHttpClient

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

SetHttpClient updates or inserts a value.

func (*Client) Timeout

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

Timeout returns the result.

type ClientType

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

type ContentType

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

func (*ContentType) Decode

func (c *ContentType) Decode(contentType string)

Decode formats or converts the value.

func (ContentType) String

func (c ContentType) String() string

String returns the string representation.

type DownloadMode

type DownloadMode uint16
const (
	DModeOverwrite DownloadMode = 1 << iota
	DModeForceContinue
)

type DownloadReq

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

func NewDownloadReq

func NewDownloadReq(url string) *DownloadReq

NewDownloadReq creates and returns a new instance.

func (*DownloadReq) AddHeader

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

AddHeader updates or inserts a value.

func (*DownloadReq) ConcurrencyDownload

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

ConcurrencyDownload performs the operation.

func (*DownloadReq) Context added in v1.11.2

func (dReq *DownloadReq) Context(ctx context.Context) *DownloadReq

Context returns the result.

func (*DownloadReq) Download

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

Download executes the operation.

func (*DownloadReq) DownloadAttachment

func (dReq *DownloadReq) DownloadAttachment(dir string) error

DownloadAttachment executes the operation.

func (*DownloadReq) Downloader

func (dReq *DownloadReq) Downloader(c *Downloader) *DownloadReq

Downloader executes the operation.

func (*DownloadReq) GetMode

func (dReq *DownloadReq) GetMode() DownloadMode

GetMode returns the value.

func (*DownloadReq) GetReader

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

GetReader returns the value.

func (*DownloadReq) GetResponse

func (dReq *DownloadReq) GetResponse(options ...func(*http.Request)) (*http.Response, error)

GetResponse returns the value.

func (*DownloadReq) Header

func (dReq *DownloadReq) Header(header http.Header) *DownloadReq

Header returns the result.

func (*DownloadReq) Mode

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

Mode returns the result.

func (*DownloadReq) OverwriteMode

func (dReq *DownloadReq) OverwriteMode() *DownloadReq

OverwriteMode returns the result.

func (*DownloadReq) SetDownloader

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

SetDownloader updates or inserts a value.

type Downloader

type Downloader = Client

TODO: Range Status(206) PartialContent download

func NewDownloader

func NewDownloader(options ...Option) *Downloader

NewDownloader creates and returns a new instance.

func (*Downloader) Download

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

Download executes the operation.

func (*Downloader) DownloadAttachment

func (d *Downloader) DownloadAttachment(dir string, r *DownloadReq)

DownloadAttachment executes the operation.

func (*Downloader) DownloadReq

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

DownloadReq executes the operation.

type File

type File struct {
	Path string
	*os.File
}

func NewFile

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

NewFile creates and returns a new instance.

func (*File) ToMutilPart

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

ToMutilPart converts the value.

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

AddHeader updates or inserts a value.

func SetAccept

func SetAccept(refer string) HttpRequestOption

SetAccept updates or inserts a value.

func SetCookie

func SetCookie(cookie string) HttpRequestOption

SetCookie updates or inserts a value.

func SetRefer

func SetRefer(refer string) HttpRequestOption

SetRefer updates or inserts a value.

func (HttpRequestOption) ToOption

func (o HttpRequestOption) ToOption() Option

ToOption converts the value.

type LogLevel

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

type Multipart

type Multipart struct {
	Name     string
	Value    string
	Filename string
	Header   textproto.MIMEHeader
	io.Reader
}

func NewMultipart

func NewMultipart(name, filename string, header textproto.MIMEHeader, reader io.Reader) *Multipart

NewMultipart creates and returns a new instance.

type Option

type Option func(req *Client)

type RawBytes

type RawBytes = []byte

type Request

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

func DefaultHeaderRequest

func DefaultHeaderRequest() *Request

DefaultHeaderRequest returns the result.

func DeleteRequest

func DeleteRequest(url string) *Request

DeleteRequest removes or resets state.

func GetRequest

func GetRequest(url string) *Request

GetRequest returns the value.

func NewRequest

func NewRequest(method, url string, opts ...RequestOption) *Request

NewRequest creates and returns a new instance.

func PostRequest

func PostRequest(url string) *Request

PostRequest returns the result.

func PutRequest

func PutRequest(url string) *Request

PutRequest updates or inserts a value.

func (*Request) AddHeader

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

AddHeader updates or inserts a value.

func (*Request) Client

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

Client returns the result.

func (*Request) ContentType

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

ContentType returns the result.

func (*Request) Context

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

Context returns the result.

func (*Request) Do

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

Do executes the operation.

func (*Request) DoRaw

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

DoRaw executes the operation.

func (*Request) DoResponse added in v1.11.8

func (req *Request) DoResponse(param any) (*http.Response, error)

DoResponse executes the operation.

func (*Request) DoStream

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

DoStream executes the operation.

func (*Request) Header

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

Header returns the result.

func (*Request) HeaderX added in v1.7.12

func (req *Request) HeaderX(header httpx.Header) *Request

HeaderX returns the result.

type RequestOption

type RequestOption func(req *Request)

type ResponseBodyCheck

type ResponseBodyCheck interface {
	CheckError() error
}

type SetRequest

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

type UploadMode

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

type UploadReq

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

func NewUploadReq

func NewUploadReq(url string) *UploadReq

NewUploadReq creates and returns a new instance.

func (*UploadReq) Boundary

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

Boundary returns the result.

func (*UploadReq) ChunkSize

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

ChunkSize returns the result.

func (*UploadReq) ConcurrentUploadReaderChunked added in v1.11.2

func (r *UploadReq) ConcurrentUploadReaderChunked(reader io.ReaderAt, name string, total int64, concurrencyNum int) error

ConcurrentUploadReaderChunked performs the operation.

func (*UploadReq) Context added in v1.11.2

func (r *UploadReq) Context(ctx context.Context) *UploadReq

Context returns the result.

func (*UploadReq) Mode

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

Mode returns the result.

func (*UploadReq) Upload added in v1.11.2

func (r *UploadReq) Upload(filepath string) error

Upload performs the operation.

func (*UploadReq) UploadMultipart

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

UploadMultipart performs the operation.

func (*UploadReq) UploadReader added in v1.11.2

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

UploadReader performs the operation.

func (*UploadReq) UploadReaderChunked added in v1.11.2

func (r *UploadReq) UploadReaderChunked(reader io.ReaderAt, name string, total int64) error

UploadReaderChunked performs the operation.

func (*UploadReq) Uploader

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

Uploader returns the result.

type Uploader

type Uploader = Client

func NewUploader

func NewUploader() *Uploader

NewUploader creates and returns a new instance.

func (*Uploader) UploadReq

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

UploadReq returns the result.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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