nurse/protocols/http/get.go

98 lines
1.9 KiB
Go
Raw Permalink Normal View History

2022-06-09 20:12:45 +00:00
package http
import (
"bytes"
"context"
2022-06-09 20:12:45 +00:00
"io"
"log/slog"
2022-06-09 20:12:45 +00:00
"net/http"
2022-09-22 09:46:36 +00:00
"code.icb4dc0.de/prskr/nurse/check"
"code.icb4dc0.de/prskr/nurse/config"
"code.icb4dc0.de/prskr/nurse/grammar"
"code.icb4dc0.de/prskr/nurse/internal/retry"
2022-09-22 09:46:36 +00:00
"code.icb4dc0.de/prskr/nurse/validation"
2022-06-09 20:12:45 +00:00
)
type ClientInjectable interface {
SetClient(client *http.Client)
}
var (
_ check.SystemChecker = (*GenericCheck)(nil)
_ ClientInjectable = (*GenericCheck)(nil)
)
type GenericCheck struct {
*http.Client
validators validation.Validator[*http.Response]
Method string
Body []byte
URL string
}
func (g *GenericCheck) SetClient(client *http.Client) {
if client == nil {
return
}
g.Client = client
}
func (g *GenericCheck) Execute(ctx check.Context) error {
logger := slog.Default().With(
slog.String("check", "http"),
slog.String("method", g.Method),
slog.String("url", g.URL),
)
return retry.Retry(ctx, ctx.AttemptCount(), ctx.AttemptTimeout(), func(ctx context.Context, attempt int) error {
logger.Debug("Execute check", slog.Int("attempt", attempt))
var body io.Reader
if len(g.Body) > 0 {
body = bytes.NewReader(g.Body)
}
req, err := http.NewRequestWithContext(ctx, g.Method, g.URL, body)
if err != nil {
return err
}
resp, err := g.Do(req)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
return g.validators.Validate(resp)
})
2022-06-09 20:12:45 +00:00
}
func (g *GenericCheck) UnmarshalCheck(c grammar.Check, _ config.ServerLookup) error {
const urlArgsNumber = 1
inst := c.Initiator
if err := grammar.ValidateParameterCount(inst.Params, urlArgsNumber); err != nil {
return err
}
if g.Client == nil {
g.Client = http.DefaultClient
}
var err error
if g.URL, err = inst.Params[0].AsString(); err != nil {
return err
}
if g.validators, err = registry.ValidatorsForFilters(c.Validators); err != nil {
return err
}
return nil
}