Documentation ¶
Index ¶
- Constants
- Variables
- func CachedResponse(c Cache, req *http.Request) (resp *http.Response, err error)
- func CreateHttpErrorMessage(url string, body string, statusCode int) string
- func Date(respHeaders http.Header) (date time.Time, err error)
- func HttpErrorPropagatedRetryPolicy(ctx context.Context, resp *http.Response, err error) (bool, error)
- func NewHttpRetryClient(log *zap.Logger, maxRetries int) *retryablehttp.Client
- func SetCacheExpireHeader(headers http.Header)
- func SetCommonLoggingAttributes(ctx context.Context, c *gin.Context) (*zap.Logger, context.Context)
- func SignBody(secret, body []byte) []byte
- type Cache
- type ErrorDetail
- type MemoryCache
- type Transport
- type WebhookConfig
Constants ¶
const ( // XFromCache is the header added to responses that are returned from the cache XFromCache = "X-From-Cache" CacheExpireHeaderKey = "X-Cache-Expire" )
const (
RequestHeaderKey = "X-Request-Id"
)
Variables ¶
var ( CacheDirectory = path.Join(os.TempDir(), fmt.Sprintf("%s-cache", settings.CliBinaryName)) MaxCacheAgeInSeconds = 300 )
var ErrNoDateHeader = errors.New("no Date header")
ErrNoDateHeader indicates that the HTTP headers contained no Date header.
var (
TraceHeaderKey = "X-TRACE"
)
Functions ¶
func CachedResponse ¶
CachedResponse returns the cached http.Response for req if present, and nil otherwise.
func CreateHttpErrorMessage ¶
func HttpErrorPropagatedRetryPolicy ¶
func HttpErrorPropagatedRetryPolicy(ctx context.Context, resp *http.Response, err error) (bool, error)
ErrorPropagatedRetryPolicy is the same as DefaultRetryPolicy, except it propagates errors back instead of returning nil. This allows you to inspect why it decided to retry or not.
func NewHttpRetryClient ¶
func SetCacheExpireHeader ¶
Types ¶
type Cache ¶
type Cache interface { // Get returns the []byte representation of a cached response and a bool // set to true if the value isn't empty Get(key string) (responseBytes []byte, ok bool) // Set stores the []byte representation of a response against a key Set(key string, responseBytes []byte) // Delete removes the value associated with the key Delete(key string) }
A Cache interface is used by the Transport to store and retrieve responses.
type ErrorDetail ¶
type ErrorDetail struct { Type string `json:"type,omitempty" yaml:"type,omitempty"` Title string `json:"title,omitempty" yaml:"title,omitempty"` Status int64 `json:"status,omitempty" yaml:"status,omitempty"` Detail string `json:"detail,omitempty" yaml:"detail,omitempty"` Instance string `json:"instance,omitempty" yaml:"instance,omitempty"` }
https://www.baeldung.com/rest-api-error-handling-best-practices
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache is an implemtation of Cache that stores responses in an in-memory map.
func NewMemoryCache ¶
func NewMemoryCache() *MemoryCache
NewMemoryCache returns a new Cache that will store items in an in-memory map
func (*MemoryCache) Delete ¶
func (c *MemoryCache) Delete(key string)
Delete removes key from the cache
func (*MemoryCache) Get ¶
func (c *MemoryCache) Get(key string) (resp []byte, ok bool)
Get returns the []byte representation of the response and true if present, false if not
func (*MemoryCache) Set ¶
func (c *MemoryCache) Set(key string, resp []byte)
Set saves response resp to the cache with key
type Transport ¶
type Transport struct { // The RoundTripper interface actually used to make requests // If nil, http.DefaultTransport is used Transport http.RoundTripper Cache Cache // If true, responses returned from the cache will be given an extra header, X-From-Cache MarkCachedResponses bool // If true, caching will be controlled locally UseLocalCacheTimes bool }
Transport is an implementation of http.RoundTripper that will return values from a cache where possible (avoiding a network request) and will additionally add validators (etag/if-modified-since) to repeated requests allowing servers to return 304 / Not Modified
func NewMemoryCacheTransport ¶
func NewMemoryCacheTransport() *Transport
NewMemoryCacheTransport returns a new Transport using the in-memory cache implementation
func NewTransport ¶
NewTransport returns a new Transport with the provided Cache implementation and MarkCachedResponses set to true
func NewTransportLocalCacheTime ¶
NewTransport returns a new Transport with the provided Cache implementation and MarkCachedResponses and UseLocalCacheTimes set to true
func (*Transport) RoundTrip ¶
RoundTrip takes a Request and returns a Response
If there is a fresh Response already in cache, then it will be returned without connecting to the server.
If there is a stale Response, then any validators it contains will be set on the new request to give the server a chance to respond with NotModified. If this happens, then the cached Response will be returned.