trip

package module
v0.0.0-...-0574203 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2018 License: MIT Imports: 7 Imported by: 0

README

Build Status codecov Maintainability

trip - implements a round-trip pattern for http requests

Quick start

go get github.com/gregoryv/trip

The round-trip pattern

A round-trip pattern is basically

  1. Prepare trip
  2. Execute
  3. Optionally parse response

Other descriptions that fit this pattern would be remote procedure call(RPC), which http requests are of sorts. With this package the steps inbetween are abstracted and you get to write more go idomatic code. I designed this package in a way that resembles os.exec

Prepare trip

request := http.NewRequest("GET", "/", nil)
cmd := trip.NewCommand(request)

Do the trip

statusCode, err := cmd.Run()
// or if you want the response parsed
err := cmd.Output(&model)

When to use

When you talk to remote services and need to only vary parts of the flow, ie. an API has changed and requires a new parameter, then you only have to modify the part that builds your request. Hopefully it's easier to maintain a backwards compatible client for a constantly changing remote service.

Documentation

Overview

package trip implements a round-trip pattern for http requests

Index

Constants

View Source
const BadResponse = 590

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

type Command

type Command struct {
	Client   Client
	Request  *http.Request
	Response *http.Response
	// IsOk should return false if the response is considered wrong, according
	// to status and headers. The body should not be parsed.
	IsOk func(*http.Response) bool
	// Parse should convert the response into the given model. By default json.Unmarshal is
	// used. Parse should close the reader when done or on error.
	Parse func(io.ReadCloser, interface{}) error

	// Duration to wait before next retry
	Pause time.Duration
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand(request *http.Request) (cmd *Command)

NewCommand returns a command using the http.DefaultClient. By default requests that have a status code larger or equal to 400 return an error

func (*Command) Dump

func (cmd *Command) Dump(w io.Writer, body bool)

Dump writes request and response information, if body has already been read body=true has no affect.

func (Command) Error

func (cmd Command) Error() string

func (*Command) Output

func (cmd *Command) Output(model interface{}) (err error)

Output sends the request and does a status validation against considered status codes. Failing to send the request altogether results in a 590. Parsing errors result in 591

func (*Command) Run

func (cmd *Command) Run() (err error)

Run, calls the Output method with no model

func (*Command) Try

func (cmd *Command) Try(times int) (err error)

Try, runs command with Run() and retries if it fails

Jump to

Keyboard shortcuts

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