Categorygithub.com/xiaojiaoyu100/cast
modulepackage
1.4.2
Repository: https://github.com/xiaojiaoyu100/cast.git
Documentation: pkg.go.dev

# README

cast

Build Status Go Report Card GoDoc

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

# Functions

AddBeforeRequestHook adds a before request hook.
AddCircuitConfig loads a circuit config.
AddHeader provides an easy way to add header.
AddRequestHook adds a request hook.
AddResponseHooks adds hooks that can be triggered when a request finished.
AddRetryHooks adds hooks that can be triggered when in customized conditions.
New returns an instance of Cast.
NewMonitor 返回一个实例.
NewRequest returns an instance of of Request.
SetHeader provides an easy way to set header.
ShouldRetry returns whether an error needs to be retried.
WithBaseURL sets the consistent part of your address.
WithBasicAuth enables basic auth.
WithBearerToken enables bearer authentication.
WithConstantBackoffStrategy changes the retry strategy called "Constant".
WithCookies replaces the underlying cookies which can be sent to server when initiate a request.
WithDefaultCircuit sets the default circuit breaker.
WithExponentialBackoffDecorrelatedJitterStrategy changes the retry strategy called “Decorrelated Jitter”.
WithExponentialBackoffEqualJitterStrategy changes the retry strategy called "Equal Jitter".
WithExponentialBackoffFullJitterStrategy changes the retry strategy called "Full Jitter".
WithExponentialBackoffStrategy changes the retry strategy called "Exponential".
WithHeader replaces the underlying header.
WithHTTPClientTimeout sets the underlying http client timeout.
WithLinearBackoffStrategy changes the retry strategy called "Linear".
WithLogHook sets a log callback when condition is achieved.
WithLogLevel sets log level.
WithRetry sets the number of attempts, not counting the normal one.

# Structs

BasicAuth provides info to authenticate.
Cast provides a set of rules to its request.
FormData represents multipart.
Monitor 信息监控.
Request is the http.Request wrapper with attributes.
Response wraps the raw response with attributes.

# Type aliases

BeforeRequestHook 请求之前执行的函数.
Error defines cast error.
LogHook log hook模板.
RequestHook 请求回调.
RetryHook defines a retry cond.
Setter can change the cast instance.