simple post utils

This commit is contained in:
Lorenz Hohermuth 2024-11-06 16:59:12 +01:00
parent 48b6397731
commit ff03883985
8 changed files with 64 additions and 0 deletions

2
.env-example Normal file
View File

@ -0,0 +1,2 @@
URL=your-home-assistant-url with /api/
TOKEN=you accsess token

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env

1
go.mod
View File

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

2
go.sum
View File

@ -1,4 +1,6 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/gofor-little/env v1.0.18 h1:k87nb3OjhiZyq2mmNbuW9Hvm/vqUszNPe1C8HPb9etM=
github.com/gofor-little/env v1.0.18/go.mod h1:2BE2i6c9e/C6EaGnfhpqzfNERUqkzJ+s/ApnRyl+588=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

View File

@ -0,0 +1,13 @@
package api
type ApiPaths struct {
FireEvent string
JsonContentType string
}
func GetApiSpecifications() ApiPaths {
return ApiPaths{
FireEvent: "events/",
JsonContentType: "application/json",
}
}

1
internal/mood/mood.go Normal file
View File

@ -0,0 +1 @@
package mood

21
pkg/env/env.go vendored Normal file
View File

@ -0,0 +1,21 @@
package env
import (
"github.com/gofor-little/env"
)
func GetUrl() string {
url, err := env.MustGet("URL")
if err != nil {
panic(err)
}
return url
}
func GetToken() string {
token, err := env.MustGet("TOKEN")
if err != nil {
panic(err)
}
return token
}

23
pkg/http/post.go Normal file
View File

@ -0,0 +1,23 @@
package http
import (
"fmt"
"net/http"
"strings"
"git.lorenzzz.dev/lorenzhohermuth/home-assistant/internal/api"
"git.lorenzzz.dev/lorenzhohermuth/home-assistant/pkg/env"
)
func FireEvent(event string, eventBody string) {
reader := strings.NewReader(eventBody)
apiSpecs := api.GetApiSpecifications()
url := env.GetUrl() + apiSpecs.FireEvent + event
response, err := http.Post(url, apiSpecs.JsonContentType, reader)
if err != nil {
panic(err)
}
fmt.Println(response)
}