CachedHttpClient

package module
v0.0.0-...-98e66a4 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2019 License: MIT Imports: 21 Imported by: 0

README

CachedHTTPClient-Go

How to use

Use the DefaultCachedClient

request,err := http.NewRequest("GET","http://example.com",nil)
DefaultCashedClient.Do(request) //not cached
...
DefaultCachedClient.Do(request) //cached
...

Use the DefaultCachedTransport


request, err := http.NewRequest("GET", "http://example.com", nil)
client := http.Client{
	Transport: DefaultCachedTransport,
}

client.Do(request) //not cached
client.Do(request) //cached

Create own CachedTransport

request, err := http.NewRequest("GET", "http://example.com", nil)

cachedTransport := CachedTransport{
	Cache:    NewMapCache(),
	Fallback: http.DefaultTransport,
}

client := http.Client{
	Transport: &cachedTransport,
}

client.Do(request) //not cached
client.Do(request) //cached

Create own Cacher

type MyCache struct {

}

func (m *MyCache) Get(req *http.Request) (*http.Response, error) {
	panic("implement me")
}

func (m *MyCache) Set(req *http.Request, res *http.Response) error {
	panic("implement me")
}

func someFunction() {

	request, err := http.NewRequest("GET", "http://example.com", nil)

	cachedTransport := CachedTransport{
		Cache:    &MyCache{},
		Fallback: http.DefaultTransport,
	}

	client := http.Client{
		Transport: &cachedTransport,
	}

	client.Do(request) //(not cached)
	client.Do(request) //(cached)
}

Caches

Interface Cacher
type Cacher interface {
	Get(req *http.Request) (*http.Response, error)
	Set(req *http.Request, res *http.Response) error
}
MapCache
type MapCache struct {
	cache map[string]*http.Response
	MapCacheOptions
}

type MapCacheOptions struct {
	IgnoreRequestBody            bool
	DontIncludeAllRequestHeaders bool
}
FileCache
type FileCache struct {
	*MapCache
	filePath string
	file     *os.File
}

Documentation

Overview

Package CachedHttpClient provides structs and a interface to add a custom cache to a http.client

Index

Constants

This section is empty.

Variables

View Source
var DefaultCachedTransport = &CachedTransport{
	Cache:                         NewMapCache(),
	Fallback:                      http.DefaultTransport,
	ContinueRoundTripWithSetError: nil,
}
View Source
var DefaultCashedClient = &http.Client{
	Transport: DefaultCachedTransport,
}
View Source
var NotInCacheError = errors.New("request not in the cache")

Functions

func CopyResponse

func CopyResponse(response *http.Response) (*http.Response, error)

CopyResponse creates a light copy of the response and the body and reads the body of the input response into a buffer and places a ReaderCloser of the buffers content in both responses

func DumpRequest

func DumpRequest(req *http.Request, ignoreBody bool, dontIncludeAllHeaders bool) ([]byte, error)

DumpRequest dumps the request to bytes using httputil.DumpRequest if includeAllHeaders httputil.DumpRequestOut is used

func NewJsonX509CertificateArrayArray

func NewJsonX509CertificateArrayArray(certs [][]*x509.Certificate) [][]*JsonX509Certificate

func ToX509CertificateArray

func ToX509CertificateArray(certificates []*JsonX509Certificate) []*x509.Certificate

func ToX509CertificateArrayArray

func ToX509CertificateArrayArray(certificates [][]*JsonX509Certificate) [][]*x509.Certificate

Types

type CachedTransport

type CachedTransport struct {
	Cache                         Cacher
	Fallback                      http.RoundTripper
	ContinueRoundTripWithSetError func(transport *CachedTransport, err error, request *http.Request, response *http.Response) bool
}

func (*CachedTransport) RoundTrip

