walgo

package module
Version: v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2016 License: MIT Imports: 16 Imported by: 0

README

walgo

Web/API library in Go - Easy-to-use methods for implementing HTTP requests

Documentation

Overview

The walgo package provide functions commonly used when creating applications and/or APIs using web (HTTP) trafic.

The package mainly focus around sending and receiving HTTP requests containing JSON payloads - but other payload types are fully supported.

Index

Constants

View Source
const (
	DefaultClientName = "walgo"
)

Variables

View Source
var (
	// NoAuthorizationHeaderErr is the error returned when the TokenRatePolicy
	// can't resolve the client because there is no Authorization header value
	// in the request.
	NoAuthorizationHeaderErr = errors.New("No authorization header value.")

	// NoBearerPrefixErr is the error returned when the TokenRatePolicy
	// can't resolve the client because there is no "Bearer " prefix in the
	// Authorizastion header value in the request.
	NoBearerPrefixErr = errors.New("Authorization has no bearer prefix.")

	// NoHeaderValueErr is the error returned when the HeaderRatePolicy
	// can't get a value for the supplied header.
	NoHeaderValueErr = errors.New("No header value.")

	// RateLimitExceeded is returned from the RateLimitRequester when making
	// requests beyond the specified limit.
	RateLimitExceededErr = errors.New("Rate limit exceeded.")
)

Functions

func CheckErr

func CheckErr(w http.ResponseWriter, err error, next func())

CheckErr checks the provided error and if it is not nil it sends the status code 500 (Internal server error) to the given ResponseWriter.

There is a special case where the error provided is the sql.ErrNoRows. In that case the status code 404 is returned.

func CheckErrOutputJson

func CheckErrOutputJson(err error, w http.ResponseWriter, v interface{})

CheckErrOutputJson checks the provided error. If it is nil the provided v is encoded as JSON and written to the given response writer. If it is not nil the status code 500 (Internal server error) is instead sent.

CheckErr is used to check the error. If JSON encoding fails 500 is sent.

func VerifyBody

func VerifyBody(w http.ResponseWriter, r *http.Request, v interface{}, next func())

VarifyBody reads the body from the HTTP request and tries to decode it as JSON. It also checks for the presence of all the values in the given interface type. If the parsed body matches the interface the next function is called.

Types

type CookieRatePolicy added in v1.1.0

type CookieRatePolicy struct {
	// Name of the cookie holding the client identification string.
	Name string
}

CookieRatePolicy rate limits using the value of a cookie as the client identification.

func (CookieRatePolicy) GetClient added in v1.1.0

func (p CookieRatePolicy) GetClient(r *http.Request) (client string, err error)

GetClient implemenets getting the client id string from the cookie value using the CookieRatePolicy.

type FormFile

type FormFile struct {
	File io.ReadCloser
	Name string
}

FormFile is the representation of a file that should be added to a MultipartPayload.

type HeaderRatePolity added in v1.1.0

type HeaderRatePolity struct {
	// Name of the header holding the client identification string.
	Name string
}

HeaderRatePolicy rate limits using the value from the request header with the provided name as client identification.

func (HeaderRatePolity) GetClient added in v1.1.0

func (p HeaderRatePolity) GetClient(r *http.Request) (client string, err error)

GetClient implemenets getting the client id string from the header using the HeaderRatePolicy.

type IPRatePolicy added in v1.1.0

type IPRatePolicy struct{}

IPRatePolicy rate limits using the clients IP address as client identification.

func (IPRatePolicy) GetClient added in v1.1.0

func (p IPRatePolicy) GetClient(r *http.Request) (client string, err error)

GetClient implements getting the client id string from the request remote IP address value.

type MultipartPayload

type MultipartPayload struct {
	// contains filtered or unexported fields
}

MultipartPayload holds data for values and files that can be used when making requests with a multipart body.

func (*MultipartPayload) Add

func (p *MultipartPayload) Add(name, value string) (err error)

Add adds a simple string value to the MultipartPayload.

func (*MultipartPayload) AddFile

func (p *MultipartPayload) AddFile(name string, f FormFile) (err error)

AddFile adds a FormFile to the MultipartPayload.

type ParameterMap

type ParameterMap map[string]string

Maps parameters used in query string in HTTP requests.

func (ParameterMap) AddInt

func (p ParameterMap) AddInt(key string, value int)

Adds an integer parameter to the parameter map.

func (ParameterMap) AddString

func (p ParameterMap) AddString(key, value string)

