httptask

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MPL-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package httptask provides some helper to wrap http server and some client job into task.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Server

func Server(s *http.Server, shutdownMods ...task.CtxMod) task.Task

Server wraps s into a task so it can shutdown gracefully when canceled.

s.Shutdown is called with a new context modified by shutdownMods.

Types

type ReqGen

type ReqGen forge.Generator[*http.Request]

ReqGen is a forge.Generator which generates HTTP request.

func NewRequest

func NewRequest[T io.Reader](method, url string, bodyGen forge.Generator[T]) ReqGen

NewRequest wraps http.NewRequestWithContext into a ReqGen.

func (ReqGen) AddCookie

func (r ReqGen) AddCookie(c *http.Cookie) ReqGen

AddCookie adds c to generated request.

func (ReqGen) AddHeader

func (r ReqGen) AddHeader(k, v string) ReqGen

AddHeader adds HTTP header to generated request.

func (ReqGen) Customize

func (r ReqGen) Customize(f func(*http.Request) (*http.Request, error)) ReqGen

Customize use a function to customize the generated request.

func (ReqGen) Do

func (r ReqGen) Do() forge.Generator[*http.Response]

Do is shortcut to DoWith(nil)

You might want to use it in most case, since timeout info can be set without changing http client (using context). Following code set a 3 seconds timeout to request, send it to server, wait a second and retry for once if failed.

body := `{"a":1}`
resp, err := NewRequest(method, url, forge.StringReader(body)).SetHeader(
	"Content-Type", "application/json",
).Do().With(
	task.Timeout(3*time.Second),
).TimedFail(time.Second).RetryN(1).Run(ctx)

func (ReqGen) DoWith

func (r ReqGen) DoWith(cl *http.Client) forge.Generator[*http.Response]

DoWith creates a forge.Generator that generates http.Response.

It builds http request using r and send the request using cl to get response.

Like idiom of http package, pass nil to cl will use http.DefaultClient, or you might use ReqGen.Do for lesser key strokes.

func (ReqGen) SetHeader

func (r ReqGen) SetHeader(k, v string) ReqGen

SetHeader overwrites HTTP header to generated request.

func (ReqGen) Update

func (r ReqGen) Update(f func(*http.Request) *http.Request) ReqGen

Update use a function to customize the generated request.

Jump to

Keyboard shortcuts

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