restful

package module
v0.0.0-...-78b85f2 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2018 License: BSD-3-Clause Imports: 5 Imported by: 0

README

restful · CircleCI Status GitHub license PRs Welcome

restful - a very simple wrapper around restful clients in golang

Provides basic backoff and stats reporting facilities.


import "github.com/MediaMath/restful

func main() {

    type foo struct {
        Foo int `json:"foo"`
    }

    client := restful.WithExpectedResult(http.DefaultClient, http.StatusOK)

    fooResponse := &foo{}
    status, body, err := restful.DoJSON(client, http.NewRequest("GET", "http://example.com", nil), fooResponse)

    if err != nil || fooResponse.Foo != 98 {
	log.Fatal("Incorrect", status, body, err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoJSON

func DoJSON(restful Restful, request *http.Request, response interface{}) (status int, body []byte, err error)

DoJSON will parse the body of a request response as json

Types

type BackOff

type BackOff interface {
	Reset()
	Stop() (bool, time.Duration)
}

BackOff is modelled off of "github.com/cenk/backoff/BackOff"

type CreateBackOff

type CreateBackOff func() BackOff

CreateBackOff is a function that returns a restful.BackOff

type Notify

type Notify func(error, time.Duration)

Notify corresponds directly with github.com/cenk/backoff/Notify

type Restful

type Restful interface {
	Do(request *http.Request) (response *http.Response, err error)
}

Restful takes an http request and an optional response json struct

func WithBackoff

func WithBackoff(r Restful, b CreateBackOff, n Notify) Restful

WithBackoff will use the provided backoff policy

func WithExpectedResult

func WithExpectedResult(r Restful, expected int) Restful

WithExpectedResult will error if the response status is not the provided one

func WithStats

func WithStats(r Restful, stats Stats, requestName string) Restful

WithStats collects stats while it does the JSON

func WithStatsV2

func WithStatsV2(r Restful, stats StatsV2) Restful

WithStatsV2 collects stats in a better way while it does the JSON

type Stats

type Stats interface {
	Incr(string)
	TimingPeriod(string, time.Time, time.Time)
}

Stats is an interface for reporting statistics

type StatsV2

type StatsV2 interface {
	OnRequest()
	OnError(err error)
	Timing(hadError bool, start time.Time, end time.Time)
	OnResponse(statusCode int)
}

StatsV2 is an interface for reporting statistics, it is more explicit than the v1 interface

type UnexpectedResponseError

type UnexpectedResponseError struct {
	Expected int
	Received int
}

UnexpectedResponseError indicates that the request happened correctly but the response was unexpected

func IsUnexpectedResponseError

func IsUnexpectedResponseError(err error) *UnexpectedResponseError

IsUnexpectedResponseError will return the error as a UnexpectedResponseError struct or nil

func (*UnexpectedResponseError) Error

func (u *UnexpectedResponseError) Error() string

Jump to

Keyboard shortcuts

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