r2

package
v0.0.0-...-bc49051 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureHTTPTransport

func EnsureHTTPTransport(r *Request) (typed *http.Transport, err error)

EnsureHTTPTransport ensures the http client's transport is set and that it is an *http.Transport.

It will return an error `ErrInvalidTransport` if it is set to something other than *http.Transport.

func ErrIsTooManyRedirects

func ErrIsTooManyRedirects(err error) bool

ErrIsTooManyRedirects returns if the error is too many redirects.

func HostHasPort

func HostHasPort(s string) bool

HostHasPort returns true if a string is in the form "host:port", or "[ipv6::address]:port".

func RemoveHostEmptyPort

func RemoveHostEmptyPort(host string) string

RemoveHostEmptyPort strips the empty port in ":port" to "" as mandated by RFC 3986 Section 6.2.3.

Types

type Error

type Error string

Error is an error constant.

const (
	// ErrRequestUnset is an error returned from options if they are called on a r2.Request that has not
	// been created by r2.New(), and as a result the underlying request is uninitialized.
	ErrRequestUnset Error = "r2; cannot modify request, underlying request unset. please use r2.New()"
	// ErrInvalidTransport is an error returned from options if they are called on a request that has had
	// the transport set to something other than an *http.Transport; this precludes using http.Transport
	// specific options like tls.Config mutators.
	ErrInvalidTransport Error = "r2; cannot modify transport, is not *http.Transport"
	// ErrNoContentJSON is returns from sending requests when a no-content status is returned.
	ErrNoContentJSON Error = "r2; server returned an http 204 for a request expecting json"
	// ErrNoContentGob is returns from sending requests when a no-content status is returned.
	ErrNoContentGob Error = "r2; server returned an http 204 for a request expecting a gob encoded response"
	// ErrInvalidMethod is an error that is returned from `r2.Request.Do()` if a method
	// is specified on the request that violates the valid charset for HTTP methods.
	ErrInvalidMethod Error = "r2; invalid http method"
	// ErrMismatchedPathParameters is an error that is returned from `OptParameterizedPath()` if
	// the parameterized path string has a different number of parameters than what was passed as
	// variadic arguments.
	ErrMismatchedPathParameters Error = "r2; route parameters provided don't match parameters needed in path"
	// ErrFormAndBodySet is an error where the caller has provided both a body and post form values.
	ErrFormAndBodySet Error = "r2; post form and body are both set"
)

Error Constants

func (Error) Error

func (e Error) Error() string

Error implements error.

type Option

type Option func(*Request) error

Option is a modifier for a request.

func OptBasicAuth

func OptBasicAuth(username, password string) Option

OptBasicAuth is an option that sets the http basic auth.

func OptBody

func OptBody(body []byte) Option

OptBody sets a body on a context from bytes.

func OptBodyGob

func OptBodyGob(obj interface{}) Option

OptBodyGob sets the post body on the request to a given value encoded with gob.

func OptBodyJSON

func OptBodyJSON(obj interface{}) Option

OptBodyJSON sets the post body on the request to a given value encoded with json.

func OptBodyReader

func OptBodyReader(contents io.ReadCloser) Option

OptBodyReader sets the post body on the request.

func OptCheckRedirect

func OptCheckRedirect(checkfn func(r *http.Request, via []*http.Request) error) Option

OptCheckRedirect sets the client check redirect function.

func OptClientTimeout

func OptClientTimeout(d time.Duration) Option

OptClientTimeout sets the client timeout.

func OptCloser

func OptCloser(closefn func() error) Option

OptCloser sets the closer function on the request which is called once the request completes.

It is useful for closing underlying streams if they need to be re-used multiple times.

func OptContext

func OptContext(ctx context.Context) Option

OptContext sets the request context.

func OptCookie

func OptCookie(cookie *http.Cookie) Option

OptCookie adds a cookie to a context.

func OptCookieValue

func OptCookieValue(key, value string) Option

OptCookieValue adds a cookie value to a context.

func OptDelete

func OptDelete() Option

OptDelete sets the request method.

func OptDialer

func OptDialer(dialer net.Dialer) Option

OptDialer sets the dialer for the transport.

func OptDisableKeepAlives

func OptDisableKeepAlives(disableKeepAlives bool) Option

OptDisableKeepAlives disables keep alives.

func OptErr

func OptErr(err error) Option

OptErr sets the request error pre-emptively.

This should only be used for debugging or testing purposes as it will prevent requests from being sent.

func OptGet

func OptGet() Option

OptGet sets the request method.

func OptHeader

func OptHeader(key, value string) Option

OptHeader is an alias to `r2.OptHeaderSet` and sets a header on the request by key and value.

func OptHeaderAdd

func OptHeaderAdd(key, value string) Option

OptHeaderAdd adds a header value on a request.

func OptHeaderDel

func OptHeaderDel(key string) Option

OptHeaderDel deletes a header by key on a request.

func OptHeaderSet

func OptHeaderSet(key, value string) Option

OptHeaderSet sets a header value on a request.

func OptHeaders

func OptHeaders(headers http.Header) Option

