httptask

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MPL-2.0 Imports: 6 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.

func ToBody added in v0.2.0

ToBody is a dirty hack to fix generic type error when you set body.

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) ReqGen

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) 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) Body added in v0.2.0

func (r ReqGen) Body(bodyGen forge.Generator[io.ReadCloser]) ReqGen

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

func (r ReqGen) ContentType(typ string) ReqGen

ContentType is a shortcut so set Content-Type header.

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) GetBody added in v0.2.0

func (r ReqGen) GetBody(bodyGen forge.Generator[io.ReadCloser]) ReqGen

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) Location added in v0.2.0

func (r ReqGen) Location(locGen forge.Generator[string]) ReqGen

Location sets request url from string.

func (ReqGen) SetHeader

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

SetHeader overwrites HTTP header to generated request.

func (ReqGen) URL added in v0.2.0

func (r ReqGen) URL(urlGen forge.Generator[*url.URL]) ReqGen

URL sets request url.

func (ReqGen) URLEncoded added in v0.2.0

func (r ReqGen) URLEncoded() ReqGen

URLEncoded is a shortcut so set Content-Type header.

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