nicehttp

package module
v0.0.0-...-0d3d3dd Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2020 License: MIT Imports: 9 Imported by: 27

README

nicehttp

MIT License go.dev reference Discord Chat

Package nicehttp contains helper utilities for downloading files/making requests with valyala/fasthttp.

  • Download a file from a URL serially/in chunks with multiple workers in parallel, should the URL allow it.
  • Download contents of a URL and write its contents to a io.Writer.
  • Query the headers of a URL using a HTTP head request.
  • Follow redirects provisioned by a URL.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(req *fasthttp.Request, res *fasthttp.Response) error

Do sends a HTTP request prescribed in req and populates its results into res. It additionally handles redirects unlike the de-facto Do(req, res) method in fasthttp.

func DoDeadline

func DoDeadline(req *fasthttp.Request, res *fasthttp.Response, deadline time.Time) error

DoDeadline sends a HTTP request prescribed in req and populates its results into res. It additionally handles redirects unlike the de-facto Do(req, res) method in fasthttp. It overrides the default timeout set with a deadline.

func DoTimeout

func DoTimeout(req *fasthttp.Request, res *fasthttp.Response, timeout time.Duration) error

DoTimeout sends a HTTP request prescribed in req and populates its results into res. It additionally handles redirects unlike the de-facto Do(req, res) method in fasthttp. It overrides the default timeout set.

func Download

func Download(w Writer, url string, contentLength int, acceptsRanges bool) error

Download downloads the contents of url and writes its contents to w.

func DownloadBytes

func DownloadBytes(dst []byte, url string) ([]byte, error)

DownloadBytes downloads the contents of url, and returns them as a byte slice.

func DownloadBytesDeadline

func DownloadBytesDeadline(dst []byte, url string, deadline time.Time) ([]byte, error)

DownloadBytesDeadline downloads the contents of url, and returns them as a byte slice.

func DownloadBytesTimeout

func DownloadBytesTimeout(dst []byte, url string, timeout time.Duration) ([]byte, error)

DownloadBytesTimeout downloads the contents of url, and returns them as a byte slice.

func DownloadDeadline

func DownloadDeadline(w Writer, url string, contentLength int, acceptsRanges bool, deadline time.Time) error

DownloadDeadline downloads the contents of url and writes its contents to w.

func DownloadFile

func DownloadFile(filename, url string) error

DownloadFile downloads of url, and writes its contents to a newly-created file titled filename.

func DownloadFileDeadline

func DownloadFileDeadline(filename, url string, deadline time.Time) error

DownloadFileDeadline downloads of url, and writes its contents to a newly-created file titled filename.

func DownloadFileTimeout

func DownloadFileTimeout(filename, url string, timeout time.Duration) error

DownloadFileTimeout downloads of url, and writes its contents to a newly-created file titled filename.

func DownloadInChunks

func DownloadInChunks(w io.WriterAt, url string, length int) error

DownloadInChunks downloads file at url comprised of length bytes in chunks using multiple workers, and stores it in writer w.

func DownloadInChunksDeadline

func DownloadInChunksDeadline(w io.WriterAt, url string, length int, deadline time.Time) error

DownloadInChunksDeadline downloads file at url comprised of length bytes in chunks using multiple workers, and stores it in writer w.

func DownloadInChunksTimeout

func DownloadInChunksTimeout(w io.WriterAt, url string, length int, timeout time.Duration) error

DownloadInChunksTimeout downloads file at url comprised of length bytes in chunks using multiple workers, and stores it in writer w.

func DownloadSerially

func DownloadSerially(w io.Writer, url string) error

DownloadSerially contents of url and writes it to w.

func DownloadSeriallyDeadline

func DownloadSeriallyDeadline(w io.Writer, url string, deadline time.Time) error

DownloadSeriallyDeadline contents of url and writes it to w.

func DownloadSeriallyTimeout

func DownloadSeriallyTimeout(w io.Writer, url string, timeout time.Duration) error

DownloadSeriallyTimeout contents of url and writes it to w.

func DownloadTimeout

func DownloadTimeout(w Writer, url string, contentLength int, acceptsRanges bool, timeout time.Duration) error