OptHeaders sets the request headers.

func OptHost

func OptHost(host string) Option

OptHost sets the url host of a request.

func OptMaxRedirects

func OptMaxRedirects(maxRedirects int) Option

OptMaxRedirects tells the http client to only follow a given number of redirects, overriding the standard library default of 10. Use the companion helper `ErrIsTooManyRedirects` to test if the returned error from a call indicates the redirect limit was reached.

func OptMethod

func OptMethod(method string) Option

OptMethod sets the request method.

func OptOnRequest

func OptOnRequest(listener func(*http.Request) error) Option

OptOnRequest sets an on request listener.

func OptOnResponse

func OptOnResponse(listener func(*http.Request, *http.Response, time.Time, error) error) Option

OptOnResponse adds an on response listener. If an OnResponse listener has already been addded, it will be merged with the existing listener.

func OptOptions

func OptOptions() Option

OptOptions sets the request method.

func OptPatch

func OptPatch() Option

OptPatch sets the request method.

func OptPath

func OptPath(path string) Option

OptPath sets the url path of a request.

func OptPathf

func OptPathf(format string, args ...any) Option

OptPathf sets the url path of a request.

func OptPost

func OptPost() Option

OptPost sets the request method.

func OptPut

func OptPut() Option

OptPut sets the request method.

func OptQuery

func OptQuery(key, value string) Option

OptQuery is an alias to `r2.OptQuerySet` and sets a query string value on the request url by key and value.

func OptQueryAdd

func OptQueryAdd(key, value string) Option

OptQueryAdd adds a header value on a request.

func OptQueryDel

func OptQueryDel(key string) Option

OptQueryDel deletes a header by key on a request.

func OptQuerySet

func OptQuerySet(key, value string) Option

OptQuerySet sets a header value on a request.

func OptRawQuery

func OptRawQuery(rawQuery string) Option

OptRawQuery sets the request url query string.

func OptRawQueryValues

func OptRawQueryValues(q url.Values) Option

OptRawQueryValues sets the request url query string.

func OptRequest

func OptRequest(req *http.Request) Option

OptRequest sets the raw underlying request.

This should only be used in testing or debugging situations.

func OptScheme

func OptScheme(scheme string) Option

OptScheme sets the url scheme of a request.

func OptTLSClientCert

func OptTLSClientCert(cert tls.Certificate) Option

OptTLSClientCert adds a client certificate to the request.

func OptTLSClientConfig

func OptTLSClientConfig(cfg *tls.Config) Option

OptTLSClientConfig sets the tls config for the request. It will create a client, and a transport if unset.

func OptTLSHandshakeTimeout

func OptTLSHandshakeTimeout(d time.Duration) Option

OptTLSHandshakeTimeout sets the client transport TLSHandshakeTimeout.

func OptTLSInsecureSkipVerify

func OptTLSInsecureSkipVerify(insecureSkipVerify bool) Option

OptTLSInsecureSkipVerify sets if we should skip verification.

func OptTLSRootCAs

func OptTLSRootCAs(pool *x509.CertPool) Option

OptTLSRootCAs sets the client tls root ca pool.

func OptTransport

func OptTransport(transport http.RoundTripper) Option

OptTransport sets the client transport for a request.

func OptURL

func OptURL(u *url.URL) Option

OptURL sets the underlying request url directly.

func OptURLParsed

func OptURLParsed(rawURL string) Option

OptURLParsed sets the url of a request by parsing the given raw url.

func OptUserAgent

func OptUserAgent(userAgent string) Option

OptUserAgent sets the user agent header on a request. It will initialize the request headers map if it's unset. It will overwrite any existing user agent header.

type Request

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

Request is a combination of the http.Request options and the underlying client.

func New

func New(remoteURL string, options ...Option) *Request

New returns a new request.

The default method is GET, the default Proto is `HTTP/1.1`.

func (Request) Bytes

func (r Request) Bytes() (contents []byte, res *http.Response, err error)

Bytes reads the response and returns it as a byte array, along with the response metadata..

func (*Request) Close

func (r *Request) Close() error

Close closes the request if there is a closer specified.

func (Request) CopyTo

func (r Request) CopyTo(dst io.Writer) (count int64, err error)

CopyTo copies the response body to a given writer.

func (Request) Discard

func (r Request) Discard() (res *http.Response, err error)

Discard reads the response fully and discards all data it reads, and returns the response metadata.

func (Request) Do

func (r Request) Do() (*http.Response, error)

Do executes the request.

func (Request) Gob

func (r Request) Gob(dst interface{}) (res *http.Response, err error)

Gob reads the response as gob into a given object and returns the response metadata.

func (Request) JSON

func (r Request) JSON(dst interface{}) (res *http.Response, err error)

JSON reads the response as json into a given object and returns the response metadata.

func (*Request) Method

func (r *Request) Method() string

Method returns the request method.

func (*Request) URL

func (r *Request) URL() *url.URL

URL returns the request url.

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) *Request

WithContext implements the `WithContext` method for the underlying request.

It is preserved here because the pointer indirects are non-trivial.

Jump to

Keyboard shortcuts

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