cast

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2020 License: MIT Imports: 24 Imported by: 0

README

cast

Build Status Go Report Card

Cast is a http request library written in Golang.

This project is ready for production use and the master branch is always stable. But the API may be broken in the future release.

Getting started

dep ensure -add github.com/xiaojiaoyu100/cast

Usage

Generate a Cast
c, err := cast.New(cast.WithBaseURL("https://status.github.com"))
Generate a request
request := c.NewRequest()
Get
request := c.NewRequest().Get().WithPath("/api.json")
response, err := c.Do(request)
POST X-WWW-FORM-URLENCODED
request := c.NewRequest().Get().WithPath("/api.json").WithFormURLEncodedBody(body)
resp, err := c.Do(request)
POST JSON
request := c.NewRequest().Post().WithPath("/api.json").WithJSONBody(body)
response, err := c.Do(request)
POST XML
request := c.NewRequest().Post().WithPath("/api.json").WithXMLBody(body)
response, err := c.Do(request)
POST MULTIPART FORM DATA
request := c.NewRequest().Post().WithPath("/api.json").WithMultipartFormDataBody(formData)
resp, err := c.Do(request)
Timeout
c.NewRequest().WithTimeout(3 * time.Second)
Retry
cast.WithRetry(3)
Backoff
cast.WithXXXBackoffStrategy()

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ShouldRetry added in v1.3.2

func ShouldRetry(err error) bool

ShouldRetry returns whether an error needs to be retried.

Types

type BasicAuth

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

BasicAuth provides info to authenticate

type BeforeRequestHook added in v1.0.6

type BeforeRequestHook func(cast *Cast, request *Request) error

BeforeRequestHook 请求之前执行的函数

type Cast

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

Cast provides a set of rules to its request.

func New

func New(sl ...Setter) (*Cast, error)

New returns an instance of Cast

func (*Cast) Do

func (c *Cast) Do(ctx context.Context, request *Request) (*Response, error)

Do initiates a request.

func (*Cast) GetBaseURL added in v1.3.3

func (c *Cast) GetBaseURL() string

func (*Cast) GetHeader added in v1.3.3

func (c *Cast) GetHeader() http.Header

func (*Cast) Logger added in v1.0.10

func (c *Cast) Logger() *logrus.Logger

Logger return the underlying log instance.

func (*Cast) NewRequest

func (c *Cast) NewRequest() *Request

NewRequest returns an instance of Request.

type Error added in v1.0.3

type Error string

Error defines cast error

func (Error) Error added in v1.0.3

func (err Error) Error() string

type FormData

type FormData struct {
	FieldName string
	Value     string
	FileName  string
	Path      string
	Reader    io.Reader
}

FormData represents multipart

type LogHook added in v1.0.5

type LogHook func(entry *logrus.Entry)

LogHook log hook模板

type Monitor added in v1.0.5

type Monitor struct {
	Callback LogHook
}

Monitor 信息监控

func NewMonitor added in v1.0.5

func NewMonitor(l LogHook) *Monitor

NewMonitor 返回一个实例

func (*Monitor) Fire added in v1.0.5

func (m *Monitor) Fire(entry *logrus.Entry) error

Fire 实际执行了回调

func (*Monitor) Levels added in v1.0.5

func (m *Monitor) Levels() []logrus.Level

Levels 这些级别的日志会被回调

type Request

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

Request is the http.Request wrapper with attributes.

func NewRequest

func NewRequest() *Request

NewRequest returns an instance of of Request.

func (*Request) AddHeader

func (r *Request) AddHeader(vv ...string)

AddHeader adds the key, value pair list.

func (*Request) Connect

func (r *Request) Connect() *Request

Connect sets the following http request method to "CONNECT".

func (*Request) Delete

func (r *Request) Delete() *Request

Delete sets the following http request method to "DELETE".

func (*Request) Get

func (r *Request) Get() *Request

Get sets the following http request method to "GET".

func (*Request) GetBody added in v1.3.3

func (r *Request) GetBody() []byte

func (*Request) GetHeader added in v1.3.3

func (r *Request) GetHeader() http.Header

func (*Request) GetMethod added in v1.3.3

func (r *Request) GetMethod() string

func (*Request) GetPath added in v1.3.3

func (r *Request) GetPath() string

func (*Request) GetPathParam added in v1.3.3

func (r *Request) GetPathParam() map[string]interface{}

func (*Request) GetQueryParam added in v1.3.3

