Documentation
¶
Overview ¶
Package httptask provides some helper to wrap http server and some client job into task.
Index ¶
- func EmptyBody(_ context.Context) (io.Reader, error)
- func Server(s *http.Server, shutdownMods ...task.CtxMod) task.Task
- type ReqGen
- func (r ReqGen) AddCookie(c *http.Cookie) ReqGen
- func (r ReqGen) AddHeader(k, v string) ReqGen
- func (r ReqGen) Customize(f func(*http.Request) (*http.Request, error)) ReqGen
- func (r ReqGen) Do() forge.Generator[*http.Response]
- func (r ReqGen) DoWith(cl *http.Client) forge.Generator[*http.Response]
- func (r ReqGen) SetHeader(k, v string) ReqGen
- func (r ReqGen) Update(f func(*http.Request) *http.Request) ReqGen
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EmptyBody ¶ added in v0.1.1
EmptyBody is a helper to be used in NewRequest as empty body.
Types ¶
type ReqGen ¶
ReqGen is a forge.Generator which generates HTTP request.
func NewRequest ¶
NewRequest wraps http.NewRequestWithContext into a ReqGen.
You might want to take a look at forge.StringReader, forge.BytesReader, forge.OpenFile and forge.FsFile to save your life.
You have to take extra care if you want to share same bodyGen among multiple requests.
func (ReqGen) Do ¶
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 ¶
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.