feather

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2021 License: MIT Imports: 16 Imported by: 1

README

Feather is a golang http client

Usage

	c := feather.NewClient(feather.Options{BaseURI: "https://run.mocky.io/v3/", HttpErrors: true})

	pr, e := c.Get("ed68fdb5-e9e9-4846-bb0f-f208e6820039")
	if e != nil {
		t.Fatalf("request error %v", e)
	}

	pr.Then(func(r Result) {
		t.Logf("response: %v", r)
	})

	pr.Catch(func(err error) {
		if ue, ok := err.(feather.HttpError); ok {
			t.Logf("error: %v[%d]", err, ue.Response.StatusCode)
		}
	})

Documentation

Index

Constants

View Source
const (
	DefaultTimeout = 5 * time.Second
	NoneProxy      = "none"
)

Variables

This section is empty.

Functions

func NewRequestFromOptions

func NewRequestFromOptions(method string, path string, o RequestOptions) (*http.Request, error)

NewRequestFromOptions new http request from RequestOptions

Types

type Client

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

Client is request clinet

var DefaultClient *Client

func NewClient

func NewClient(opts ...Options) *Client

func (*Client) Config

func (c *Client) Config(opt Options) *Client

func (*Client) Delete

func (c *Client) Delete(url string, opts ...RequestOptions) (*PendingRequest, error)

Delete send DELETE http request

func (*Client) Get

func (c *Client) Get(url string, opts ...RequestOptions) (*PendingRequest, error)

Get send GET http request

func (*Client) Head added in v0.0.3

func (c *Client) Head(url string, opts ...RequestOptions) (*PendingRequest, error)

Head send HEAD http request

func (*Client) Interceptor

func (c *Client) Interceptor(pf PipeFunc) *Client

func (*Client) Patch

func (c *Client) Patch(url string, opts ...RequestOptions) (*PendingRequest, error)

Patch send PATCH http request

func (*Client) Post

func (c *Client) Post(url string, opts ...RequestOptions) (*PendingRequest, error)

Post send POST http request

func (*Client) Put

func (c *Client) Put(url string, opts ...RequestOptions) (*PendingRequest, error)

Put send PUT http request

func (*Client) Request

func (c *Client) Request(method, url string, opts ...RequestOptions) (*PendingRequest, error)

Request send http request

type ErrorHandler

type ErrorHandler func(error)

type Handler

type Handler func(r *http.Request) *Result

type HttpError added in v0.0.3

type HttpError struct {
	*Result
}

func (HttpError) Error added in v0.0.3

func (ue HttpError) Error() string

type Options

type Options struct {
	// BaseURI is prefix of request url
	BaseURI string
	// Timeout request timeout
	Timeout time.Duration
	// ProxyURL set proxy url for request
	ProxyURL string
	// HttpErrors trigger error if response is not ok
	HttpErrors bool
}

Options is request client options

type PendingRequest

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

func Delete

func Delete(url string, opts ...RequestOptions) (*PendingRequest, error)

func Download added in v0.0.3

func Download(url string, filename string, opts ...RequestOptions) (*PendingRequest, error)

func Get

func Get(url string, opts ...RequestOptions) (*PendingRequest, error)

func Patch

func Patch(url string, opts ...RequestOptions) (*PendingRequest, error)

func Post

func Post(url string, opts ...RequestOptions) (*PendingRequest, error)

func Put

func Put(url string, opts ...RequestOptions) (*PendingRequest, error)

func Request

func Request(method, url string, opts ...RequestOptions) (*PendingRequest, error)

func (*PendingRequest) Catch

func (pr *PendingRequest) Catch(cb ErrorHandler)

func (*PendingRequest) IsSuccess

func (pr *PendingRequest) IsSuccess() bool

func (*PendingRequest) Then

func (pr *PendingRequest) Then(cb interface{}) error

func (*PendingRequest) Wait

func (pr *PendingRequest) Wait() *Result

type PipeFunc

type PipeFunc func(r *http.Request, next Handler) *Result

type Pipeline

type Pipeline []PipeFunc

func (Pipeline) Push

func (pl Pipeline) Push(pf PipeFunc) Pipeline

func (Pipeline) Resolve

func (pl Pipeline) Resolve(r *http.Request, handler Handler) *Result

type ProgressHandler added in v0.0.3

type ProgressHandler func(now, total int64)

type RequestOptions

type RequestOptions struct {
	// Body is raw request body
	Body io.Reader
	// Handler is request handler
	Handler Handler
	// Json body of request
	Json interface{}
	// Header is request header
	Header http.Header
	// FormParams is request form params
	FormParams url.Values
	// Query is request query
	Query url.Values
	// Method is request method
	Method string
	// OnProgress handle content download progress
	OnProgress ProgressHandler
	// Sink response content to io.Writer, for file download
	Sink io.Writer
	// Upload file
	Upload *UploadFile
}

RequestOptions simplified http request options

type Result

type Result struct {
	*http.Response
	Err error
	// contains filtered or unexported fields
}

func (*Result) Content

func (r *Result) Content() []byte

func (*Result) ContentString

func (r *Result) ContentString() string

func (*Result) Unmarshal

func (r *Result) Unmarshal(v interface{}) error

type UploadFile added in v0.0.3

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

func NewUploadFile added in v0.0.3

func NewUploadFile(file io.Reader, key, filename string) *UploadFile

Jump to

Keyboard shortcuts

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