httptask

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

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

Index

Examples

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

Timeout info can be set without changing http client (using context). But when to set timeout info is critical. Take a look at following two examples.

Example (Correct)
globalCtx, stop := context.WithCancel(context.TODO())
defer stop()

// Start a http server at http://127.0.0.1:9487 for test
//
// It provides an endpoint "/slow" which will write response body after
// waiting 100ms.
done := newServer().Go(globalCtx)
time.Sleep(10 * time.Millisecond)

extractBody := func(r *http.Response) ([]byte, error) {
	defer r.Body.Close()
	return io.ReadAll(r.Body)
}

req := NewRequest(
	http.MethodGet, "http://127.0.0.1:9487/slow",
).Do()

// The timeout info covers full transportation of data, including request
// sending, http header and body retrieving.
buf, err := forge.Convert(
	req, extractBody,
).With(task.Timeout(time.Minute)).Run(globalCtx)
if err != nil {
	fmt.Println("unexpected error:", err)
	return
}
// simulates some processing work or network latency
time.Sleep(200 * time.Millisecond)
// The timeout info above covers full transportation of data, including
// request sending, response header and body retrieving.

fmt.Println(err)
fmt.Println(len(buf))

// stop http server
stop()
<-done
Output:

<nil>
1048576
Example (Incorrect)
globalCtx, stop := context.WithCancel(context.TODO())
defer stop()

// Start a http server at http://127.0.0.1:9487 for test
//
// It provides an endpoint "/slow" which will write response body after
// waiting 100ms.
done := newServer().Go(globalCtx)
time.Sleep(10 * time.Millisecond)

resp, err := NewRequest(
	http.MethodGet, "http://127.0.0.1:9487/slow",
).Do().With(task.Timeout(time.Minute)).Run(globalCtx)
if err != nil {
	fmt.Println("unexpected error:", err)
	return
}
defer resp.Body.Close()
// simulates some processing work or network latency
time.Sleep(200 * time.Millisecond)
// The timeout info above covers only request sending and response header
// retrieving, so context is canceled now.

// now read body after context is canceled, causing error
_, err = io.ReadAll(resp.Body)
fmt.Println(errors.Is(err, context.Canceled))

// stop http server
stop()
<-done
Output:

true

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.

Take a look at ReqGen.Do for more detailed explaination and common gotcha.

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