diff --git a/internal/api/api.go b/internal/api/api.go index 3b6ccac..f3623e1 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "io" "net/http" "time" @@ -52,6 +53,8 @@ func Listen(parentLogger *slog.Logger) { router.Post("/api/ntfy", postNtfyHandler) + router.Post("/api/command/{commandName}", postCommandHandler) + server = http.Server{ Addr: ":8888", Handler: router, @@ -265,3 +268,36 @@ func postNtfyHandler(w http.ResponseWriter, r *http.Request) { 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 + } +} diff --git a/pkg/ntfy/structs.go b/pkg/ntfy/structs.go index 4dc3eae..d838800 100644 --- a/pkg/ntfy/structs.go +++ b/pkg/ntfy/structs.go @@ -1,5 +1,7 @@ package ntfy +import "encoding/json" + type Message struct { Topic string `json:"topic"` // Required Message string `json:"message,omitempty"` @@ -24,3 +26,27 @@ type Message struct { Extras map[string]string `json:"extras,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) +} diff --git a/pkg/qbittorrent/api.go b/pkg/qbittorrent/api.go index 7f6127f..adc5d54 100644 --- a/pkg/qbittorrent/api.go +++ b/pkg/qbittorrent/api.go @@ -56,6 +56,14 @@ func (c *QbittorrentClient) Login(user string, pass string) error { 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) { resp, err := util.CheckSuccess(c.restClient.R().Get("app/version")) if err != nil {