func (c *CachedTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip checks if the cache has a response for the request and return it, if not save the response of the fallback RoundTripper to the cache. If the set function returns a error ContinueRoundTripWithSetError will be called if not nil

type Cacher

type Cacher interface {
	Get(req *http.Request) (*http.Response, error)
	Set(req *http.Request, res *http.Response) error
}

type FileCache

type FileCache struct {
	*MapCache
	// contains filtered or unexported fields
}

func NewFileCache

func NewFileCache(filePath string) (*FileCache, error)

NewFileCache create a new FileCache overriding the cache file

func OpenFileCache

func OpenFileCache(filePath string) (*FileCache, error)

OpenFileCache loaded the cache from an existing cache file

func OpenOrCreateFileCache

func OpenOrCreateFileCache(filePath string) (*FileCache, error)

OpenOrCreateFileCache open the existing cache file or creates a new

func (*FileCache) Get

func (f *FileCache) Get(req *http.Request) (*http.Response, error)

func (*FileCache) Set

func (f *FileCache) Set(req *http.Request, res *http.Response) error

type FileCacheEntry

type FileCacheEntry struct {
	Request  string
	Response *JsonResponse
}

type JsonPublicKey

type JsonPublicKey struct {
	PublicKey []byte
	Type      string
}

type JsonResponse

type JsonResponse struct {
	Status           string
	StatusCode       int
	Proto            string
	ProtoMajor       int
	ProtoMinor       int
	Header           http.Header
	Body             []byte
	ContentLength    int64
	TransferEncoding []string
	Close            bool
	Uncompressed     bool
	Trailer          http.Header
	Request          string
	TLS              *JsonTlsConnectionState
}

func NewJsonResponse

func NewJsonResponse(res *http.Response) (*JsonResponse, error)

func (*JsonResponse) ToResponse

func (response *JsonResponse) ToResponse() *http.Response

type JsonTlsConnectionState

type JsonTlsConnectionState struct {
	Version                     uint16
	HandshakeComplete           bool
	DidResume                   bool
	CipherSuite                 uint16
	NegotiatedProtocol          string
	NegotiatedProtocolIsMutual  bool
	ServerName                  string
	PeerCertificates            []*JsonX509Certificate
	VerifiedChains              [][]*JsonX509Certificate
	SignedCertificateTimestamps [][]byte
	OCSPResponse                []byte
	TLSUnique                   []byte
}

func NewJsonTlsConnectionState

func NewJsonTlsConnectionState(tls *tls.ConnectionState) *JsonTlsConnectionState

func (*JsonTlsConnectionState) ToConnectionState

func (state *JsonTlsConnectionState) ToConnectionState() *tls.ConnectionState

type JsonX509Certificate

type JsonX509Certificate struct {
	Raw                         []byte
	RawTBSCertificate           []byte
	RawSubjectPublicKeyInfo     []byte
	RawSubject                  []byte
	RawIssuer                   []byte
	Signature                   []byte
	SignatureAlgorithm          x509.SignatureAlgorithm
	PublicKeyAlgorithm          x509.PublicKeyAlgorithm
	PublicKey                   *JsonPublicKey
	Version                     int
	SerialNumber                *big.Int
	Issuer                      pkix.Name
	Subject                     pkix.Name
	NotBefore, NotAfter         time.Time
	KeyUsage                    x509.KeyUsage
	Extensions                  []pkix.Extension
	ExtraExtensions             []pkix.Extension
	UnhandledCriticalExtensions []asn1.ObjectIdentifier
	ExtKeyUsage                 []x509.ExtKeyUsage
	UnknownExtKeyUsage          []asn1.ObjectIdentifier
	BasicConstraintsValid       bool
	IsCA                        bool
	MaxPathLen                  int
	MaxPathLenZero              bool
	SubjectKeyId                []byte
	AuthorityKeyId              []byte
	OCSPServer                  []string
	IssuingCertificateURL       []string
	DNSNames                    []string
	EmailAddresses              []string
	IPAddresses                 []net.IP
	URIs                        []*url.URL
	PermittedDNSDomainsCritical bool
	PermittedDNSDomains         []string
	ExcludedDNSDomains          []string
	PermittedIPRanges           []*net.IPNet
	ExcludedIPRanges            []*net.IPNet
	PermittedEmailAddresses     []string
	ExcludedEmailAddresses      []string
	PermittedURIDomains         []string
	ExcludedURIDomains          []string
	CRLDistributionPoints       []string
	PolicyIdentifiers           []asn1.ObjectIdentifier
}

func NewJsonX509Certificate

func NewJsonX509Certificate(cert *x509.Certificate) *JsonX509Certificate

func NewJsonX509CertificateArray

func NewJsonX509CertificateArray(certs []*x509.Certificate) []*JsonX509Certificate

func (*JsonX509Certificate) ToCertificate

func (certificate *JsonX509Certificate) ToCertificate() *x509.Certificate

type MapCache

type MapCache struct {
	MapCacheOptions
	// contains filtered or unexported fields
}

MapCache caches the response in a map string -> *http.Response

func NewMapCache

func NewMapCache(options ...MapCacheOptions) *MapCache

func (*MapCache) Get

func (m *MapCache) Get(req *http.Request) (*http.Response, error)

func (*MapCache) Set

func (m *MapCache) Set(req *http.Request, res *http.Response) error

type MapCacheOptions

type MapCacheOptions struct {
	IgnoreRequestBody            bool
	DontIncludeAllRequestHeaders bool
}

Jump to

Keyboard shortcuts

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