Repository: JanDeDobbeleer/oh-my-posh
Pull request #4856 (2024-04-19 20:49:40)

feat(Nightscout): Added Headers Property
### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md].
- [x] The commit message follows the [conventional commits][cc] guidelines.
- [x] Tests for the changes have been added (for bug fixes / features).
- [x] Docs have been added/updated (for bug fixes / features).

### Description

Nightscout is an incredible tool that allows diabetics the ability to access their glucose data through this tool.

Primarily it's used as a service that has an endpoint you can hit to retrieve the JSON data that is displayed in the Nightscout segment.

Nightscout can be self-hosted, which is what I'm currently doing. To access my secure JSON endpoint, I need to pass a modifier to update the headers of the HTTP request.

### Work Done

- Added Headers property
  - Added subsequent test, doc, and schema update

<!---

Tips:

If you're not comfortable with working with Git,
we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/segments/nightscout.go b/src/segments/nightscout.go
index b182be9b..fe1faba0 100644
--- a/src/segments/nightscout.go
+++ b/src/segments/nightscout.go
@@ -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
 	}
diff --git a/themes/schema.json b/themes/schema.json
index 931c2673..fe2040e1 100644
--- a/themes/schema.json
+++ b/themes/schema.json
@@ -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": {}
                   }
                 }
               }
diff --git a/website/docs/segments/nightscout.mdx b/website/docs/segments/nightscout.mdx
index ad8e09f0..b357352a 100644
--- a/website/docs/segments/nightscout.mdx
+++ b/website/docs/segments/nightscout.mdx
@@ -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!


Test patch:
diff --git a/src/segments/nightscout_test.go b/src/segments/nightscout_test.go
index 29bb6686..1cf8a336 100644
--- a/src/segments/nightscout_test.go
+++ b/src/segments/nightscout_test.go
@@ -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{}