DownloadTimeout downloads the contents of url and writes its contents to w.

func QueryHeaders

func QueryHeaders(url string) (contentLength int, acceptsRanges bool)

QueryHeaders learns from url its content length, and if it accepts parallel chunk fetching.

func QueryHeadersDeadline

func QueryHeadersDeadline(url string, deadline time.Time) (contentLength int, acceptsRanges bool)

QueryHeadersDeadline learns from url its content length, and if it accepts parallel chunk fetching.

func QueryHeadersTimeout

func QueryHeadersTimeout(url string, timeout time.Duration) (contentLength int, acceptsRanges bool)

QueryHeadersTimeout learns from url its content length, and if it accepts parallel chunk fetching.

Types

type Client

type Client struct {
	// The underlying instance which nicehttp.Client wraps around.
	Instance Transport

	// Decide whether or not URLs that accept being downloaded in parallel chunks are handled with multiple workers.
	AcceptsRanges bool

	// The number of workers that are to be spawned for downloading chunks in parallel.
	NumWorkers int

	// Size of individual byte chunks downloaded.
	ChunkSize int

	// Max number of redirects to follow before a request is marked to have failed.
	MaxRedirectCount int
}

Client wraps over fasthttp.Client a couple of useful helper functions.

func NewClient

func NewClient() Client

NewClient instantiates a new nicehttp.Client with sane configuration defaults.

func WrapClient

func WrapClient(instance Transport) Client

WrapClient wraps an existing fasthttp.Client or Transport into a nicehttp.Client.

func (*Client) Do

func (c *Client) Do(req *fasthttp.Request, res *fasthttp.Response) error

Do sends a HTTP request prescribed in req and populates its results into res. It additionally handles redirects unlike the de-facto Do(req, res) method in fasthttp.

func (*Client) DoDeadline

func (c *Client) DoDeadline(req *fasthttp.Request, res *fasthttp.Response, deadline time.Time) error

DoDeadline sends a HTTP request prescribed in req and populates its results into res. It additionally handles redirects unlike the de-facto Do(req, res) method in fasthttp. It overrides the default timeout set with a deadline.

func (*Client) DoTimeout

func (c *Client) DoTimeout(req *fasthttp.Request, res *fasthttp.Response, timeout time.Duration) error

DoTimeout sends a HTTP request prescribed in req and populates its results into res. It additionally handles redirects unlike the de-facto Do(req, res) method in fasthttp. It overrides the default timeout set.

func (*Client) Download

func (c *Client) Download(w Writer, url string, contentLength int, acceptsRanges bool) error

Download downloads the contents of url and writes its contents to w.

func (*Client) DownloadBytes

func (c *Client) DownloadBytes(dst []byte, url string) ([]byte, error)

DownloadBytes downloads the contents of url, and returns them as a byte slice.

func (*Client) DownloadBytesDeadline

func (c *Client) DownloadBytesDeadline(dst []byte, url string, deadline time.Time) ([]byte, error)

DownloadBytesDeadline downloads the contents of url, and returns them as a byte slice.

func (*Client) DownloadBytesTimeout

func (c *Client) DownloadBytesTimeout(dst []byte, url string, timeout time.Duration) ([]byte, error)

DownloadBytesTimeout downloads the contents of url, and returns them as a byte slice.

func (*Client) DownloadDeadline

func (c *Client) DownloadDeadline(w Writer, url string, contentLength int, acceptsRanges bool, deadline time.Time) error

DownloadDeadline downloads the contents of url and writes its contents to w.

func (*Client) DownloadFile

func (c *Client) DownloadFile(filename, url string) error

DownloadFile downloads the contents of url, and writes its contents to a newly-created file titled filename.

func (*Client) DownloadFileDeadline

func (c *Client) DownloadFileDeadline(filename, url string, deadline time.Time) error

DownloadFileDeadline downloads the contents of url, and writes its contents to a newly-created file titled filename.

func (*Client) DownloadFileTimeout

func (c *Client) DownloadFileTimeout(filename, url string, timeout time.Duration) error

