httpagent

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2022 License: MIT Imports: 5 Imported by: 0

README

go-httpagent

Build Status codecov GoDoc

HTTP Agent for go programming language. Provides hooks, timeout, and default header for http.Client and any other similar interface.

Example

agent := httpagent.NewAgent(http.DefaultClient)
agent.DefaultTimeout = 10 * time.Second
agent.DefaultHeader.Set("User-Agent", "go-httpagent/0.1")
agent.RequestHooks.Append(&httpagent.RequestDumperHook{Writer: os.Stderr})
agent.ResponseHooks.Append(&httpagent.ResponseDumperHook{Writer: os.Stderr})

req, _ := http.NewRequest("GET", "https://karupas.org/", nil)

ctx := context.Background()
req = req.WithContext(req)
res, err := agent.Do(req)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultAgent = NewAgent(http.DefaultClient)
View Source
var NopRequestHook = nopRequestHook{}
View Source
var NopResponseHook = nopResponseHook{}

Functions

func ContextWithClient

func ContextWithClient(ctx context.Context, client Client) context.Context

Types

type Agent

type Agent struct {
	Client         Client
	DefaultTimeout time.Duration
	DefaultHeader  http.Header
	RequestHooks   *RequestHooks
	ResponseHooks  *ResponseHooks
}

func NewAgent

func NewAgent(client Client) *Agent

func (*Agent) Do

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

func (*Agent) WithClient

func (a *Agent) WithClient(client Client) *Agent

type Client

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

type ClientFunc

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

func (ClientFunc) Do

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

type RequestDumperHook

type RequestDumperHook struct {
	Writer io.Writer
}

func (*RequestDumperHook) Do

func (h *RequestDumperHook) Do(req *http.Request) error

type RequestHeaderHook

type RequestHeaderHook struct {
	Header       http.Header
	Add          bool
	SkipIfExists bool
}

func (*RequestHeaderHook) Do

func (h *RequestHeaderHook) Do(req *http.Request) error

type RequestHook

type RequestHook interface {
	Do(*http.Request) error
}

type RequestHookFunc

type RequestHookFunc func(*http.Request) error

func (RequestHookFunc) Do

func (h RequestHookFunc) Do(req *http.Request) error

type RequestHooks

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

func NewRequestHooks

func NewRequestHooks(hooks ...RequestHook) (h *RequestHooks)

func (*RequestHooks) Append

func (h *RequestHooks) Append(hook RequestHook)

func (*RequestHooks) Clone

func (h *RequestHooks) Clone() *RequestHooks

func (*RequestHooks) Do

func (h *RequestHooks) Do(req *http.Request) (err error)

func (*RequestHooks) Len

func (h *RequestHooks) Len() int

type ResponseDumperHook

type ResponseDumperHook struct {
	Writer io.Writer
}

func (*ResponseDumperHook) Do

func (h *ResponseDumperHook) Do(res *http.Response) error

type ResponseHook

type ResponseHook interface {
	Do(*http.Response) error
}

type ResponseHookFunc

type ResponseHookFunc func(*http.Response) error

func (ResponseHookFunc) Do

func (h ResponseHookFunc) Do(req *http.Response) error

type ResponseHooks

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

func NewResponseHooks

func NewResponseHooks(hooks ...ResponseHook) (h *ResponseHooks)

func (*ResponseHooks) Append

func (h *ResponseHooks) Append(hook ResponseHook)

func (*ResponseHooks) Clone

func (h *ResponseHooks) Clone() *ResponseHooks

func (*ResponseHooks) Do

func (h *ResponseHooks) Do(req *http.Response) (err error)

func (*ResponseHooks) Len

func (h *ResponseHooks) Len() int

Jump to

Keyboard shortcuts

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