joshuar-go-hass-agent/internal/hass/api.go

117 lines
3.0 KiB
Go

// Copyright (c) 2023 Joshua Rich <joshua.rich@gmail.com>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package hass
import (
"bytes"
"context"
"encoding/json"
"errors"
"time"
"github.com/carlmjohnson/requests"
"github.com/joshuar/go-hass-agent/internal/config"
"github.com/rs/zerolog/log"
)
//go:generate stringer -type=RequestType -output apiRequestType.go -linecomment
const (
requestTypeEncrypted RequestType = iota + 1 // encrypted
requestTypeGetConfig // get_config
requestTypeUpdateLocation // update_location
RequestTypeRegisterSensor // register_sensor
RequestTypeUpdateSensorStates // update_sensor_states
)
type RequestType int
type Request interface {
RequestType() RequestType
RequestData() *json.RawMessage
ResponseHandler(bytes.Buffer)
}
func MarshalJSON(request Request, secret string) ([]byte, error) {
if request.RequestType() == requestTypeEncrypted {
if secret != "" {
return json.Marshal(&struct {
EncryptedData *json.RawMessage `json:"encrypted_data,omitempty"`
Type string `json:"type"`
Encrypted bool `json:"encrypted"`
}{
Type: requestTypeEncrypted.String(),
Encrypted: true,
EncryptedData: request.RequestData(),
})
} else {
return nil, errors.New("encrypted request recieved without secret")
}
} else {
return json.Marshal(&struct {
Data *json.RawMessage `json:"data,omitempty"`
Type string `json:"type"`
}{
Type: request.RequestType().String(),
Data: request.RequestData(),
})
}
}
type UnencryptedRequest struct {
Data *json.RawMessage `json:"data,omitempty"`
Type RequestType `json:"type"`
}
type EncryptedRequest struct {
EncryptedData *json.RawMessage `json:"encrypted_data,omitempty"`
Type RequestType `json:"type"`
Encrypted bool `json:"encrypted"`
}
type Response struct {
Success bool `json:"success,omitempty"`
}
func APIRequest(ctx context.Context, request Request) {
var res bytes.Buffer
secret, err := config.FetchPropertyFromContext(ctx, "secret")
if err != nil {
log.Error().Stack().Err(err).
Msg("Could not fetch secret from agent config.")
return
}
url, err := config.FetchPropertyFromContext(ctx, "apiURL")
if err != nil {
log.Error().Stack().Err(err).
Msg("Could not fetch api url from agent config.")
return
}
reqJson, err := MarshalJSON(request, secret.(string))
if err != nil {
log.Error().Stack().Err(err).
Msg("Unable to format request")
return
}
requestCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
err = requests.
URL(url.(string)).
BodyBytes(reqJson).
ToBytesBuffer(&res).
Fetch(requestCtx)
if err != nil {
log.Error().Stack().Err(err).
Msgf("Unable to send request with body:\n\t%s\n\t", reqJson)
cancel()
return
}
request.ResponseHandler(res)
}