relax

package module
v0.0.0-...-7165f67 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2020 License: MIT Imports: 7 Imported by: 0

README

relax

Go Report Card

Documentation

Overview

Package relax provides HTTP client and server implementations that provide rate limitation and caching.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Cache       *cache.Cache
	Credentials *clientcredentials.Config
	HTTP        *http.Client
	Limiter     *rate.Limiter
	Timeout     time.Duration
}

Client represents an HTTP client with additional possible features

func New

func New(option ClientOption, feats ...ClientFeature) *Client

New creates a new client with a specified option along with the optional features implemented.

func (*Client) Do

func (c *Client) Do(req *http.Request, mods ...Modifier) (*http.Response, error)

Do sends an HTTP request and returns an HTTP response, with the option of using modifiers to cache or limit the response

func (*Client) Get

func (c *Client) Get(url string, mods ...Modifier) (resp *http.Response, err error)

Get issues a GET to the specified URL, with the option of using modifiers to cache or limit the response

type ClientFeature

type ClientFeature func(c *Client)

ClientFeature is a functional option for a client to specify additional optional features on top of the default implementation

func WithCache

func WithCache(defaultExpiration, cleanupInterval time.Duration) ClientFeature

WithCache allows the client to utilize a cache with a specified expiration time and cleanup interval

func WithDefaultCache

func WithDefaultCache() ClientFeature

WithDefaultCache allows the client to utilize a cache with default values

func WithDefaultLimiter

func WithDefaultLimiter() ClientFeature

WithDefaultLimiter allows the client to use a rate limiter for requests using default values

func WithDefaultTimeout

func WithDefaultTimeout(duration time.Duration) ClientFeature

WithDefaultTimeout allows the client to timeout after a default duration of 5 seconds

func WithLimiter

func WithLimiter(limit float64, burst int) ClientFeature

WithLimiter allows the client to use a rate limiter for requests

func WithTimeout

func WithTimeout(duration time.Duration) ClientFeature

WithTimeout allows the client to timeout after a specified duration

type ClientOption

type ClientOption func(c *Client)

ClientOption specifies how to create a client

func FromClient

func FromClient(http *http.Client) ClientOption

FromClient creates a client from a specified HTTP client

func FromConfig

func FromConfig(cred *clientcredentials.Config) ClientOption

FromConfig creates a client from a specified OAuth client configuration

func FromCredentials

func FromCredentials(apiKey, apiKeySecret, tokenURL string) ClientOption

FromCredentials creates a client from OAuth credentials

func FromDefaultClient

func FromDefaultClient() ClientOption

FromDefaultClient creates a client from the default HTTP implementation

type Modifier

type Modifier func(m *Modifiers)

Modifier is a functional option for a request

func UseCache

func UseCache(use bool) Modifier

UseCache forces the client to attempt to pull from a cache, if the request was already made, or otherwise update the cache.

func UseLimiter

func UseLimiter(use bool) Modifier

UseLimiter forces the client to obey rate limiting rules when performing the request or not.

type Modifiers

type Modifiers struct {
	UseCache   bool
	UseLimiter bool
}

Modifiers represents the potential modifiers to use on a request

Jump to

Keyboard shortcuts

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