client

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2017 License: MIT Imports: 9 Imported by: 2

Documentation

Index

Constants

View Source
const (
	LOGDEBUG = iota
	LOGINFO
	LOGERROR
)

Constants for the request log level. Currently ignored if not LOGINFO.

Variables

View Source
var (
	LhAddress  string // LhAddress: The address of Lighthouse instance (FQDN or IP)
	LhUsername string // LhUsername: The username for auth.
	LhPassword string // LhPassword: The password for auth.
	LhToken    string // LhToken: The auth token.
	LogLevel   int    // LogLevel: The loglevel, for request logging.
)

All variables are retrieved from the config file and set in main routine.

Functions

func BuildReq

func BuildReq(body *[]byte, url string, method string, auth bool) (*http.Request, error)

BuildReq is a wrapper around the http.NewRequest function that ensures authenticated requests have the expected auth headers, and any http client has the fault tolerance etc added to it.

func CheckToken added in v0.1.3

func CheckToken() (bool, error)

CheckToken checks if a token is still valid with the lighthouse instance. Returns true if it's invalid.

func GetToken added in v0.1.3

func GetToken() (string, error)

GetToken logs into the lighthouse instance and creates an Auth token for subsequent requests.

func GetURL

func GetURL(uri string, params ...Parameters) (string, error)

GetURL returns a formatted and percent encoded URL from the lhaddress in config. Expects that version, and uri start with / and do not end with a /.

func ParseReq

func ParseReq(resp *http.Response) ([]byte, error)

ParseReq checks error codes, and returns the body of a successful request.

Types

type Client

type Client interface {
	Do(r *http.Request) (*http.Response, error)
}

Client is an interface with the idea of wrapping an http.Client with extra functionality.

func Decorate

func Decorate(c Client, d ...Decorator) Client

Decorate takes a Client and wraps it with the provided decorators.

func HTTPClient added in v0.1.3

func HTTPClient() Client

HTTPClient returns a decorated http client.

type Decorator

type Decorator func(Client) Client

Decorator wraps a Client with extra behaviour. Inspired by Tomas Senart (https://www.youtube.com/watch?v=xyDkyFjzFVc)

func IgnoreTLSErr added in v0.1.3

func IgnoreTLSErr() Decorator

IgnoreTLSErr is a that will prevent http client certificate errors when making an http request with a self-signed cert.

func Retry

func Retry(attempts int, backoff time.Duration) Decorator

Retry is a will retry an http request up to 'attempts' number of times, gradually increasing the retry wait time the more failed attempts.

func WriteLog added in v0.1.3

func WriteLog() Decorator

WriteLog will print basic information for the current request. TODO: Improve logging capabilities.

type Func added in v0.1.3

type Func func(*http.Request) (*http.Response, error)

Func is the implementation of the Client interface.

func (Func) Do added in v0.1.3

func (f Func) Do(r *http.Request) (*http.Response, error)

Do performs the http request.

type Parameters

type Parameters struct {
	Name  string
	Value string
}

Parameters can be passed to the getURL function if query params are needed. These will be added and the percent-encoded URL will be returned.

Jump to

Keyboard shortcuts

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