Skip to content

Commit

Permalink
fix: remove generalization for http
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincenzo authored and Vincenzo committed Nov 4, 2024
1 parent 2a57008 commit 7ca7588
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
31 changes: 18 additions & 13 deletions internal/plugin/http/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ func (p *Plugin) Produce(k []byte, v []byte, headers map[string]string, configPa

var err error

cfg := p.configuration
err = util.UnmarshalConfig(&cfg, configParams)
if err != nil {
return nil, err
}
// parse configurable parameters
endpointURL := util.GetStringParam("endpoint.url", p.configuration.Endpoint.URL, configParams)
timeout := util.GetDurationParam("endpoint.timeout", p.configuration.Endpoint.timeoutValue, configParams)
method := util.GetStringParam("endpoint.method", string(p.configuration.Endpoint.Method), configParams)
expectStatusCode := util.GetIntParam("error_handling.expect_status_code", p.configuration.ErrorHandling.ExpectStatusCode, configParams)
ignoreStatusCode := util.GetBoolParam("error_handling.ignore_status_code", p.configuration.ErrorHandling.IgnoreStatusCode, configParams)
insecureSkipTLSVerify := util.GetBoolParam("tls.insecure_skip_verify", p.configuration.TLS.InsecureSkipVerify, configParams)

// adding headers
hds := make(map[string]string)
Expand All @@ -173,31 +175,34 @@ func (p *Plugin) Produce(k []byte, v []byte, headers map[string]string, configPa
hds[k] = v
}

p.setConfig(cfg)

// creating request
req := p.client.
SetTimeout(timeout).
SetTLSClientConfig(&tls.Config{
InsecureSkipVerify: insecureSkipTLSVerify,
Certificates: p.certificates,
}).
R().
SetHeaders(hds).
SetBody(v)

var resp *resty.Response

switch p.configuration.Endpoint.Method {
switch Method(method) {
case POST:
resp, err = req.Post(p.configuration.Endpoint.URL)
resp, err = req.Post(endpointURL)
case PUT:
resp, err = req.Put(p.configuration.Endpoint.URL)
resp, err = req.Put(endpointURL)
default:
resp, err = req.Post(p.configuration.Endpoint.URL)
resp, err = req.Post(endpointURL)
}

if err != nil {
return nil, err
}

if resp.StatusCode() != p.configuration.ErrorHandling.ExpectStatusCode &&
!p.configuration.ErrorHandling.IgnoreStatusCode {
if resp.StatusCode() != expectStatusCode &&
!ignoreStatusCode {
return nil, fmt.Errorf("Unexpected status code: %d", resp.StatusCode())
}

Expand Down
40 changes: 40 additions & 0 deletions internal/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,43 @@ func setField(config any, key string, value string) error {
}
return nil
}

func GetStringParam(key string, def string, params map[string]string) string {
if val, ok := params[key]; ok {
return val
}
return def
}

func GetDurationParam(key string, def time.Duration, params map[string]string) time.Duration {
if val, ok := params[key]; ok {
d, err := time.ParseDuration(val)
if err != nil {
return def
}
return d
}
return def
}

func GetIntParam(key string, def int, params map[string]string) int {
if val, ok := params[key]; ok {
i, err := strconv.Atoi(val)
if err != nil {
return def
}
return i
}
return def
}

func GetBoolParam(key string, def bool, params map[string]string) bool {
if val, ok := params[key]; ok {
b, err := strconv.ParseBool(val)
if err != nil {
return def
}
return b
}
return def
}

0 comments on commit 7ca7588

Please sign in to comment.