feather

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2022 License: MIT Imports: 16 Imported by: 1

README

Feather is a golang http client

Usage

go get -u github.com/enorith/feather

import (
	"github.com/enorith/feather"
)

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

	pr, e := client.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)
		}
	})
}

Interceptor


func requestLogger(logger *logger.Logger) feather.PipeFunc {
	return func(r *http.Request, next feather.Handler) *feather.Result {
        // print every http request
		logger.Infof("request [%s](%s)", r.Method, r.URL)
		return next(r)
	}
}

client := feather.NewClient()

client.Interceptor(requestLogger(logger.Default()))

Json unmarshal

type Response struct {
	*feather.Result
    Code int `json:"code"`
    Message string `json:"message"`
}


client := feather.NewClient()

req, _ := client.Get("http://bar.com/foo.json")

var resp Response
req.Then(&resp)

// or

req.Then(func(resp *Response) {
    fmt.Println(resp.Code)
})

File download


client := feather.NewClient()

file, _ := os.OpenFile("/tmp/temp.txt", os.O_CREATE|os.O_WRONLY, 0775)

req, _ := client.Get("http://bar.com/foo.json", feather.RequestOptions{
    Sink: file,
    OnProgress: func(now, total int64) {
        p := float64(now) / float64(total) * 100
        fmt.Printf("\rdownloading: [%s>%s] %.2f%%", strings.Repeat("=", int(p)), strings.Repeat(" ", 100-int(p)), p)
    },
})

req.Wait()

Documentation

Index

Constants

View Source
const (
	NoneProxy = "none"
)

Variables

View Source
var DefaultTimeout = 30 * time.Second

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

func (*Client) SyncRequest added in v0.0.4

func (c *Client) SyncRequest(method, url string, opts ...RequestOptions) (*Result, error)

SyncRequest send sync 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{}) *PendingRequest

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

func MergeRequestOptions added in v1.0.1

func MergeRequestOptions(ros ...RequestOptions) RequestOptions

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