caching_http_client

package module
v0.0.0-...-bc02a2c Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2021 License: MIT Imports: 7 Imported by: 5 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

caching_http_client

http client that can do caching of requests

TODO:

  • maybe make caches thread-safe
  • maybe add a disk cache

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cache *MemoryCache) *http.Client

New creates http.Client

Types

type CachingRoundTripper

type CachingRoundTripper struct {
	Cache RequestResponseCache
	// this is RoundTripper to use to make the actual request
	// if nil, will use http.DefaultTransport
	RoundTripper http.RoundTripper

	// for diagnostics, you can check how many http requests
	// were served from a cache and how many from network requests
	RequestsFromCache    int `json:"-"`
	RequestsNotFromCache int `json:"-"`
}

CachingRoundTripper is a http round-tripper that implements caching of past requests

func GetCachingRoundTripper

func GetCachingRoundTripper(c *http.Client) *CachingRoundTripper

GetCachingRoundTripper returns CachingRoundTripper from c.Transport

func NewRoundTripper

func NewRoundTripper(cache RequestResponseCache) *CachingRoundTripper

NewRoundTripper creates http.RoundTripper that caches requests

func (*CachingRoundTripper) RoundTrip

func (t *CachingRoundTripper) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip is to satisfy http.RoundTripper interface

type MemoryCache

type MemoryCache struct {
	// CachedRequests remembers past requests and their responses
	CachedRequests []*RequestResponse `json:"cached_requests"`

	// if true, will not return cached responses (but will still
	// record requests / responses)
	// Useful for tracing requests (but only those that return 200)
	DisableRespondingFromCache bool

	// if true, when comparing body of the request, and the body
	// is json, we'll normalize JSON
	CompareNormalizedJSONBody bool
}

MemoryCache remembers past requests and responses

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache returns a cache for http requests

func (*MemoryCache) Add

func (c *MemoryCache) Add(rr *RequestResponse)

Add remembers a given RequestResponse

func (*MemoryCache) FindCachedResponse

func (c *MemoryCache) FindCachedResponse(r *http.Request, cachedBody *[]byte) (*RequestResponse, error)

FindCachedResponse returns

type RequestResponse

type RequestResponse struct {
	Method string `json:"method"`
	URL    string `json:"url"`
	Body   []byte `json:"body"`

	Response []byte      `json:"response"`
	Header   http.Header `json:"header"`
}

RequestResponse is a cache entry. It remembers important details of the request and response

type RequestResponseCache

type RequestResponseCache interface {
	Add(rr *RequestResponse)
	FindCachedResponse(r *http.Request, cachedBody *[]byte) (*RequestResponse, error)
}

RequestResponseCache defines interface

func GetCache

func GetCache(c *http.Client) RequestResponseCache

GetCache gets from the client if it's a client created by us

Jump to

Keyboard shortcuts

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