httpf

package
v0.11.5 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2022 License: MIT Imports: 13 Imported by: 3

Documentation

Overview

Package httpf provides HTTP request utilities similar to github.com/carlmjohnson/requests but with support of github.com/carlmjohnson/flu IO capabilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDefaultTransport added in v0.10.5

func NewDefaultTransport() *http.Transport

NewDefaultTransport creates a new http.Transport based on http.DefaultTransport.

Types

type Authorization

type Authorization interface {
	SetAuth(req *http.Request)
}

Authorization is used to set Authorization headers in requests.

func Basic

func Basic(username, password string) Authorization

Basic returns Basic Authorization header.

func Bearer

func Bearer(token string) Authorization

Bearer returns Bearer Authorization header.

type Catch added in v0.10.5

type Catch interface {
	Handle(resp *http.Response, err error) error
}

Catch handles exchange error.

type CatchFunc added in v0.10.5

type CatchFunc func(resp *http.Response, err error) error

CatchFunc is Catch functional adapter.

func (CatchFunc) Handle added in v0.10.5

func (f CatchFunc) Handle(resp *http.Response, err error) error

type Client

type Client interface {
	Do(req *http.Request) (*http.Response, error)
}

Client is a basic HTTP request executor.

type ContentType

type ContentType interface {
	ContentType() string
}

ContentType is an interface which may be optionally implemented by flu.EncoderTo implementations to automatically set Content-Type header in requests.

type ContentTypeError

type ContentTypeError string

ContentTypeError is returned when response Content-Type does not match the expected one.

func (ContentTypeError) Error

func (e ContentTypeError) Error() string

type ExchangeResult added in v0.10.5

type ExchangeResult struct {
	*http.Response
	// contains filtered or unexported fields
}

ExchangeResult is a fluent response wrapper.

func ExchangeError added in v0.10.5

func ExchangeError(err error) *ExchangeResult

ExchangeError returns an ExchangeResult stub with error set.

func (*ExchangeResult) Catch added in v0.10.5

func (r *ExchangeResult) Catch(catch Catch) *ExchangeResult

Catch processes and overwrites the error if it occurred.

func (*ExchangeResult) CatchFunc added in v0.10.5

func (r *ExchangeResult) CatchFunc(catch CatchFunc) *ExchangeResult

CatchFunc is like Catch, but accepts functions.

func (*ExchangeResult) CheckContentType added in v0.10.5

func (r *ExchangeResult) CheckContentType(value string) *ExchangeResult

CheckContentType checks the response Content-Type and returns error if it does not match the value. It closes response body on failure.

func (*ExchangeResult) CheckStatus added in v0.10.5

func (r *ExchangeResult) CheckStatus(codes ...int) *ExchangeResult

CheckStatus checks the response status code and sets the error to StatusCodeError if there is no match. It closes response body on failure.

func (*ExchangeResult) CopyBody added in v0.11.0

func (r *ExchangeResult) CopyBody(out flu.Output) *ExchangeResult

CopyBody copies response body to flu.Output.

func (*ExchangeResult) DecodeBody added in v0.10.5

func (r *ExchangeResult) DecodeBody(decoder flu.DecoderFrom) *ExchangeResult

DecodeBody decodes response body using the provided decoder.

func (*ExchangeResult) Error added in v0.10.5

func (r *ExchangeResult) Error() error

Error terminates the exchange, closes response body if necessary, and returns an error if any.

func (*ExchangeResult) Handle added in v0.10.5

func (r *ExchangeResult) Handle(handler ResponseHandler) *ExchangeResult

Handle executes a ResponseHandler if no previous handling errors occurred.

func (*ExchangeResult) HandleFunc added in v0.10.5

func (r *ExchangeResult) HandleFunc(handler ResponseHandlerFunc) *ExchangeResult

HandleFunc is like Handle, but accepts functions.

func (*ExchangeResult) Reader added in v0.10.5

func (r *ExchangeResult) Reader() (io.Reader, error)

Reader implements flu.Input interface.

type Form

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

Form represents a HTTP form request body.

func FormValue added in v0.10.5

func FormValue(value interface{}) *Form

FormValue creates a Form based on this value. It uses `url` tags for property name resolution.

func (*Form) ContentType

func (*Form) ContentType() string

func (*Form) EncodeTo

func (f *Form) EncodeTo(writer io.Writer) error

func (*Form) Multipart

func (f *Form) Multipart() *MultipartForm

Multipart creates a MultipartForm using this Form as property base.

func (*Form) Set

func (f *Form) Set(key string, values ...string) *Form

Set sets values to the key.

func (*Form) SetAll added in v0.10.5

func (f *Form) SetAll(values url.Values) *Form

SetAll adds all values from url.Values.

type MultipartForm

type MultipartForm struct {
	// Form contains form values (except for files).
	Form
	// contains filtered or unexported fields
}

MultipartForm represents a multipart/form-data request body.

func (*MultipartForm) ContentType

func (mf *MultipartForm) ContentType() string

func (*MultipartForm) EncodeTo

func (mf *MultipartForm) EncodeTo(w io.Writer) error

func (*MultipartForm) File