Adds a string parameter to the parameter map.

type RateLimitHandler added in v1.1.0

type RateLimitHandler struct {
	// contains filtered or unexported fields
}

RateLimitHandler keeps the information about rate limiting and provide functions to handle requests or shield http.HandleFunc using the limits provided.

func NewRateLimiter added in v1.1.0

func NewRateLimiter(maxRequests int, duration time.Duration, p RatePolicy, handler http.Handler) (r *RateLimitHandler)

Creates a new rate limiter that accepts maxRequest in the duration given. The rate limiter uses the provided policy and redirects requests within the limit to the given handler (when itself is used as http.Handler).

func (*RateLimitHandler) LimitHandlerFunc added in v1.1.0

func (r *RateLimitHandler) LimitHandlerFunc(hf http.HandlerFunc) (h http.HandlerFunc)

LimitHandlerFunc takes a http.HandlerFunc and wraps it in a rate limited version.

func (*RateLimitHandler) ServeHTTP added in v1.1.0

func (r *RateLimitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP is implemented to satisfy the http.Handler interface. It checks if the request should be allowed through using the policy and if that is the case it forwards the call to the internal handler.

type RateLimitRequester added in v1.1.0

type RateLimitRequester struct {
	// contains filtered or unexported fields
}

RateLimitRequester is used for managing and limiting outgoing requests.

func (*RateLimitRequester) Delete added in v1.1.0

func (l *RateLimitRequester) Delete(url string, p ParameterMap) (r Response, err error)

Delete forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) Get added in v1.1.0

func (l *RateLimitRequester) Get(url string, p ParameterMap) (res Response, err error)

Get forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) Post added in v1.1.0

func (l *RateLimitRequester) Post(url string, p ParameterMap) (r Response, err error)

Post forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) PostJson added in v1.1.0

func (l *RateLimitRequester) PostJson(url string, p ParameterMap, v interface{}) (r Response, err error)

PostJson forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) PostMultipart added in v1.1.0

func (l *RateLimitRequester) PostMultipart(url string, p ParameterMap, m *MultipartPayload) (r Response, err error)

PostMultipart forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) PostRaw added in v1.1.0

func (l *RateLimitRequester) PostRaw(url string, p ParameterMap, data []byte) (r Response, err error)

PostRaw forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) PostValues added in v1.1.0

func (l *RateLimitRequester) PostValues(url string, p ParameterMap, v url.Values) (r Response, err error)

PostValues forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) Put added in v1.1.0

func (l *RateLimitRequester) Put(url string, p ParameterMap) (r Response, err error)

Put forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) PutJson added in v1.1.0

func (l *RateLimitRequester) PutJson(url string, p ParameterMap, v interface{}) (r Response, err error)

PutJson forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) PutMultipart added in v1.1.0

func (l *RateLimitRequester) PutMultipart(url string, p ParameterMap, m *MultipartPayload) (r Response, err error)

PutMultipart forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) PutRaw added in v1.1.0

func (l *RateLimitRequester) PutRaw(url string, p ParameterMap, data []byte) (r Response, err error)

PutRaw forwards the request to the internal Requester if it is within the rate limit.

func (*RateLimitRequester) PutValues added in v1.1.0

func (l *RateLimitRequester) PutValues(url string, p ParameterMap, v url.Values) (r Response, err error)

PutValues forwards the request to the internal Requester if it is within the rate limit.

type RatePolicy added in v1.1.0

type RatePolicy interface {
	// GetClient returns a string representing the client using the data in
	// the given request by the rules of the implementing policy.
	GetClient(*http.Request) (string, error)
}

RatePolicy is the common interface for the different rate limiting policies.

type Requester

