Add subrequests to custom-api (#385)

* feat: custom-api multiple API queries

* fix template check

* refactor

* Update implementation & docs

* Swap statement

* Update docs

---------

Co-authored-by: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com>
This commit is contained in:
Ralph Ocdol
2025-03-12 18:30:29 +08:00
committed by GitHub
parent c0bdf1551d
commit c265e42220
2 changed files with 185 additions and 41 deletions

View File

@ -1294,7 +1294,8 @@ Examples:
| headers | key (string) & value (string) | no | |
| frameless | boolean | no | false |
| template | string | yes | |
| parameters | key & value | no | |
| parameters | key (string) & value (string|array) | no | |
| subrequests | map of requests | no | |
##### `url`
The URL to fetch the data from. It must be accessible from the server that Glance is running on.
@ -1317,6 +1318,40 @@ The template that will be used to display the data. It relies on Go's `html/temp
##### `parameters`
A list of keys and values that will be sent to the custom-api as query paramters.
##### `subrequests`
A map of additional requests that will be executed concurrently and then made available in the template via the `.Subrequest` property. Example:
```yaml
- type: custom-api
cache: 2h
subrequests:
another-one:
url: https://uselessfacts.jsph.pl/api/v2/facts/random
title: Random Fact
url: https://uselessfacts.jsph.pl/api/v2/facts/random
template: |
<p class="size-h4 color-paragraph">{{ .JSON.String "text" }}</p>
<p class="size-h4 color-paragraph margin-top-15">{{ (.Subrequest "another-one").JSON.String "text" }}</p>
```
The subrequests support all the same properties as the main request, except for `subrequests` itself, so you can use `headers`, `parameters`, etc.
`(.Subrequest "key")` can be a little cumbersome to write, so you can define a variable to make it easier:
```yaml
template: |
{{ $anotherOne := .Subrequest "another-one" }}
<p>{{ $anotherOne.JSON.String "text" }}</p>
```
You can also access the `.Response` property of a subrequest as you would with the main request:
```yaml
template: |
{{ $anotherOne := .Subrequest "another-one" }}
<p>{{ $anotherOne.Response.StatusCode }}</p>
```
> [!NOTE]
>
> Setting this property will override any query parameters that are already in the URL.