1
0
Fork 0

Command API, Tasker/ntfy structs

main v0.10.0
Jordan Hotmann 2023-11-13 14:54:07 -07:00
parent 2685228c40
commit 6f1e9f4a75
No known key found for this signature in database
GPG Key ID: 01B504170C2A2EA3
3 changed files with 70 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package api
import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
"time" "time"
@ -52,6 +53,8 @@ func Listen(parentLogger *slog.Logger) {
router.Post("/api/ntfy", postNtfyHandler) router.Post("/api/ntfy", postNtfyHandler)
router.Post("/api/command/{commandName}", postCommandHandler)
server = http.Server{ server = http.Server{
Addr: ":8888", Addr: ":8888",
Handler: router, Handler: router,
@ -265,3 +268,36 @@ func postNtfyHandler(w http.ResponseWriter, r *http.Request) {
render.PlainText(w, r, "OK") render.PlainText(w, r, "OK")
} }
// Command
func postCommandHandler(w http.ResponseWriter, r *http.Request) {
commandName := chi.URLParam(r, "commandName")
logRequest(w, r)
switch commandName {
// Commands without payloads
case "bedtime":
nats.PublishString(fmt.Sprintf("command.%s", commandName), "called")
render.PlainText(w, r, "OK")
return
// Commands with payloads
case "sonarr":
case "radarr":
case "paupdate":
body, err := io.ReadAll(r.Body)
if err != nil {
logger.Error("Error reading request body", "error", err, "url", r.URL.String())
http.Error(w, "Unable to read body", http.StatusBadRequest)
return
}
nats.Publish(fmt.Sprintf("command.%s", commandName), body)
render.PlainText(w, r, "OK")
return
// Otherwise
default:
logger.Error("Command not implemented", "commandName", commandName)
http.Error(w, "Command not implemented", http.StatusNotFound)
return
}
}

View File

@ -1,5 +1,7 @@
package ntfy package ntfy
import "encoding/json"
type Message struct { type Message struct {
Topic string `json:"topic"` // Required Topic string `json:"topic"` // Required
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
@ -24,3 +26,27 @@ type Message struct {
Extras map[string]string `json:"extras,omitempty"` Extras map[string]string `json:"extras,omitempty"`
} `json:"actions,omitempty"` } `json:"actions,omitempty"`
} }
type RunOptions struct {
TaskName string `json:"taskName"`
Par1 string `json:"par1,omitempty"`
Par2 string `json:"par2,omitempty"`
Priority int `json:"priority,omitempty"`
}
type VarOptions struct {
Name string `json:"name"`
Value string `json:"value"`
}
type TaskerApi struct {
Run []RunOptions `json:"run,omitempty"`
Set []VarOptions `json:"set,omitempty"`
Enable []string `json:"enable,omitempty"`
Disable []string `json:"disable,omitempty"`
}
func (t *TaskerApi) ToString() string {
b, _ := json.Marshal(t)
return string(b)
}

View File

@ -56,6 +56,14 @@ func (c *QbittorrentClient) Login(user string, pass string) error {
return nil return nil
} }
func (c *QbittorrentClient) Logout() error {
_, err := util.CheckSuccess(c.restClient.R().Get("auth/logout"))
c.restClient.SetCookies([]*http.Cookie{})
return err
}
func (c *QbittorrentClient) GetVersion() (string, error) { func (c *QbittorrentClient) GetVersion() (string, error) {
resp, err := util.CheckSuccess(c.restClient.R().Get("app/version")) resp, err := util.CheckSuccess(c.restClient.R().Get("app/version"))
if err != nil { if err != nil {