func (mf *MultipartForm) File(fieldName, filename string, input flu.Input) *MultipartForm

File adds a file to the form data.

func (*MultipartForm) Set

func (mf *MultipartForm) Set(key string, values ...string) *MultipartForm

Set sets form values.

type RequestBuilder added in v0.10.5

type RequestBuilder struct {
	*http.Request
	// contains filtered or unexported fields
}

RequestBuilder provides methods for building a http.Request.

func CONNECT added in v0.10.5

func CONNECT(resource string) *RequestBuilder

CONNECT creates a CONNECT HTTP request.

func DELETE added in v0.10.5

func DELETE(resource string) *RequestBuilder

DELETE creates a DELETE HTTP request.

func GET

func GET(resource string) *RequestBuilder

GET creates a GET HTTP request.

func HEAD(resource string) *RequestBuilder

HEAD creates a HEAD HTTP request.

func OPTIONS added in v0.10.5

func OPTIONS(resource string) *RequestBuilder

OPTIONS creates an OPTIONS HTTP request.

func PATCH added in v0.10.5

func PATCH(resource string) *RequestBuilder

PATCH creates a PATCH HTTP request.

func POST

func POST(resource string, body flu.EncoderTo) *RequestBuilder

POST creates a POST HTTP request.

func PUT added in v0.10.5

func PUT(resource string) *RequestBuilder

PUT creates a PUT HTTP request.

func Request

func Request(resource string) *RequestBuilder

Request creates a new HTTP request.

func TRACE added in v0.10.5

func TRACE(resource string) *RequestBuilder

TRACE creates a TRACE HTTP request.

func (*RequestBuilder) Auth added in v0.10.5

func (*RequestBuilder) Body added in v0.10.5

func (r *RequestBuilder) Body(encoder flu.EncoderTo) *RequestBuilder

Body is used to specify the request body as flu.EncoderTo. Note that if passed encoder implements ContentType interface, it will be used to set Content-Type header automatically.

func (*RequestBuilder) BodyInput added in v0.10.5

func (r *RequestBuilder) BodyInput(input flu.Input) *RequestBuilder

BodyInput is used to specify the request body directly from flu.Input.

func (*RequestBuilder) ContentLength added in v0.10.5

func (r *RequestBuilder) ContentLength(contentLength int64) *RequestBuilder

ContentLength sets Content-Length header for this request. If not set, Content-Length header will be set automatically for request body io.Readers:

bytes.Buffer (flu.ByteBuffer)
bytes.Reader (flu.Bytes)
strings.Reader

Note that some servers may not accept unknown content length.

func (*RequestBuilder) ContentType added in v0.10.5

func (r *RequestBuilder) ContentType(contentType string) *RequestBuilder

ContentType sets Content-Type header for this request. If not set, Content-Type header will be set automatically if body implements ContentType interface.

func (*RequestBuilder) Exchange added in v0.10.5

func (r *RequestBuilder) Exchange(ctx context.Context, client Client) *ExchangeResult

Exchange executes the request and returns a response.

func (*RequestBuilder) Header added in v0.10.5

func (r *RequestBuilder) Header(key, value string) *RequestBuilder

Header set HTTP header for this request.

func (*RequestBuilder) Headers added in v0.10.5

func (r *RequestBuilder) Headers(kvPairs ...string) *RequestBuilder

func (*RequestBuilder) Method added in v0.10.5

func (r *RequestBuilder) Method(value string) *RequestBuilder

Method sets HTTP method for this request.

func (*RequestBuilder) Query added in v0.10.5

func (r *RequestBuilder) Query(key, value string) *RequestBuilder

Query sets a query parameter.

func (*RequestBuilder) QueryValues added in v0.10.5

func (r *RequestBuilder) QueryValues(values url.Values) *RequestBuilder

QueryValues sets query parameters from url.Values.

func (RequestBuilder) String added in v0.10.5

func (r RequestBuilder) String() string

type ResponseHandler

type ResponseHandler interface {
	Handle(resp *http.Response) error
}

ResponseHandler handles http.Response.

type ResponseHandlerFunc added in v0.10.5

type ResponseHandlerFunc func(resp *http.Response) error

ResponseHandlerFunc is ResponseHandler functional adapter.

func (ResponseHandlerFunc) Handle added in v0.10.5

func (f ResponseHandlerFunc) Handle(resp *http.Response) error

type RoundTripperFunc added in v0.10.5

type RoundTripperFunc func(req *http.Request) (*http.Response, error)

RoundTripperFunc is http.RoundTripper functional adapter.

func (RoundTripperFunc) RoundTrip added in v0.10.5

func (f RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)

type StatusCodeError

type StatusCodeError struct {
	// StatusCode is the response status code.
	StatusCode int
	// Status is the response status.
	Status string
}

StatusCodeError is returned when response status code does not match expected.

func (StatusCodeError) Error

func (e StatusCodeError) Error() string

type VarargsLengthError

type VarargsLengthError int

VarargsLengthError is returned when length of passed vararg is not even.

func (VarargsLengthError) Error

func (e VarargsLengthError) Error() string

func (VarargsLengthError) Length

func (e VarargsLengthError) Length() int

Jump to

Keyboard shortcuts

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