greq

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

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

Go to latest
Published: Feb 28, 2016 License: MIT Imports: 8 Imported by: 0

README

greq

Yet another HTTP client library for go(golang).

Example

// examples/retry.go
package main

import (
	"errors"
	"fmt"
	"github.com/bluele/greq"
	"io/ioutil"
	"net/http"
)

func main() {
	res, err := greq.Get("http://example.com/notfound.html").
		RequestHandler(greq.RetryBackoff(3, greq.NewBackOff())).
		ResponseHandler(func(res *http.Response, err error) error {
		if res != nil && res.StatusCode >= 400 && res.StatusCode < 500 {
			return errors.New("40X error")
		}
		return err
	}).Do()
	if err != nil {
		fmt.Println("error:", err.Error())
		return
	}
	body, _ := ioutil.ReadAll(res.Body)
	fmt.Println(string(body))
}

Output

$ go run examples/retry.go
error: 40X error

TODO

  • Write more tests.

Author

Jun Kimura

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Debug = false
)

Functions

func Bytes

func Bytes(res *http.Response, err error) ([]byte, error)

func JSON

func JSON(res *http.Response, err error, ptr interface{}) error

func NewBackOff

func NewBackOff() backoff.BackOff

NewBackOff returns cenkalti/backoff.BackOff

func String

func String(res *http.Response, err error) (string, error)

Types

type Request

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

Request context object.

func Get

func Get(rawurl string) *Request

Set get method

func New

func New() *Request

Create a new request object.

func Post

func Post(rawurl string, body []byte) *Request

Set post method

func (*Request) AddCookie

func (req *Request) AddCookie(cookie *http.Cookie) *Request

AddCookie adds a cookie to request headers.

func (*Request) AddHeader

func (req *Request) AddHeader(key string, values ...string) *Request

SetHeader adds key-values to request header.

func (*Request) Bytes

func (req *Request) Bytes() ([]byte, error)

Type converter for []byte response body.

func (*Request) Client

func (req *Request) Client() *http.Client

Client returns current *http.Client

func (*Request) Debug

func (req *Request) Debug(debug bool) *Request

Give true argument, print debug log when do request.

func (*Request) Do

func (req *Request) Do() (*http.Response, error)

Do HTTP requests using itself parameters.

func (*Request) Get

func (req *Request) Get() *Request

Set get method

func (*Request) Header

func (req *Request) Header() http.Header

Header returns current http.Header.

func (*Request) JSON

func (req *Request) JSON(ptr interface{}) error

JSON bind a response body to specified object.

func (*Request) Method

func (req *Request) Method() string

Method returns method name.

func (*Request) Post

func (req *Request) Post() *Request

Set post method

func (*Request) RequestHandler

func (req *Request) RequestHandler(handler RequestHandler) *Request

RequestHandler hooks an event which before sending request.

func (*Request) ResponseHandler

func (req *Request) ResponseHandler(handler ResponseHandler) *Request

ResponseHandler hooks an event which after sending request.

func (*Request) SetBody

func (req *Request) SetBody(body []byte) *Request

SetBody sets specified body as request body.

func (*Request) SetClient

func (req *Request) SetClient(client http.Client) *Request

SetClient sets *http.Client

func (*Request) SetHeader

func (req *Request) SetHeader(key string, values ...string) *Request

SetHeader sets key-values as request header.

func (*Request) SetMethod

func (req *Request) SetMethod(method string) *Request

SetMethod sets specified string as http method.

func (*Request) SetURL

func (req *Request) SetURL(rawurl string) *Request

SetURL sets specified string as request url.

func (*Request) SetUseragent

func (req *Request) SetUseragent(value string) *Request

SetUseragent sets a specified string as request useragent.

func (*Request) String

func (req *Request) String() (string, error)

Type converter for string response body.

func (*Request) URL

func (req *Request) URL() string

URL returns url string.

type RequestHandler

type RequestHandler func(*Request, RequestMethod) (*http.Response, error)

func Retry

func Retry(retry int, interval time.Duration) RequestHandler

Retry retry to request at even intervals. retry: retry number interval: retry interval

func RetryBackoff

func RetryBackoff(retry int, b backoff.BackOff) RequestHandler

Exponential backoff retry: retry number b: cenkalti backoff object

func RetryOnResult

func RetryOnResult(cb func(*http.Response, error) bool, interval time.Duration) RequestHandler

We should retry if specified function returns true. cb: callback function after request. If this function returns true, retry request cancelled. interval: retry number

type RequestMethod

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

type ResponseHandler

type ResponseHandler func(*http.Response, error) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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