urlx

package
v0.0.0-...-419c0c3 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MIT Imports: 24 Imported by: 0

README

urlx: http请求的封装, 链式API

Documentation

Index

Constants

View Source
const (
	HeaderContentType = "Content-Type"
	ContentTypeForm   = "application/x-www-form-urlencoded"
	CharsetUTF8       = "charset=utf-8"
)
View Source
const (
	MethodGet     = http.MethodGet
	MethodHead    = http.MethodHead
	MethodPost    = http.MethodPost
	MethodPut     = http.MethodPut
	MethodPatch   = http.MethodPatch
	MethodDelete  = http.MethodDelete
	MethodConnect = http.MethodConnect
	MethodOptions = http.MethodOptions
	MethodTrace   = http.MethodTrace
)
View Source
const DownloadTempExt = ".downloading"
View Source
const (
	HeaderRequestCookie = "Cookie" // Request Cookie

)
View Source
const Version = "0.0.1"

Variables

View Source
var (
	WindowsEdgeAgent   = UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43")
	MacSafariAgent     = UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15")
	IOSSafariAgent     = UserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4")
	AndroidWebkitAgent = UserAgent("Mozilla/5.0 (Linux; Android 11; SM-G9910) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
)

Functions

func DefaultAgent

func DefaultAgent(r *Request) error

func Func2AddE

func Func2AddE[T, T2 any](f func(T, T2)) func(T, T2) error

将无错误返回的函数包装成返回 error 的函数,便于统一处理

func Func2DelE

func Func2DelE[T, T2 any](f func(T, T2) error) func(T, T2)

将返回 error 的函数包装成无错误返回的函数,便于统一处理

func FuncAddE

func FuncAddE[T any](f func(T)) func(T) error

将无错误返回的函数包装成返回 error 的函数,便于统一处理

func FuncDelE

func FuncDelE[T any](f func(T) error) func(T)

将返回 error 的函数包装成无错误返回的函数,便于统一处理

func ProgressReader

func ProgressReader(r io.Reader, total float64, report ProgressReport) io.Reader

func ProgressStream

func ProgressStream(process func([]byte) (int, error), total float64, report ProgressReport) func([]byte) (int, error)

func ProgressWriter

func ProgressWriter(w io.Writer, total float64, report ProgressReport) io.Writer

Types

type Body

type Body = func(ctx context.Context) (body io.Reader, contentType string, err error) // 请求提交内容构造方法

type ClientOption

type ClientOption func(cli *http.Client) error

type HeaderOption

type HeaderOption = func(headers http.Header) // 请求头处理

type Logger

type Logger = func(ctx context.Context, msg string, args ...any)

type Option

type Option = func(*Request) error

func Accept

func Accept(accept string) Option

func Authorization

func Authorization(authorization string) Option

func BasicAuth

func BasicAuth(user, pass string) Option

func Bearer

func Bearer(bearerToken string) Option

func Client

func Client(client ClientOption) Option

Client 使用自定义的HTTP客户端

func CookieAdd

func CookieAdd(cookies ...*http.Cookie) Option

CookieAdd 添加Cookie到请求

func CookieAddString

func CookieAddString(cookies ...string) Option

CookieAddString 添加Cookie到请求

func CookieEnabled

func CookieEnabled(enabled ...bool) Option

CookieEnabled 开关 Cookie

func CookieSet

func CookieSet(cookieText string) Option

func ErrTry

func ErrTry(tryAt ...time.Duration) Option

func HeaderSet

func HeaderSet(key, value string) Option

HeaderSet 设置请求头

func HeaderSets

func HeaderSets(lines ...string) Option

HeaderSets 设置请求头

func Headers

func Headers(options ...HeaderOption) Option

func Idempotent

func Idempotent(base time.Duration, maxTimes int) Option

Idempotent 幂等重试

func Jar

func Jar(jar http.CookieJar) Option

Jar 设置Cookie容器

func Log

func Log(log Logger) Option

func Options

func Options(options ...Option) Option

func Proxy

func Proxy(proxyUrlString string) Option

Proxy 设置代理

func Referer

func Referer(referer string) Option

Referer 设置请求来源

func UserAgent

func UserAgent(userAgent string) Option

UserAgent 设置用户代理

type PowerDownload

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

func Power

func Power(url, dir, name string) *PowerDownload

func (*PowerDownload) Prepare

func (d *PowerDownload) Prepare(ctx context.Context) error

Prepare 准备下载,检查文件是否存在,是否支持断点续传等

func (*PowerDownload) Start

func (d *PowerDownload) Start(ctx context.Context) error

type Process

type Process = func(resp *http.Response) error // 响应处理器

func CookieRead

func CookieRead(read func(cookies []*http.Cookie) error) Process

CookieRead 从响应读取Cookie

func Dump

func Dump(w io.Writer, reqBody, respBody bool) Process

func HeaderRead

func HeaderRead(read func(headers http.Header) error) Process

func Progress

func Progress(report ProgressReport) Process

Progress 下载进度

func ReadBytes

func ReadBytes(read func(data []byte) error) Process

func ReadHeader

func ReadHeader(read func(headers http.Header) error) Process

func StatusRead

func StatusRead(processStatus func(status int) error) Process

StatusRead 从响应读取状态码并做预处理

type ProgressReport

type ProgressReport func(state ProgressState)

type ProgressState

type ProgressState struct {
	Total   float64 // 总字节数
	Current float64 // 当前已读/写取字节数
	Speed   float64 // 当前读/写速度(字节/秒)
}

type Request

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

Request 请求构造

func Android

func Android() *Request

func Default

func Default() *Request

Default 默认的请求器

func IOS

func IOS() *Request

func Mac

func Mac() *Request

func New

func New(options ...Option) *Request

New 以一些选项开始初始化请求器

func Windows

func Windows() *Request

func (*Request) Body

func (c *Request) Body(body Body) *Request

Body 设置请求提交内容

func (*Request) Bytes

func (c *Request) Bytes(ctx context.Context) (data []byte, err error)

Bytes 处理响应字节

func (*Request) Client

func (c *Request) Client(clientOptions ...ClientOption) *Request

Client 使用自定义的HTTP客户端

func (*Request) Download

func (c *Request) Download(ctx context.Context, fn string, overwrite ...bool) (err error)

Download 下载到文件

func (*Request) ErrTry

func (c *Request) ErrTry(times ...time.Duration) *Request

ErrTry 失败重试,等待休眠时间

func (*Request) Form

func (c *Request) Form(formBody io.Reader) *Request

func (*Request) FormValues

func (c *Request) FormValues(formBody url.Values) *Request

func (*Request) HeaderSet

func (c *Request) HeaderSet(key, value string) *Request

func (*Request) HeaderSets

func (c *Request) HeaderSets(lines ...string) *Request

func (*Request) Log

func (c *Request) Log(log Logger) *Request

func (*Request) Method

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

Method 设置请求方法

func (*Request) Process

func (c *Request) Process(ctx context.Context, process Process, options ...Option) error

Process 处理响应

func (*Request) Referer

func (c *Request) Referer(referer string) *Request

Referer 设置请求来源

func (*Request) RoundTrip

func (c *Request) RoundTrip(transport http.RoundTripper) *Request

RoundTrip 自定义 RoundTripper

func (*Request) Url

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

Url 设置请求链接

func (*Request) Use

func (c *Request) Use(processes ...Process) *Request

Use 添加中间件(Process前置处理)

func (*Request) UserAgent

func (c *Request) UserAgent(userAgent string) *Request

UserAgent 设置用户代理

func (*Request) With

func (c *Request) With(options ...Option) *Request

With 增加选项

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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