mirror of
https://github.com/glanceapp/glance.git
synced 2025-06-21 18:31:24 +02:00
feat: add parameters and array parameters support
This commit is contained in:
parent
1512718d41
commit
026b644630
@ -1294,6 +1294,7 @@ Examples:
|
|||||||
| headers | key (string) & value (string) | no | |
|
| headers | key (string) & value (string) | no | |
|
||||||
| frameless | boolean | no | false |
|
| frameless | boolean | no | false |
|
||||||
| template | string | yes | |
|
| template | string | yes | |
|
||||||
|
| parameters | key & value | no | |
|
||||||
|
|
||||||
##### `url`
|
##### `url`
|
||||||
The URL to fetch the data from. It must be accessible from the server that Glance is running on.
|
The URL to fetch the data from. It must be accessible from the server that Glance is running on.
|
||||||
@ -1313,6 +1314,17 @@ When set to `true`, removes the border and padding around the widget.
|
|||||||
##### `template`
|
##### `template`
|
||||||
The template that will be used to display the data. It relies on Go's `html/template` package so it's recommended to go through [its documentation](https://pkg.go.dev/text/template) to understand how to do basic things such as conditionals, loops, etc. In addition, it also uses [tidwall's gjson](https://github.com/tidwall/gjson) package to parse the JSON data so it's worth going through its documentation if you want to use more advanced JSON selectors. You can view additional examples with explanations and function definitions [here](custom-api.md).
|
The template that will be used to display the data. It relies on Go's `html/template` package so it's recommended to go through [its documentation](https://pkg.go.dev/text/template) to understand how to do basic things such as conditionals, loops, etc. In addition, it also uses [tidwall's gjson](https://github.com/tidwall/gjson) package to parse the JSON data so it's worth going through its documentation if you want to use more advanced JSON selectors. You can view additional examples with explanations and function definitions [here](custom-api.md).
|
||||||
|
|
||||||
|
##### `parameters`
|
||||||
|
A list of keys and values that will be sent to the custom-api as query paramters.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
parameters:
|
||||||
|
param1: value1
|
||||||
|
param2:
|
||||||
|
- item1
|
||||||
|
- item2
|
||||||
|
```
|
||||||
|
|
||||||
### Extension
|
### Extension
|
||||||
Display a widget provided by an external source (3rd party). If you want to learn more about developing extensions, checkout the [extensions documentation](extensions.md) (WIP).
|
Display a widget provided by an external source (3rd party). If you want to learn more about developing extensions, checkout the [extensions documentation](extensions.md) (WIP).
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@ -23,6 +24,7 @@ type customAPIWidget struct {
|
|||||||
Template string `yaml:"template"`
|
Template string `yaml:"template"`
|
||||||
Frameless bool `yaml:"frameless"`
|
Frameless bool `yaml:"frameless"`
|
||||||
Headers map[string]string `yaml:"headers"`
|
Headers map[string]string `yaml:"headers"`
|
||||||
|
Parameters map[string]interface{} `yaml:"parameters"`
|
||||||
APIRequest *http.Request `yaml:"-"`
|
APIRequest *http.Request `yaml:"-"`
|
||||||
compiledTemplate *template.Template `yaml:"-"`
|
compiledTemplate *template.Template `yaml:"-"`
|
||||||
CompiledHTML template.HTML `yaml:"-"`
|
CompiledHTML template.HTML `yaml:"-"`
|
||||||
@ -51,6 +53,32 @@ func (widget *customAPIWidget) initialize() error {
|
|||||||
return err
|
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 {
|
for key, value := range widget.Headers {
|
||||||
req.Header.Add(key, value)
|
req.Header.Add(key, value)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user