Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds JSON API segment #5842

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
HTTP SegmentType = "http"
// 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{} },
HTTP: func() SegmentWriter { return &segments.HTTP{} },
JULIA: func() SegmentWriter { return &segments.Julia{} },
KOTLIN: func() SegmentWriter { return &segments.Kotlin{} },
KUBECTL: func() SegmentWriter { return &segments.Kubectl{} },
Expand Down
60 changes: 60 additions & 0 deletions src/segments/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package segments

import (
"encoding/json"

"net/http"

"github.com/jandedobbeleer/oh-my-posh/src/properties"
)

type HTTP struct {
base

URL string
Result map[string]interface{}
}

const (
HTTPURL properties.Property = "url"
METHOD properties.Property = "method"
)

func (cs *HTTP) Template() string {
return " {{ .Result }} "
}

func (cs *HTTP) Enabled() bool {
url := cs.props.GetString(HTTPURL, "")
if len(url) == 0 {
return false
}
method := cs.props.GetString(METHOD, "GET")

result, err := cs.getResult(url, method)
if err != nil {
return false
}

cs.Result = result
return true
}

func (cs *HTTP) getResult(url, method string) (map[string]interface{}, error) {
setMethod := func(request *http.Request) {
request.Method = method
}

resultBody, err := cs.env.HTTPRequest(url, nil, 10000, setMethod)
if err != nil {
return nil, err
}

var result map[string]interface{}
err = json.Unmarshal(resultBody, &result)
if err != nil {
return nil, err
}

return result, nil
}
111 changes: 111 additions & 0 deletions src/segments/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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 TestHTTPSegmentEnabled(t *testing.T) {
cases := []struct {
caseName string
url string
method string
response string
responseID string
isError bool
expected bool
}{
{
caseName: "Valid URL with GET response",
url: "https://jsonplaceholder.typicode.com/posts/1",
method: "GET",
response: `{"id": "1"}`,
responseID: "1",
isError: false,
expected: true,
},
{
caseName: "Valid URL with POST response",
url: "https://jsonplaceholder.typicode.com/posts",
method: "POST",
response: `{"id": "101"}`,
responseID: "101",
isError: false,
expected: true,
},
{
caseName: "Valid URL with error response",
url: "https://api.example.com/data",
method: "GET",
response: ``,
responseID: ``,
isError: true,
expected: false,
},
{
caseName: "Empty URL",
url: "",
method: "GET",
response: ``,
responseID: ``,
isError: false,
expected: false,
},
}

for _, tc := range cases {
t.Run(tc.caseName, func(t *testing.T) {
env := new(mock.Environment)
props := properties.Map{
URL: tc.url,
METHOD: tc.method,
}

env.On("HTTPRequest", tc.url).Return([]byte(tc.response), func() error {
if tc.isError {
return errors.New("error")
}
return nil
}())

cs := &HTTP{
base: base{
env: env,
props: props,
},
}

enabled := cs.Enabled()
assert.Equal(t, tc.expected, enabled)
if enabled {
assert.Equal(t, tc.responseID, cs.Result["id"])
}
})
}
}

func TestHTTPSegmentTemplate(t *testing.T) {
env := new(mock.Environment)
props := properties.Map{
URL: "https://jsonplaceholder.typicode.com/posts/1",
}

env.On("HTTPRequest", "https://jsonplaceholder.typicode.com/posts/1").Return([]byte(`{"key": "value"}`), nil)

cs := &HTTP{
base: base{
env: env,
props: props,
},
}

cs.Enabled()
template := cs.Template()
expectedTemplate := " {{ .Result }} "
assert.Equal(t, expectedTemplate, template)
}
32 changes: 32 additions & 0 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@
"go",
"haskell",
"helm",
"http",
"ipify",
"julia",
"java",
Expand Down Expand Up @@ -3736,6 +3737,37 @@
}
}
},
{
"if": {
"properties": {
"type": {
"const": "http"
}
}
},
"then": {
"title": "Display your any JSON property from any API",
"description": "https://ohmyposh.dev/docs/segments/web/http",
"properties": {
"properties": {
"properties": {
"url": {
"type": "string",
"title": "URL",
"description": "The HTTP URL",
"default": "https://jsonplaceholder.typicode.com/posts/1"
},
"method": {
"type": "string",
"title": "HTTP method",
"description": "The HTTP method",
"default": "GET"
}
}
}
}
}
},
{
"if": {
"properties": {
Expand Down
54 changes: 54 additions & 0 deletions website/docs/segments/web/http.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
id: http
title: HTTP Request
sidebar_label: HTTP Request
---

## What

HTTP Request is a simple segment to return any json data from any (anonymous) HTTP call.

## Sample Configuration

import Config from "@site/src/components/Config.js";

<Config
data={{
type: "http",
style: "diamond",
foreground: "#ffffff",
background: "#c386f1",
leading_diamond: "\ue0b6",
trailing_diamond: "\uE0B0",
template: "{{ .Result }}",
properties: {
url: "https://jsonplaceholder.typicode.com/posts/1",
method: "GET",
},
}}
/>

## Properties

| Name | Type | Default | Description |
| -------- | :------: | :--------------------------------------------: | ----------------------------- |
| `url` | `string` | `https://jsonplaceholder.typicode.com/posts/1` | The HTTP URL you want to call |
| `method` | `string` | `GET` | The HTTP method 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/http",
"segments/web/nba",
"segments/web/owm",
"segments/web/wakatime",
Expand Down