DownloadFileTimeout downloads the contents of url, and writes its contents to a newly-created file titled filename.

func (*Client) DownloadInChunks

func (c *Client) DownloadInChunks(f io.WriterAt, url string, length int) error

DownloadInChunks downloads file at url comprised of length bytes in chunks using multiple workers, and stores it in writer w.

func (*Client) DownloadInChunksDeadline

func (c *Client) DownloadInChunksDeadline(f io.WriterAt, url string, length int, deadline time.Time) error

DownloadInChunksDeadline downloads file at url comprised of length bytes in chunks using multiple workers, and stores it in writer w.

func (*Client) DownloadInChunksTimeout

func (c *Client) DownloadInChunksTimeout(f io.WriterAt, url string, length int, timeout time.Duration) error

DownloadInChunksTimeout downloads file at url comprised of length bytes in chunks using multiple workers, and stores it in writer w.

func (*Client) DownloadSerially

func (c *Client) DownloadSerially(w io.Writer, url string) error

DownloadSerially serially downloads the contents of url and writes it to w.

func (*Client) DownloadSeriallyDeadline

func (c *Client) DownloadSeriallyDeadline(w io.Writer, url string, deadline time.Time) error

DownloadSeriallyDeadline serially downloads the contents of url and writes it to w.

func (*Client) DownloadSeriallyTimeout

func (c *Client) DownloadSeriallyTimeout(w io.Writer, url string, timeout time.Duration) error

DownloadSeriallyTimeout serially downloads the contents of url and writes it to w.

func (*Client) DownloadTimeout

func (c *Client) DownloadTimeout(w Writer, url string, contentLength int, acceptsRanges bool, timeout time.Duration) error

DownloadTimeout downloads the contents of url and writes its contents to w.

func (*Client) QueryHeaders

func (c *Client) QueryHeaders(url string) (contentLength int, acceptsRanges bool)

QueryHeaders learns from url its content length, and if it accepts parallel chunk fetching.

func (*Client) QueryHeadersDeadline

func (c *Client) QueryHeadersDeadline(url string, deadline time.Time) (contentLength int, acceptsRanges bool)

QueryHeadersDeadline learns from url its content length, and if it accepts parallel chunk fetching.

func (*Client) QueryHeadersTimeout

func (c *Client) QueryHeadersTimeout(url string, timeout time.Duration) (contentLength int, acceptsRanges bool)

QueryHeadersTimeout learns from url its content length, and if it accepts parallel chunk fetching.

type Transport

type Transport interface {
	Do(req *fasthttp.Request, res *fasthttp.Response) error
	DoTimeout(req *fasthttp.Request, res *fasthttp.Response, timeout time.Duration) error
	DoDeadline(req *fasthttp.Request, res *fasthttp.Response, deadline time.Time) error
}

Transport represents the interface of a HTTP client supported by nicehttp.

type WriteBuffer

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

WriteBuffer implements io.Writer and io.WriterAt on an optionally-provided byte slice.

func NewWriteBuffer

func NewWriteBuffer(dst []byte) *WriteBuffer

NewWriteBuffer instantiates a new write buffer around dst. dst may be nil.

func (*WriteBuffer) Bytes

func (b *WriteBuffer) Bytes() []byte

Bytes returns the underlying byte slice.

func (*WriteBuffer) Write

func (b *WriteBuffer) Write(p []byte) (int, error)

Write implements io.Writer.

func (*WriteBuffer) WriteAt

func (b *WriteBuffer) WriteAt(p []byte, off int64) (int, error)

WriteAt implements io.WriterAt.

type Writer

type Writer interface {
	io.Writer
	io.WriterAt
}

Writer implements io.Writer and io.WriterAt.

type WriterAtOffset

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

WriterAtOffset implements io.Writer for a given io.WriterAt at an offset.

func NewWriterAtOffset

func NewWriterAtOffset(dst io.WriterAt, offset int64) *WriterAtOffset

NewWriterAtOffset instantiates a new writer at a specified offset.

func (WriterAtOffset) Write

func (w WriterAtOffset) Write(b []byte) (int, error)

Write implements io.Writer.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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