feat(nightscout): add headers property

This commit is contained in:
Ivan 2024-04-19 17:09:00 -04:00 committed by GitHub
parent 8be7e573ae
commit 659a57f013
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 7 deletions

View file

@ -3,6 +3,7 @@ package segments
import (
"encoding/json"
"errors"
http2 "net/http"
"time"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
@ -20,7 +21,8 @@ type Nightscout struct {
const (
// Your complete Nightscout URL and APIKey like this
URL properties.Property = "url"
URL properties.Property = "url"
Headers properties.Property = "headers"
DoubleUpIcon properties.Property = "doubleup_icon"
SingleUpIcon properties.Property = "singleup_icon"
@ -116,7 +118,14 @@ func (ns *Nightscout) getResult() (*NightscoutData, error) {
}
}
body, err := ns.env.HTTPRequest(url, nil, httpTimeout)
headers := ns.props.GetKeyValueMap(Headers, map[string]string{})
modifiers := func(request *http2.Request) {
for key, value := range headers {
request.Header.Add(key, value)
}
}
body, err := ns.env.HTTPRequest(url, nil, httpTimeout, modifiers)
if err != nil {
return nil, err
}

View file

@ -136,6 +136,7 @@ func TestNSSegment(t *testing.T) {
props := properties.Map{
properties.CacheTimeout: tc.CacheTimeout,
URL: "FAKE",
Headers: map[string]string{"Fake-Header": "xxxxx"},
}
cache := &mock.MockedCache{}

View file

@ -4370,6 +4370,12 @@
"title": "Http request timeout",
"description": "Milliseconds to use for http request timeouts",
"default": 500
},
"headers": {
"type": "object",
"title": "Headers",
"description": "A key, value map of Headers to send with the request",
"default": {}
}
}
}

View file

@ -57,11 +57,12 @@ Or display in mmol/l (instead of the default mg/dl) with the following template:
## Properties
| Name | Type | Default | Description |
| --------------- | :------: | :-----: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `url` | `string` | | Your Nightscout URL, including the full path to entries.json AND count=1 AND token. Example above. You'll know this works if you can curl it yourself and get a single value |
| `http_timeout` | `int` | `500` | in milliseconds - how long do you want to wait before you want to see your prompt more than your sugar? I figure a half second is a good default |
| `cache_timeout` | `int` | `5` | in minutes - how long do you want your numbers cached? |
| Name | Type | Default | Description |
| --------------- | :-----------------: | :-----: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `url` | `string` | | Your Nightscout URL, including the full path to entries.json AND count=1 AND token. Example above. You'll know this works if you can curl it yourself and get a single value |
| `headers` | `map[string]string` | | A key, value map of Headers to send with the request |
| `http_timeout` | `int` | `500` | in milliseconds - how long do you want to wait before you want to see your prompt more than your sugar? I figure a half second is a good default |
| `cache_timeout` | `int` | `5` | in minutes - how long do you want your numbers cached? |
:::info
You can change the icons for trend, put the trend elsewhere, add text, however you like!