85 lines
1.5 KiB
Go
85 lines
1.5 KiB
Go
package http
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
|
|
"code.1533b4dc0.de/prskr/nurse/check"
|
|
"code.1533b4dc0.de/prskr/nurse/config"
|
|
"code.1533b4dc0.de/prskr/nurse/grammar"
|
|
"code.1533b4dc0.de/prskr/nurse/validation"
|
|
)
|
|
|
|
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 context.Context) error {
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|