Documentation
¶
Index ¶
- Variables
- func NewConfigBuilder() *configBuilder
- type Cache
- type Config
- type SqliteCache
- func (s *SqliteCache) Read(request *http.Request) (*http.Response, error)
- func (s *SqliteCache) ReadContext(ctx context.Context, request *http.Request) (*http.Response, error)
- func (s *SqliteCache) Save(response *http.Response, expiryTime *time.Duration) error
- func (s *SqliteCache) SaveContext(ctx context.Context, response *http.Response, expiryTime *time.Duration) error
- type Transport
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMissingCache will be returned if cache is not set when creating an // instance of [Transport]. ErrMissingCache = errors.New("cache not set when creating transport") // ErrMissingConfig will be returned if config is not set when creating an // instance of [Transport]. ErrMissingConfig = errors.New("config not set when creating transport") )
var DefaultConfig = NewConfigBuilder().
WithAllowedStatusCodes(defaultAllowedStatusCodes).
WithAllowedMethods(defaultAllowedMethods).
WithExpiryTime(defaultExpiryTime).
Build()
DefaultConfig creates a Config with default values, namely:
- AllowedStatusCodes: http.StatusOK
- AllowedMethods: http.MethodGet
- ExpiryTime: 7 days.
var ErrNoDatabase = errors.New("no database connection")
ErrNoDatabase denotes when the database does not exist or has not been constructed correctly.
var ( // ErrNoResponse describes when the cache does not have a response stored. // [Transport] will check if ErrNoResponse is returned from [Cache.Read]. If // ErrNoResponse is returned, then the request/response will be saved with [Save]. ErrNoResponse = errors.New("no stored response") )
Functions ¶
func NewConfigBuilder ¶
func NewConfigBuilder() *configBuilder
Types ¶
type Cache ¶
type Cache interface {
// Save a response for a HTTP request using [context.Background].
Save(response *http.Response, expiryTime *time.Duration) error
// Read a saved response for a HTTP request using [context.Background].
Read(request *http.Request) (*http.Response, error)
// Save a response for a HTTP request with a [context.Context]. expiryTime
// is the duration from [time.Now] to expire the response.
SaveContext(ctx context.Context, response *http.Response, expiryTime *time.Duration) error
// Read a saved response for a HTTP request with a [context.Context]. If no
// response is saved for the corresponding request, or the expiryTime has
// been surpassed, then return [ErrNoResponse].
ReadContext(ctx context.Context, request *http.Request) (*http.Response, error)
}
Cache is the entrypoint for saving and reading responses. This can be implemented for a custom method to cache responses.
type Config ¶
type Config struct {
// AllowedStatusCodes describes if a HTTP response should be saved by
// checking that it's status code is accepted by [Cache]. If the HTTP
// response's status code is not in AllowedStatusCodes, then do not persist.
//
// This is a required field.
AllowedStatusCodes []int
// AllowedMethods describes if a HTTP response should be saved by checking
// if the HTTP request's method is accepted by the [Cache]. If the HTTP
// request's method is not in AllowedMethods, then do not persist.
//
// This is a required field.
AllowedMethods []string
// ExpiryTime describes when a HTTP response should be considered invalid.
ExpiryTime *time.Duration
}
Config describes the configuration to use when saving and reading responses from Cache using the Transport.
type SqliteCache ¶
SqliteCache is a default implementation of Cache which creates a local SQLite cache to persist and to query HTTP responses.
func NewSqliteCache ¶
func NewSqliteCache(databaseName string) (*SqliteCache, error)
NewSqliteCache creates a new SQLite database with a certain name. This name is the filename of the database. If the file does not exist, then we create it. If the file is in a non-existent directory, we create the directory.
func (*SqliteCache) ReadContext ¶
func (*SqliteCache) SaveContext ¶
type Transport ¶
type Transport struct {
// contains filtered or unexported fields
}
Transport is the main interface of the package. It uses Cache to persist HTTP response, and and uses configuration values from Config to interpret requests and responses.
func NewTransport ¶
NewTransport creates a Transport. If the cache is nil, return ErrMissingCache. If the config is nil, return ErrMissingConfig.
func (*Transport) RoundTrip ¶
RoundTrip wraps the http.DefaultTransport RoundTrip to execute HTTP requests and persists, if necessary, the responses. If Cache returns ErrNoResponse, then execute a HTTP request and persist the response if passing the criteria in [Transport.shouldSaveResponse].