gohttp

package module
v0.0.0-...-1b86048 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: MIT Imports: 22 Imported by: 0

README

用法示例

gohttp 实现的类似wget/curl多线程下载

package main

import (
	"fmt"
	"github.com/deweizhu/gohttp"
	"log"
)

func exampleGet() {
	ctx := context.Background()
	cli := gohttp.NewClient(ctx)
	resp, err := cli.Get("https://cn.bing.com/search", gohttp.Options{
		Query: "q=bookget&form=gohttp",
	})
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("%s", resp.GetRequest().URL.RawQuery)
}

func examplePostWithQuery() {
	ctx := context.Background()
	cli := gohttp.NewClient(ctx)
	resp, err := cli.Post("http://127.0.0.1:5000/", gohttp.Options{
		Headers: map[string]interface{}{
			"Content-Type": "application/json",
			"User-Agent":   "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0",
			"token":        "xxxx-xxxx-xxxx-xxxx",
		},
		JSON: struct {
			Key1 string   `json:"name"`
			Key2 []string `json:"data"`
			Key3 int      `json:"page"`
		}{"name", []string{"data1", "data2"}, 100},
	})
	if err != nil {
		log.Fatalln(err)
	}

	body, _ := resp.GetBody()
	fmt.Println(body)
}

func exampleProxy() {
	ctx := context.Background()
	cli := gohttp.NewClient(ctx)
	resp, err := cli.Get("https://ip.cn/api/index?ip=&type=0", gohttp.Options{
		Timeout: 5.0,
		Proxy:   "http://127.0.0.1:4000",
	})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(resp.GetStatusCode())
}

