Skip to content

Commit

Permalink
add json api segment
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Wiedemeyer authored and mwiedemeyer committed Nov 7, 2024
1 parent 86291dc commit 277c1d8
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/config/segment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ const (
IPIFY SegmentType = "ipify"
// JAVA writes the active java version
JAVA SegmentType = "java"
// API writes the output of a custom JSON API
JSONAPI SegmentType = "jsonapi"
// JULIA writes which julia version is currently active
JULIA SegmentType = "julia"
// KOTLIN writes the active kotlin version
Expand Down Expand Up @@ -259,6 +261,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
HELM: func() SegmentWriter { return &segments.Helm{} },
IPIFY: func() SegmentWriter { return &segments.IPify{} },
JAVA: func() SegmentWriter { return &segments.Java{} },
JSONAPI: func() SegmentWriter { return &segments.JSONAPI{} },
JULIA: func() SegmentWriter { return &segments.Julia{} },
KOTLIN: func() SegmentWriter { return &segments.Kotlin{} },
KUBECTL: func() SegmentWriter { return &segments.Kubectl{} },
Expand Down
52 changes: 52 additions & 0 deletions src/segments/jsonapi.go
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
}
90 changes: 90 additions & 0 deletions src/segments/jsonapi_test.go
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)
}
26 changes: 26 additions & 0 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@
"haskell",
"helm",
"ipify",
"jsonapi",
"julia",
"java",
"kotlin",
Expand Down Expand Up @@ -3736,6 +3737,31 @@
}
}
},
{
"if": {
"properties": {
"type": {
"const": "jsonapi"
}
}
},
"then": {
"title": "Display your any JSON property from any API",
"description": "https://ohmyposh.dev/docs/segments/web/jsonapi",
"properties": {
"properties": {
"properties": {
"api_url": {
"type": "string",
"title": "API URL",
"description": "The JSON API URL",
"default": "https://jsonplaceholder.typicode.com/posts/1"
}
}
}
}
}
},
{
"if": {
"properties": {
Expand Down
52 changes: 52 additions & 0 deletions website/docs/segments/web/jsonapi.mdx
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
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ module.exports = {
"segments/web/brewfather",
"segments/web/carbonintensity",
"segments/web/ipify",
"segments/web/jsonapi",
"segments/web/nba",
"segments/web/owm",
"segments/web/wakatime",
Expand Down

0 comments on commit 277c1d8

Please sign in to comment.