Documentation
¶
Overview ¶
Package httptask provides some helper to wrap http server and some client job into task.
Index ¶
- func Server(s *http.Server, shutdownMods ...task.CtxMod) task.Task
- func ToBody[T io.ReadCloser](g forge.Generator[T]) forge.Generator[io.ReadCloser]
- type ReqGen
- func (r ReqGen) AddCookie(c *http.Cookie) ReqGen
- func (r ReqGen) AddHeader(k, v string) ReqGen
- func (r ReqGen) Body(bodyGen forge.Generator[io.ReadCloser]) ReqGen
- func (r ReqGen) ContentType(typ 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) GetBody(bodyGen forge.Generator[io.ReadCloser]) ReqGen
- func (r ReqGen) Location(locGen forge.Generator[string]) ReqGen
- func (r ReqGen) SetHeader(k, v string) ReqGen
- func (r ReqGen) URL(urlGen forge.Generator[*url.URL]) ReqGen
- func (r ReqGen) URLEncoded() ReqGen
- func (r ReqGen) Update(f func(*http.Request) *http.Request) ReqGen
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Server ¶
Server wraps s into a task so it can shutdown gracefully when canceled.
s.Shutdown is called with a new context modified by shutdownMods.
func ToBody ¶ added in v0.2.0
func ToBody[T io.ReadCloser](g forge.Generator[T]) forge.Generator[io.ReadCloser]
ToBody is a dirty hack to fix generic type error when you set body.
Types ¶
type ReqGen ¶
ReqGen is a forge.Generator which generates HTTP request.
func NewRequest ¶
NewRequest wraps http.NewRequestWithContext into a ReqGen.
It generates a request with empty body by default, use ReqGen.Body or ReqGen.GetBody to set a body. You might want to take a look at forge.StringReader, forge.BytesReader, forge.OpenFile and forge.FsFile to save your life.
func (ReqGen) Body ¶ added in v0.2.0
Body sets the request body to request.
If a type error strikes you, give following code a try:
r.Body(forge.ToBody(bodyGen))
If you want to retry failed http request, you might want to cache the body with forge.Cached to prevent, for example, re-openning same file or generating new bytes.Buffer from same content.
func (ReqGen) ContentType ¶ added in v0.2.0
ContentType is a shortcut so set Content-Type header.
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.
func (ReqGen) GetBody ¶ added in v0.2.0
GetBody sets the request body and http.Request.GetBody to request.
If a type error strikes you, give following code a try:
r.GetBody(forge.ToBody(bodyGen))
func (ReqGen) URLEncoded ¶ added in v0.2.0
URLEncoded is a shortcut so set Content-Type header.