func (r *Request) GetQueryParam() interface{}

func (*Request) Head

func (r *Request) Head() *Request

Head sets the following http request method to "HEAD".

func (*Request) HeaderExist added in v1.3.2

func (r *Request) HeaderExist(h string) bool

HeaderExist whether specified header exists.

func (*Request) Method added in v1.0.11

func (r *Request) Method(method string) *Request

Method sets the following http request method.

func (*Request) Options

func (r *Request) Options() *Request

Options sets the following http request method to "OPTIONS".

func (*Request) Patch

func (r *Request) Patch() *Request

Patch sets the following http request method to "PATCH".

func (*Request) Post

func (r *Request) Post() *Request

Post sets the following http request method to "POST".

func (*Request) Put

func (r *Request) Put() *Request

Put sets the following http request method to "PUT".

func (*Request) RawRequest added in v1.3.2

func (r *Request) RawRequest() *http.Request

RawRequest returns the http request.

func (*Request) ReqBody added in v1.3.2

func (r *Request) ReqBody() ([]byte, error)

ReqBody returns the underlying body.

func (*Request) SetHeader

func (r *Request) SetHeader(vv ...string) *Request

SetHeader sets the key, value pair list.

func (*Request) Trace

func (r *Request) Trace() *Request

Trace sets the following http request method to "TRACE".

func (*Request) WithCircuit added in v1.3.2

func (r *Request) WithCircuit(name string) *Request

WithCircuit is equipped with circuit.

func (*Request) WithCustomBody added in v1.3.2

func (r *Request) WithCustomBody(contentType string, body []byte) *Request

WithCustomBody creates custom body.

func (*Request) WithFormURLEncodedBody added in v1.0.3

func (r *Request) WithFormURLEncodedBody(body interface{}) *Request

WithFormURLEncodedBody creates body with url encoded string.

func (*Request) WithHeader

func (r *Request) WithHeader(header http.Header) *Request

WithHeader replaces the request header.

func (*Request) WithJSONBody added in v1.0.3

func (r *Request) WithJSONBody(body interface{}) *Request

WithJSONBody creates body with JSON.

func (*Request) WithMultipartFormDataBody

func (r *Request) WithMultipartFormDataBody(formData ...*FormData) *Request

WithMultipartFormDataBody creates body with form data

func (*Request) WithPath

func (r *Request) WithPath(path string) *Request

WithPath set the relative or absolute path for the http request if the base url don't be provided.

func (*Request) WithPathParam

func (r *Request) WithPathParam(pathParam map[string]interface{}) *Request

WithPathParam sets path parameters.

func (*Request) WithPlainBody

func (r *Request) WithPlainBody(body string) *Request

WithPlainBody creates body with plain text.

func (*Request) WithQueryParam

func (r *Request) WithQueryParam(queryParam interface{}) *Request

WithQueryParam sets query parameters.

func (*Request) WithTimeout

func (r *Request) WithTimeout(timeout time.Duration) *Request

WithTimeout sets the request timeout.

func (*Request) WithXMLBody added in v1.0.3

func (r *Request) WithXMLBody(body interface{}) *Request

WithXMLBody creates body with XML.

type RequestHook added in v1.3.2

type RequestHook func(cast *Cast, request *Request) error

RequestHook 请求回调

type Response

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

Response wraps the raw response with attributes.

func (*Response) AddHeader added in v1.3.2

func (resp *Response) AddHeader(vv ...string) *Response

AddHeader adds the key, value pair list.

func (*Response) Body

func (resp *Response) Body() []byte

Body returns the underlying response body.

func (*Response) Cookies

func (resp *Response) Cookies() []*http.Cookie

Cookies returns http cookies.

func (*Response) DecodeFromJSON added in v1.0.3

func (resp *Response) DecodeFromJSON(v interface{}) error

DecodeFromJSON decodes the JSON body into data variable.

func (*Response) DecodeFromXML added in v1.0.3

func (resp *Response) DecodeFromXML(v interface{}) error

DecodeFromXML decodes the XML body into data variable.

func (*Response) Header

func (resp *Response) Header() http.Header

Header returns the response header.

func (*Response) Method added in v1.3.2

func (resp *Response) Method() string

Method returns the request method.

func (*Response) SetHeader added in v1.3.2

func (resp *Response) SetHeader(vv ...string) *Response

SetHeader sets the key, value pair list.

func (*Response) Size

func (resp *Response) Size() int64

