mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-15 04:12:02 -06:00
good progress yay:
- impl http client - create http client in App() - generic builder for Service.* - set Service on app to pass to callbacks later - impl State - set State on app to pass to callbacks later - change panic to log.Fatalln
This commit is contained in:
@@ -1,6 +1,83 @@
|
||||
// http is used to interact with the home assistant
|
||||
// REST API, currently only for retrieving state for
|
||||
// REST API. Currently only used to retrieve state for
|
||||
// a single entity_id
|
||||
package http
|
||||
|
||||
// TODO: impl http struct, should be initialized as part of App()
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type HttpClient struct {
|
||||
url string
|
||||
token string
|
||||
}
|
||||
|
||||
func NewHttpClient(url, token string) *HttpClient {
|
||||
url = fmt.Sprintf("http://%s/api", url)
|
||||
return &HttpClient{url, token}
|
||||
}
|
||||
|
||||
func (c *HttpClient) GetState(entityId string) ([]byte, error) {
|
||||
resp, err := get(c.url+"/states/"+entityId, c.token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func get(url, token string) ([]byte, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, errors.New("Error creating HTTP request: " + err.Error())
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.New("Error on response.\n[ERROR] -" + err.Error())
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, errors.New("Error while reading the response bytes:" + err.Error())
|
||||
}
|
||||
|
||||
return body, nil
|
||||
}
|
||||
|
||||
// func post(url string, token string, data any) ([]byte, error) {
|
||||
// postBody, err := json.Marshal(data)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// req, err := http.NewRequest("GET", url, bytes.NewBuffer(postBody))
|
||||
// if err != nil {
|
||||
// return nil, errors.New("Error building post request: " + err.Error())
|
||||
// }
|
||||
|
||||
// req.Header.Add("Authorization", "Bearer "+token)
|
||||
|
||||
// client := &http.Client{}
|
||||
// resp, err := client.Do(req)
|
||||
// if err != nil {
|
||||
// return nil, errors.New("Error in post response: " + err.Error())
|
||||
// }
|
||||
// defer resp.Body.Close()
|
||||
|
||||
// if resp.StatusCode == 401 {
|
||||
// log.Fatalln("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile")
|
||||
// }
|
||||
|
||||
// body, err := io.ReadAll(resp.Body)
|
||||
// if err != nil {
|
||||
// log.Fatalln(err)
|
||||
// }
|
||||
|
||||
// return body, nil
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user