-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
executable file
·43 lines (35 loc) · 981 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package homeassistant
import (
"context"
"fmt"
"github.com/function61/gokit/net/http/ezhttp"
)
type Client struct {
baseUrl string
authToken string // for non-MQTT requests
}
func NewClient(baseUrl string, authToken string) *Client {
return &Client{baseUrl, authToken}
}
func (c *Client) TextToSpeechGetUrl(ctx context.Context, message string) (string, error) {
req := struct {
Platform string `json:"platform"`
Message string `json:"message"`
}{
Platform: "google_translate", // I don't know why we've to hardcode this (instead of opting for some kind of default)
Message: message,
}
res := struct {
Url string `json:"url"`
}{}
if _, err := ezhttp.Post(
ctx,
c.baseUrl+"/api/tts_get_url",
ezhttp.AuthBearer(c.authToken),
ezhttp.SendJson(&req),
ezhttp.RespondsJsonAllowUnknownFields(&res), // they have a history of adding fields ("path")
); err != nil {
return "", fmt.Errorf("TextToSpeechGetUrl: %w", err)
}
return res.Url, nil
}