Size returns the length of the body.

func (*Response) StatusCode

func (resp *Response) StatusCode() int

StatusCode returns http status code.

func (*Response) StatusOk

func (resp *Response) StatusOk() bool

StatusOk returns true if http status code is 200, otherwise false.

func (*Response) String

func (resp *Response) String() string

String returns the underlying body in string.

func (*Response) Success

func (resp *Response) Success() bool

Success returns true if http status code is in [200,299], otherwise false.

func (*Response) URL added in v1.3.2

func (resp *Response) URL() string

URL returns the request url.

type RetryHook

type RetryHook func(response *Response, err error) bool

RetryHook defines a retry cond.

type Setter added in v1.0.3

type Setter func(cast *Cast) error

Setter can change the cast instance

func AddBeforeRequestHook added in v1.0.7

func AddBeforeRequestHook(hks ...BeforeRequestHook) Setter

AddBeforeRequestHook adds a before request hook.

func AddCircuitConfig added in v1.3.2

func AddCircuitConfig(name string, config ...circuit.Config) Setter

AddCircuitConfig loads a circuit config.

func AddHeader

func AddHeader(vv ...string) Setter

AddHeader provides an easy way to add header.

func AddRequestHook added in v1.3.2

func AddRequestHook(hks ...RequestHook) Setter

AddRequestHook adds a request hook.

func AddResponseHooks added in v1.3.2

func AddResponseHooks(hooks ...responseHook) Setter

AddResponseHooks adds hooks that can be triggered when a request finished.

func AddRetryHooks

func AddRetryHooks(hooks ...RetryHook) Setter

AddRetryHooks adds hooks that can be triggered when in customized conditions

func SetHeader

func SetHeader(vv ...string) Setter

SetHeader provides an easy way to set header.

func WithBaseURL added in v1.0.3

func WithBaseURL(url string) Setter

WithBaseURL sets the consistent part of your address.

func WithBasicAuth

func WithBasicAuth(username, password string) Setter

WithBasicAuth enables basic auth.

func WithBearerToken

func WithBearerToken(token string) Setter

WithBearerToken enables bearer authentication.

func WithConstantBackoffStrategy

func WithConstantBackoffStrategy(internal time.Duration) Setter

WithConstantBackoffStrategy changes the retry strategy called "Constant".

func WithCookies

func WithCookies(cookies ...*http.Cookie) Setter

WithCookies replaces the underlying cookies which can be sent to server when initiate a request.

func WithDefaultCircuit added in v1.3.2

func WithDefaultCircuit(name string) Setter

WithDefaultCircuit sets the default circuit breaker.

func WithExponentialBackoffDecorrelatedJitterStrategy

func WithExponentialBackoffDecorrelatedJitterStrategy(base, capacity time.Duration) Setter

WithExponentialBackoffDecorrelatedJitterStrategy changes the retry strategy called “Decorrelated Jitter”.

func WithExponentialBackoffEqualJitterStrategy

func WithExponentialBackoffEqualJitterStrategy(base, capacity time.Duration) Setter

WithExponentialBackoffEqualJitterStrategy changes the retry strategy called "Equal Jitter".

func WithExponentialBackoffFullJitterStrategy

func WithExponentialBackoffFullJitterStrategy(base, capacity time.Duration) Setter

WithExponentialBackoffFullJitterStrategy changes the retry strategy called "Full Jitter".

func WithExponentialBackoffStrategy

func WithExponentialBackoffStrategy(base, capacity time.Duration) Setter

WithExponentialBackoffStrategy changes the retry strategy called "Exponential".

func WithHTTPClientTimeout added in v1.0.3

func WithHTTPClientTimeout(timeout time.Duration) Setter

WithHTTPClientTimeout sets the underlying http client timeout.

func WithHeader

func WithHeader(h http.Header) Setter

WithHeader replaces the underlying header.

func WithLinearBackoffStrategy

func WithLinearBackoffStrategy(slope time.Duration) Setter

WithLinearBackoffStrategy changes the retry strategy called "Linear".

func WithLogHook added in v1.0.10

func WithLogHook(f LogHook) Setter

WithLogHook sets a log callback when condition is achieved.

func WithLogLevel added in v1.0.10

func WithLogLevel(l logrus.Level) Setter

WithLogLevel sets log level.

func WithRetry

func WithRetry(retry int) Setter

WithRetry sets the number of attempts, not counting the normal one.

Jump to

Keyboard shortcuts

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