protocol

package
v0.0.0-...-66343a0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: BSD-3-Clause, BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(method, url string, handler ResponseHandler, mod ...Modifier) error

Do performs an http request by applying the specified options.

method is a string, generally one of the http.Method.* constants, indicating the HTTP method to use. url is the remote url to retrieve, as a string. mod is a set of modifiers indicating how to perform the request.

func Get

func Get(url string, handler ResponseHandler, mod ...Modifier) error

Get will perform a GET request to retrieve the specified url by invoking the Do method.

func Post

func Post(url string, handler ResponseHandler, mod ...Modifier) error

Post will perform a POST request to retrieve the specified url by invoking the Do method.

func StatusOK

func StatusOK(code int, message string) error

Types

type CallbackWriter

type CallbackWriter struct {
	bytes.Buffer
	// contains filtered or unexported fields
}

func NewCallbackWriter

func NewCallbackWriter(cb func([]byte) error) *CallbackWriter

func (*CallbackWriter) Close

func (bw *CallbackWriter) Close() error

type ChainWriter

type ChainWriter []io.WriteCloser

func (ChainWriter) Close

func (cw ChainWriter) Close() error

func (ChainWriter) Write

func (cw ChainWriter) Write(data []byte) (int, error)

type Cleaner

type Cleaner func()

type Cleaners

type Cleaners []Cleaner

func (Cleaners) Run

func (c Cleaners) Run()

type CloseCallbackWriter

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

func (CloseCallbackWriter) Close

func (cb CloseCallbackWriter) Close() error

func (CloseCallbackWriter) Write

func (cb CloseCallbackWriter) Write(data []byte) (int, error)

type HTTPError

type HTTPError struct {
	URL  string
	Resp *http.Response
	// contains filtered or unexported fields
}

Returned by Read operations to provide details on the failed operation, by wrapping an existing error.

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) Unwrap

func (e *HTTPError) Unwrap() error

type Modifier

type Modifier func(o *Options) error

func WithCleaner

func WithCleaner(cleaner Cleaner) Modifier

func WithClientOptions

func WithClientOptions(mods ...kclient.Modifier) Modifier

func WithContent

func WithContent(content io.Reader) Modifier

WithContent allows to provide a body to be sent to the remote end.

For example, use WithContent(strings.NewReader("this is some json")) to send some json alongside your POST requests.

func WithContext

func WithContext(ctx context.Context) Modifier

func WithOptions

func WithOptions(mods ...Modifier) Modifier

func WithRequestOptions

func WithRequestOptions(mods ...krequest.Modifier) Modifier

func WithTimeout

func WithTimeout(timeout time.Duration) Modifier

type Modifiers

type Modifiers []Modifier

func (Modifiers) Apply

func (mods Modifiers) Apply(o *Options) error

type Options

type Options struct {
	Url    string
	Method string

	Handler ResponseHandler
	Cleaner []Cleaner

	Client  *http.Client
	Ctx     context.Context
	Timeout time.Duration
	Content io.Reader

	ClientMods  kclient.Modifiers
	RequestMods krequest.Modifiers
}

func (*Options) Do

func (options *Options) Do() error

type ResponseHandler

type ResponseHandler func(url string, resp *http.Response, err error) error

func Read

func Read(opener ResponseOpener, checker ...StatusChecker) ResponseHandler

func Reader

func Reader(r ResponseReader, checker ...StatusChecker) ResponseHandler

type ResponseOpener

type ResponseOpener func(resp *http.Response) (io.WriteCloser, error)

Opener is a function able to open an output stream.

The size argument indicates how many bytes are expected in this stream. The parameter cannot be trusted, as the remote server is allowed to return any value. 0 indicates the amount of bytes is unknown.

Opener returns an io.WriteCloser(), usable to write the data out, or an error.

func Buffer

func Buffer(dest *[]byte) ResponseOpener

Buffer returns an opener able to write all the data in a []byte array.

func Callback

func Callback(cb func([]byte) error) ResponseOpener

Callback will invoke the specified callback once the write has completed.

func Chain

func Chain(openers ...ResponseOpener) ResponseOpener

func File

func File(path string) ResponseOpener

File returns an Opener able to write to a file. path if the path of the file.

Note that the file will not be opened until the Opener is actually invoked, at which point an error may be returned.

func Limit

func Limit(max int64, nested ResponseOpener) ResponseOpener

Limit returns an opener that will limit the total amount of bytes written to the max specified.

For example, by creating an opener like:

Get(..., Limit(4096, String(&data)), ...)

Get will stop the download and return an error if more than 4096 bytes of data are returned by the remote server.

func Null

func Null() ResponseOpener

Null is an Opener that will discard all the data that it is passed.

func OnClose

func OnClose(cb func(*http.Response) error) ResponseOpener

OnClose will invoke the specified callback on close only.

func String

func String(dest *string) ResponseOpener

String returns an opener able to store data in a string.

func WriteCloser

func WriteCloser(wc io.WriteCloser) ResponseOpener

WriteCloser allows to pass a simple io.WriteCloser without creating an Opener.

func Writer

func Writer(wc io.Writer) ResponseOpener

Writer allows to pass a simple io.Writer without creating an Opener.

func WriterCreator

func WriterCreator(creator func() io.Writer) ResponseOpener

type ResponseReader

type ResponseReader func(r io.Reader) error

type StatusChecker

type StatusChecker func(code int, message string) error

func StatusRange

func StatusRange(min, max int) StatusChecker

func StatusValue

func StatusValue(expected int) StatusChecker

type StatusCheckers

type StatusCheckers []StatusChecker

func (StatusCheckers) Run

func (sc StatusCheckers) Run(url string, resp *http.Response) error

Jump to

Keyboard shortcuts

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