type Requester interface {
	// Get performs a GET reuqest to the given URL with the given parameters.
	Get(url string, p ParameterMap) (res Response, err error)

	// Post performs a POST request to the given URL with the given
	// parameters and no request body.
	Post(url string, p ParameterMap) (r Response, err error)

	// PostJson performs a POST request to the given URL with the given
	// parameters and the supplied interface type encoded as JSON.
	PostJson(url string, p ParameterMap, v interface{}) (r Response, err error)

	// PostRaw performs a POST request to the given URL with the given
	// parameters and the supplied bytes as the request body.
	PostRaw(url string, p ParameterMap, data []byte) (r Response, err error)

	// PostMultipart performs a POST request to the given URL with the given
	// parameters and the supplied multipart payload encoded as the request
	// body.
	PostMultipart(url string, p ParameterMap, m *MultipartPayload) (r Response, err error)

	// PostValues performs a POST request to the given URL with the given
	// parameters and a body consisting of the supplied values urlencoded.
	PostValues(url string, p ParameterMap, v url.Values) (r Response, err error)

	// Put performs a PUT request to the given URL with the given parameters.
	Put(url string, p ParameterMap) (r Response, err error)

	// PutJson performs a PUT request to the given URL with the given
	// parameters and the supplied interface type encoded as JSON.
	PutJson(url string, p ParameterMap, v interface{}) (r Response, err error)

	// PutRaw performs a PUT request to the given URL with the given
	// parameters and the supplied bytes as the request body.
	PutRaw(url string, p ParameterMap, data []byte) (r Response, err error)

	// PutMultipart performs a PUT request to the given URL with the given
	// parameters and the supplied multipart payload encoded as the request
	// body.
	PutMultipart(url string, p ParameterMap, m *MultipartPayload) (r Response, err error)

	// PutValues performs a PUT request to the given URL with the given
	// parameters and a body consisting of the supplied values urlencoded.
	PutValues(url string, p ParameterMap, v url.Values) (r Response, err error)

	// Delete peforms a DELETE request to the given URL with the given
	// parameters.
	Delete(url string, p ParameterMap) (r Response, err error)
	// contains filtered or unexported methods
}

func NewRateLimitRequester added in v1.1.0

func NewRateLimitRequester(r Requester, limit int, duration time.Duration) (lr Requester)

Creates a new Requester that limits requets according to the given limit and duration. Requets inside the limit is forwarded to the internal requester.

func NewRequester

func NewRequester(c *http.Client, userAgent, authToken string) (r Requester)

NewRequester creates a new Requester using the supplied client. Every request has the User-Agent header set to the given value and if the authentication token differs from "" it is also used as the Authorization header value (with the "Bearer "-prefix).

type Response

type Response interface {
	// Data returns the raw bytes from the body of the response.
	Data() (data []byte)

	// String returns the content of the response body as a string.
	String() (s string)

	// Code returns the response code given by the server.
	Code() (code int)

	// Duraction returns the time it took to make the request and get the
	// response.
	Duration() (duration time.Duration)

	// JSON returns the content of the response body decoded as JSON
	// into the given interface.
	JSON(v interface{}) (err error)

	// Error gives the error that occured during the request - if any.
	Error() (err error)
}

func Delete

func Delete(url string, p ParameterMap) (r Response, err error)

Delete performs the Delete function on the default requster.

func Get

func Get(url string, p ParameterMap) (res Response, err error)

Get performs the Get function on the default requester.

func Post

func Post(url string, p ParameterMap) (r Response, err error)

Post performs the Post functions on the default requester.

func PostJson

func PostJson(url string, p ParameterMap, v interface{}) (r Response, err error)

PostJson performs the PostJson function on the default requester.

func PostMultipart

func PostMultipart(url string, p ParameterMap, m *MultipartPayload) (r Response, err error)

PostMultipart performs the PostMultipart function on the default requester.

func PostRaw

func PostRaw(url string, p ParameterMap, data []byte) (r Response, err error)

PostRaw performs the PostRaw function on the default requester.

func PostValues

func PostValues(url string, p ParameterMap, v url.Values) (r Response, err error)

PostValues performs the PostValues function on the default requester.

func Put

func Put(url string, p ParameterMap) (r Response, err error)

Put performs the Put function on the default requester.

func PutJson

func PutJson(url string, p ParameterMap, v interface{}) (r Response, err error)

PutJson performs the PutJson function on the default requster.

func PutMultipart

func PutMultipart(url string, p ParameterMap, m *MultipartPayload) (r Response, err error)

PutMultipart performs the PutMultipart function on the default requester.

func PutRaw

func PutRaw(url string, p ParameterMap, data []byte) (r Response, err error)

PutRaw performs the PutRaw function on the default requester.

func PutValues

func PutValues(url string, p ParameterMap, v url.Values) (r Response, err error)

PutValues performs the PutValues function on the default requester.

type TokenRatePolicy added in v1.1.0

type TokenRatePolicy struct{}

TokenRatePolicy rate limits using the Bearer token set en the request Authorization header as client identification.

func (TokenRatePolicy) GetClient added in v1.1.0

func (p TokenRatePolicy) GetClient(r *http.Request) (client string, err error)

GetClient implements getting the client id string from the request Authorization headers Bearer token.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL