httpio

package
v0.0.0-...-330397d Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderContentType  = "Content-Type"
	MimeOctetStream    = "application/octet-stream"
	MimeFormUrlEncoded = "application/x-www-form-urlencoded"
)

Variables

This section is empty.

Functions

func BuildClient

func BuildClient(opts ...ClientOption) (*http.Client, error)

func BuildRequest

func BuildRequest(options ...RequestOption) (req *http.Request, err error)

BuildRequest build request

func BuildTransport

func BuildTransport() http.RoundTripper

func ProcessResponse

func ProcessResponse(resp *http.Response, process ResponseProcess, middlewares ...ResponseMiddleware) (err error)

ProcessResponse 处理响应

Types

type ClientOption

type ClientOption func(*http.Client)

type ProgressState

type ProgressState struct {
	Total   int64
	Current int64
	StartAt time.Time
	EndAt   time.Time
	Speed   float64
}

func (ProgressState) AvSpeed

func (p ProgressState) AvSpeed() float64

func (ProgressState) Completed

func (p ProgressState) Completed() bool

func (ProgressState) Elapsed

func (p ProgressState) Elapsed() time.Duration

func (ProgressState) Percent

func (p ProgressState) Percent() float64

Percent 返回百分比, 0-100, 精度2位小数,如果 total 为 0 则返回 -1

func (ProgressState) String

func (p ProgressState) String() string

type Request

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

func New

func New(url string, opts ...RequestOption) *Request

func (*Request) Do

func (r *Request) Do(ctx context.Context, process ResponseProcess) (err error)

func (*Request) Use

func (r *Request) Use(middlewares ...ResponseMiddleware) *Request

func (*Request) With

func (r *Request) With(opts ...RequestOption) *Request

type RequestOption

type RequestOption func(*RequestOptions)

RequestOption request option

func File

func File(fn string) RequestOption

func Form

func Form(data url.Values) RequestOption

func FormData

func FormData(data url.Values, files map[string]string) RequestOption

func HeaderAdd

func HeaderAdd(k, v string) RequestOption

func HeaderDel

func HeaderDel(k string) RequestOption

func HeaderSet

func HeaderSet(k, v string) RequestOption

func Headers

func Headers(hdrs ...string) RequestOption

type RequestOptions

type RequestOptions struct {
	URL        string
	Method     string
	CreateBody func() (body io.Reader, contentType string, err error)
	Headers    []string
}

RequestOptions request options

type ResponseMiddleware

type ResponseMiddleware func(next ResponseProcess) ResponseProcess

ResponseMiddleware 响应中间件

func Progress

func Progress(report func(p ProgressState)) ResponseMiddleware

Progress report progress

type ResponseProcess

type ResponseProcess func(resp *http.Response) (err error)

ResponseProcess 响应处理

func Download

func Download(filePath string, saveOptions ...fileio.SaveOption) ResponseProcess

Download write response body to file

func JSON

func JSON(out any) ResponseProcess

JSON parse response body to json

func StatusOK

func StatusOK(next ResponseProcess) ResponseProcess

StatusOK check response status code

func WriteTo

func WriteTo(w io.Writer) ResponseProcess

WriteTo write response body to writer

Jump to

Keyboard shortcuts

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