first working request

This commit is contained in:
Lorenz Hohermuth 2024-11-24 19:04:48 +01:00
parent ff03883985
commit 8c268d9342
5 changed files with 47 additions and 9 deletions

View File

@ -1,17 +1,16 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"os"
"git.lorenzzz.dev/lorenzhohermuth/home-assistant/internal/mood"
"git.lorenzzz.dev/lorenzhohermuth/home-assistant/pkg/env"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "home-assistant",
@ -24,7 +23,11 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Run: func(cmd *cobra.Command, args []string) {
env.EnvLoadFile()
mood.SetMood(mood.GetMoods()[0])
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
@ -47,5 +50,3 @@ func init() {
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

7
go.mod
View File

@ -3,8 +3,11 @@ module git.lorenzzz.dev/lorenzhohermuth/home-assistant
go 1.22.5
require (
github.com/gofor-little/env v1.0.18 // indirect
github.com/gofor-little/env v1.0.18
github.com/spf13/cobra v1.8.1
)
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)

View File

@ -1 +1,23 @@
package mood
import (
"fmt"
"git.lorenzzz.dev/lorenzhohermuth/home-assistant/pkg/http"
)
var key = "mood"
var moods = []string{"relax"}
var eventName = "SET_MOOD_EVENT"
func createBody(mood string) string {
return fmt.Sprintf("{\"%s\": \"%s\"}", key, mood)
}
func GetMoods() []string {
return moods
}
func SetMood(mood string) {
http.FireEvent(eventName, createBody(mood))
}

8
pkg/env/env.go vendored
View File

@ -4,6 +4,14 @@ import (
"github.com/gofor-little/env"
)
func EnvLoadFile() {
err := env.Load(".env")
if err != nil {
panic(err)
}
}
func GetUrl() string {
url, err := env.MustGet("URL")
if err != nil {

View File

@ -15,7 +15,11 @@ func FireEvent(event string, eventBody string) {
apiSpecs := api.GetApiSpecifications()
url := env.GetUrl() + apiSpecs.FireEvent + event
response, err := http.Post(url, apiSpecs.JsonContentType, reader)
req, err := http.NewRequest("POST", url, reader)
req.Header.Add("Authorization", "Bearer "+env.GetToken())
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
panic(err)
}