feat: add parameters and array parameters support

This commit is contained in:
Ralph Ocdol
2025-02-28 08:48:07 +08:00
committed by Svilen Markov
parent 1512718d41
commit 026b644630
2 changed files with 47 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ import (
"log/slog"
"math"
"net/http"
"net/url"
"time"
"github.com/tidwall/gjson"
@@ -19,13 +20,14 @@ var customAPIWidgetTemplate = mustParseTemplate("custom-api.html", "widget-base.
type customAPIWidget struct {
widgetBase `yaml:",inline"`
URL string `yaml:"url"`
Template string `yaml:"template"`
Frameless bool `yaml:"frameless"`
Headers map[string]string `yaml:"headers"`
APIRequest *http.Request `yaml:"-"`
compiledTemplate *template.Template `yaml:"-"`
CompiledHTML template.HTML `yaml:"-"`
URL string `yaml:"url"`
Template string `yaml:"template"`
Frameless bool `yaml:"frameless"`
Headers map[string]string `yaml:"headers"`
Parameters map[string]interface{} `yaml:"parameters"`
APIRequest *http.Request `yaml:"-"`
compiledTemplate *template.Template `yaml:"-"`
CompiledHTML template.HTML `yaml:"-"`
}
func (widget *customAPIWidget) initialize() error {
@@ -51,6 +53,32 @@ func (widget *customAPIWidget) initialize() error {
return err
}
query := url.Values{}
for key, value := range widget.Parameters {
switch v := value.(type) {
case string:
query.Add(key, v)
case int, int8, int16, int32, int64, float32, float64:
query.Add(key, fmt.Sprintf("%v", v))
case []string:
for _, item := range v {
query.Add(key, item)
}
case []interface{}:
for _, item := range v {
switch item := item.(type) {
case string:
query.Add(key, item)
case int, int8, int16, int32, int64, float32, float64:
query.Add(key, fmt.Sprintf("%v", item))
}
}
}
}
req.URL.RawQuery = query.Encode()
for key, value := range widget.Headers {
req.Header.Add(key, value)
}