From ff03883985bca66a3788b77887941c0869dcb08d Mon Sep 17 00:00:00 2001 From: lorenzhohermuth Date: Wed, 6 Nov 2024 16:59:12 +0100 Subject: [PATCH] simple post utils --- .env-example | 2 ++ .gitignore | 1 + go.mod | 1 + go.sum | 2 ++ internal/api/api-specification.go | 13 +++++++++++++ internal/mood/mood.go | 1 + pkg/env/env.go | 21 +++++++++++++++++++++ pkg/http/post.go | 23 +++++++++++++++++++++++ 8 files changed, 64 insertions(+) create mode 100644 .env-example create mode 100644 .gitignore create mode 100644 internal/api/api-specification.go create mode 100644 internal/mood/mood.go create mode 100644 pkg/env/env.go create mode 100644 pkg/http/post.go diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..da04880 --- /dev/null +++ b/.env-example @@ -0,0 +1,2 @@ +URL=your-home-assistant-url with /api/ +TOKEN=you accsess token diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/go.mod b/go.mod index 9e186db..4c0e0b5 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 912390a..60c94fd 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/api/api-specification.go b/internal/api/api-specification.go new file mode 100644 index 0000000..1cba333 --- /dev/null +++ b/internal/api/api-specification.go @@ -0,0 +1,13 @@ +package api + +type ApiPaths struct { + FireEvent string + JsonContentType string +} + +func GetApiSpecifications() ApiPaths { + return ApiPaths{ + FireEvent: "events/", + JsonContentType: "application/json", + } +} diff --git a/internal/mood/mood.go b/internal/mood/mood.go new file mode 100644 index 0000000..453914a --- /dev/null +++ b/internal/mood/mood.go @@ -0,0 +1 @@ +package mood diff --git a/pkg/env/env.go b/pkg/env/env.go new file mode 100644 index 0000000..bae96a2 --- /dev/null +++ b/pkg/env/env.go @@ -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 +} diff --git a/pkg/http/post.go b/pkg/http/post.go new file mode 100644 index 0000000..d1242a2 --- /dev/null +++ b/pkg/http/post.go @@ -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) +}