//gohttp 实现的类似wget/curl多线程下载
func exampleDownloader() {
	sUrl := "https://dl.google.com/go/go1.18.4.windows-amd64.msi"
	dest := "d:\\downloads\\go1.18.4.windows-amd64.msi"
	ctx := context.Background()
	cli := gohttp.NewClient(ctx)
	resp, err := cli.FastGet(sUrl,
		gohttp.Options{
			Timeout: 0,
			//Concurrency: 4,
			Headers: map[string]interface{}{
				"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0",
			},
			DestFile: dest})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("%T", resp)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFileName = "gohttp.output"

DefaultFileName is the fallback name for GetFilename.

Functions

func ByteUnitString

func ByteUnitString(n int64) string

Types

type Chunk

type Chunk struct {
	Start, End uint64
}

Chunk represents the partial content range

type Download

type Download struct {
	Cookie                                          []http.Cookie
	Concurrency                                     uint
	StopProgress                                    bool
	URL, Dir, Dest                                  string
	Interval, ChunkSize, MinChunkSize, MaxChunkSize uint64
	// contains filtered or unexported fields
}

Download holds downloadable file config and infos.

func (*Download) AvgSpeed

func (d *Download) AvgSpeed() uint64

AvgSpeed returns average download speed.

func (*Download) ChunkInit

func (d *Download) ChunkInit() (err error)

Init set defaults and split file into chunks and gets Info, you should call Init before Start

func (*Download) ChunkStart

func (d *Download) ChunkStart() (err error)

Start downloads the file chunks, and merges them. Must be called only after init

func (*Download) DownloadChunk

func (d *Download) DownloadChunk(c *Chunk, dest io.Writer) error

DownloadChunk downloads a file chunk.

func (*Download) GetInfoOrDownload

func (d *Download) GetInfoOrDownload() (*Info, error)

Try downloading the first byte of the file using a range request. If the server supports range requests, then we'll extract the length info from content-range, Otherwise this just downloads the whole file in one go

func (*Download) IsRangeable

func (d *Download) IsRangeable() bool

IsRangeable returns file server partial content support state.

func (*Download) Path

func (d *Download) Path() string

Return constant path which will not change once the download starts

func (*Download) RunProgress

func (d *Download) RunProgress()

RunProgress runs ProgressFunc based on Interval and updates lastSize.

func (*Download) Size

func (d *Download) Size() uint64

Size returns downloaded size.

func (*Download) Speed

func (d *Download) Speed() uint64

Speed returns download speed.

func (*Download) TotalCost

func (d *Download) TotalCost() time.Duration

TotalCost returns download duration.

func (*Download) TotalSize

func (d *Download) TotalSize() uint64

TotalSize returns file total size (0 if unknown).

func (*Download) Write

func (d *Download) Write(b []byte) (int, error)

Write updates progress size.

type Info

type Info struct {
	Size      uint64
	Rangeable bool
}

Info holds downloadable file info.

type OffsetWriter

type OffsetWriter struct {
	io.WriterAt
	// contains filtered or unexported fields
}

func (*OffsetWriter) Write

func (dst *OffsetWriter) Write(b []byte) (n int, err error)

type Options

type Options struct {
	Debug       bool
	Concurrency uint //CPU核数
	BaseURI     string
	Timeout     float32

	Query      interface{}
	Headers    map[string]interface{}
	Cookies    interface{}
	CookieFile string
	CookieJar  *cookiejar.Jar
	FormParams map[string]interface{}
	JSON       interface{}
	Body       []byte
	XML        interface{}
	Proxy      string
	DestFile   string //保存到本地文件
	Overwrite  bool   //覆蓋文件
	// contains filtered or unexported fields
}

Options object

type Request

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

Request object

func NewClient

func NewClient(c context.Context, opts ...Options) *Request

NewClient new request object

func (*Request) Delete

func (r *Request) Delete(uri string, opts ...Options) (*Response, error)

Delete send delete request

func (*Request) FastGet

func (r *Request) FastGet(uri string, opts ...Options) (resp *Response, err error)

func (*Request) Get

func (r *Request) Get(uri string, opts ...Options) (*Response, error)

Get send get request

func (*Request) Options

func (r *Request) Options(uri string, opts ...Options) (*Response, error)

Options send options request

func (*Request) Patch

func (r *Request) Patch(uri string, opts ...Options) (*Response, error)

Patch send patch request

func (*Request) Post

func (r *Request) Post(uri string, opts ...Options) (*Response, error)

Post send post request

func (*Request) Put

func (r *Request) Put(uri string, opts ...Options) (*Response, error)

Put send put request

func (*Request) Request

func (r *Request) Request(method, uri string, opts ...Options) (*Request, error)

Request send request

type Response

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

Response response object

func Delete

func Delete(c context.Context, uri string, opts ...Options) (*Response, error)

Delete send delete request

func FastGet

func FastGet(c context.Context, uri string, opts ...Options) (*Response, error)

Download file

func Get

func Get(c context.Context, uri string, opts ...Options) (*Response, error)

Get send get request

func Patch

func Patch(c context.Context, uri string, opts ...Options) (*Response, error)

Patch send patch request

func Post

func Post(c context.Context, uri string, opts ...Options) (*Response, error)

Post send post request

func Put

func Put(c context.Context, uri string, opts ...Options) (*Response, error)

Put send put request

func (*Response) GetBody

func (r *Response) GetBody() (ResponseBody, error)

GetBody parse response body

func (*Response) GetCookies

func (r *Response) GetCookies() []*http.Cookie

Cookies get if header exsits in response headers

func (*Response) GetHeader

func (r *Response) GetHeader(name string) []string

GetHeader get response header

func (*Response) GetHeaderLine

func (r *Response) GetHeaderLine(name string) string

GetHeaderLine get a single response header

func (*Response) GetHeaders

func (r *Response) GetHeaders() map[string][]string

GetHeaders get response headers

func (*Response) GetJsonDecodeBody

func (r *Response) GetJsonDecodeBody(body interface{}) (err error)

GetParsedBody parse response body

func (*Response) GetReasonPhrase

func (r *Response) GetReasonPhrase() string

GetReasonPhrase get response reason phrase

func (*Response) GetRequest

func (r *Response) GetRequest() *http.Request

GetRequest get request object

func (*Response) GetStatusCode

func (r *Response) GetStatusCode() int

GetStatusCode get response status code

func (*Response) HasHeader

func (r *Response) HasHeader(name string) bool

HasHeader get if header exsits in response headers

func (*Response) IsTimeout

func (r *Response) IsTimeout() bool

IsTimeout get if request is timeout

type ResponseBody

type ResponseBody []byte

ResponseBody response body

func (ResponseBody) GetContents

func (r ResponseBody) GetContents() string

GetContents format response body as string

func (ResponseBody) Read

func (r ResponseBody) Read(length int) []byte

Read get slice of response body

func (ResponseBody) String

func (r ResponseBody) String() string

String fmt outout

Jump to

Keyboard shortcuts

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