client

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(ctx context.Context, c Client, url string, header http.Header) (*http.Response, error)

Get will create a GET request with given headers and call Do on the client

func RetryAll

func RetryAll(_ int) bool

RetryAll is a RetryPolicy which retries all http statuses

func RetryNone

func RetryNone(_ int) bool

RetryNone is a RetryPolicy which doesn't retry anything

func StopOrContinue

func StopOrContinue(err error) error

StopOrContinue can help controlling the behaviour of concurrent GET requests when using an errgroup and encountering an error. Depending on the NVD_CONTINUE_DOWNLOADING env variable, this function will return the passed error (when we want to stop pending requests) or just log the error (when we want the pending requests to continue being processed).

Types

type Client

type Client interface {
	Do(req *http.Request) (*http.Response, error)
	Get(url string) (*http.Response, error)
}

Client is an interface used for making http requests

func Default

func Default() Client

Default returns the default http client to use

func WithRetries

func WithRetries(c Client, retries int, delay time.Duration, rp RetryPolicy) Client

WithRetries will retry all given requests for the specified number of times

  • if status is 200, returns
  • if status is covered by the retry policy and hasn't been retried the total number of times, retry
  • otherwise, fail the request

func WithThrottling

func WithThrottling(c Client, period time.Duration, requestsPerPeriod int) Client

WithThrottling creates a rate limitted client - all requests are throttled

func WithUserAgent

func WithUserAgent(c Client, userAgent string) Client

WithUserAgent wrapps the given client in a way that it always makes requests with User Agent header

type Config

type Config struct {
	UserAgent string
	// contains filtered or unexported fields
}

Config is used to configure a client

func (*Config) AddFlags

func (conf *Config) AddFlags()

AddFlags adds flags used to configure a client

func (*Config) Configure

func (conf *Config) Configure(c Client) Client

Configure configures the given client (add throttling, retries, ...)

func (*Config) Validate

func (conf *Config) Validate() error

type Err

type Err struct {
	Code   int
	Status string
	Body   string
}

Err encapsulates stuff from the http.Response

func (*Err) Error

func (e *Err) Error() string

Error is a part of the error interface

type FailedRetries

type FailedRetries int

FailedRetries is an error returned when all retries have been exhausted

func (FailedRetries) Error

func (fr FailedRetries) Error() string

Error is a part of the error interface

type RetryPolicy

type RetryPolicy func(status int) bool

RetryPolicy is a function which returns true if HTTP status should be retried

func Retry

func Retry(statuses ...int) RetryPolicy

Retry will only retry given statuses

func (*RetryPolicy) Set

func (rp *RetryPolicy) Set(s string) error

func (*RetryPolicy) String

func (rp *RetryPolicy) String() string

Jump to

Keyboard shortcuts

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