-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86291dc
commit 277c1d8
Showing
6 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package segments | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/jandedobbeleer/oh-my-posh/src/properties" | ||
) | ||
|
||
type JSONAPI struct { | ||
base | ||
|
||
URL string | ||
Result map[string]interface{} | ||
} | ||
|
||
const ( | ||
JSONAPIURL properties.Property = "api_url" | ||
) | ||
|
||
func (cs *JSONAPI) Template() string { | ||
return " {{ .Result }} " | ||
} | ||
|
||
func (cs *JSONAPI) Enabled() bool { | ||
url := cs.props.GetString(JSONAPIURL, "") | ||
if len(url) == 0 { | ||
return false | ||
} | ||
|
||
result, err := cs.getResult(url) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
cs.Result = result | ||
return true | ||
} | ||
|
||
func (cs *JSONAPI) getResult(url string) (map[string]interface{}, error) { | ||
body, err := cs.env.HTTPRequest(url, nil, 10000) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result map[string]interface{} | ||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package segments | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/jandedobbeleer/oh-my-posh/src/properties" | ||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJSONAPISegmentEnabled(t *testing.T) { | ||
cases := []struct { | ||
caseName string | ||
url string | ||
response string | ||
isError bool | ||
expected bool | ||
}{ | ||
{ | ||
caseName: "Valid URL with response", | ||
url: "https://jsonplaceholder.typicode.com/posts/1", | ||
response: `{"id": "1"}`, | ||
isError: false, | ||
expected: true, | ||
}, | ||
{ | ||
caseName: "Valid URL with error response", | ||
url: "https://api.example.com/data", | ||
response: ``, | ||
isError: true, | ||
expected: false, | ||
}, | ||
{ | ||
caseName: "Empty URL", | ||
url: "", | ||
response: ``, | ||
isError: false, | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.caseName, func(t *testing.T) { | ||
env := new(mock.Environment) | ||
props := properties.Map{ | ||
JSONAPIURL: tc.url, | ||
} | ||
|
||
env.On("HTTPRequest", tc.url).Return([]byte(tc.response), func() error { | ||
if tc.isError { | ||
return errors.New("error") | ||
} | ||
return nil | ||
}()) | ||
|
||
cs := &JSONAPI{ | ||
base: base{ | ||
env: env, | ||
props: props, | ||
}, | ||
} | ||
|
||
enabled := cs.Enabled() | ||
assert.Equal(t, tc.expected, enabled) | ||
}) | ||
} | ||
} | ||
|
||
func TestJSONAPISegmentTemplate(t *testing.T) { | ||
env := new(mock.Environment) | ||
props := properties.Map{ | ||
JSONAPIURL: "https://jsonplaceholder.typicode.com/posts/1", | ||
} | ||
|
||
env.On("HTTPRequest", "https://jsonplaceholder.typicode.com/posts/1").Return([]byte(`{"key": "value"}`), nil) | ||
|
||
cs := &JSONAPI{ | ||
base: base{ | ||
env: env, | ||
props: props, | ||
}, | ||
} | ||
|
||
cs.Enabled() | ||
template := cs.Template() | ||
expectedTemplate := " {{ .Result }} " | ||
assert.Equal(t, expectedTemplate, template) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
id: jsonapi | ||
title: JSON API | ||
sidebar_label: JSON API | ||
--- | ||
|
||
## What | ||
|
||
JSON API is a simple segment to return any json data from any (anonymous) API. | ||
|
||
## Sample Configuration | ||
|
||
import Config from "@site/src/components/Config.js"; | ||
|
||
<Config | ||
data={{ | ||
type: "jsonapi", | ||
style: "diamond", | ||
foreground: "#ffffff", | ||
background: "#c386f1", | ||
leading_diamond: "\ue0b6", | ||
trailing_diamond: "\uE0B0", | ||
template: "{{ .Result }}", | ||
properties: { | ||
api_url: "https://jsonplaceholder.typicode.com/posts/1", | ||
}, | ||
}} | ||
/> | ||
|
||
## Properties | ||
|
||
| Name | Type | Default | Description | | ||
| --------- | :------: | :--------------------------------------------: | -------------------------------- | | ||
| `api_url` | `string` | `https://jsonplaceholder.typicode.com/posts/1` | The JSON API URL you want to use | | ||
|
||
## Template ([info][templates]) | ||
|
||
:::note default template | ||
|
||
```template | ||
{{ .Result }} | ||
``` | ||
|
||
::: | ||
|
||
### Properties | ||
|
||
| Name | Type | Description | | ||
| ----------------------- | -------- | ------------------------------------------------------------------ | | ||
| .Result.[json_property] | `string` | Replace [json_property] with the json property you want to display | | ||
|
||
[templates]: /docs/configuration/templates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters