autobrr/internal/http/download_client.go
ze0s 221bc35371
feat(lists): integrate Omegabrr (#1885)
* feat(lists): integrate Omegabrr

* feat(lists): add missing lists index

* feat(lists): add db repo

* feat(lists): add db migrations

* feat(lists): labels

* feat(lists): url lists and more arrs

* fix(lists): db migrations client_id wrong type

* fix(lists): db fields

* feat(lists): create list form wip

* feat(lists): show in list and create

* feat(lists): update and delete

* feat(lists): trigger via webhook

* feat(lists): add webhook handler

* fix(arr): encode json to pointer

* feat(lists): rename endpoint to lists

* feat(lists): fetch tags from arr

* feat(lists): process plaintext lists

* feat(lists): add background refresh job

* run every 6th hour with a random start delay between 1-35 seconds

* feat(lists): refresh on save and improve logging

* feat(lists): cast arr client to pointer

* feat(lists): improve error handling

* feat(lists): reset shows field with match release

* feat(lists): filter opts all lists

* feat(lists): trigger on update if enabled

* feat(lists): update option for lists

* feat(lists): show connected filters in list

* feat(lists): missing listSvc dep

* feat(lists): cleanup

* feat(lists): typo arr list

* feat(lists): radarr include original

* feat(lists): rename ExcludeAlternateTitle to IncludeAlternateTitle

* fix(lists): arr client type conversion to pointer

* fix(actions): only log panic recover if err not nil

* feat(lists): show spinner on save

* feat(lists): show icon in filters list

* feat(lists): change icon color in filters list

* feat(lists): delete relations on filter delete
2024-12-25 13:23:37 +01:00

164 lines
4 KiB
Go

// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package http
import (
"context"
"encoding/json"
"net/http"
"strconv"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/go-chi/chi/v5"
)
type downloadClientService interface {
List(ctx context.Context) ([]domain.DownloadClient, error)
FindByID(ctx context.Context, id int32) (*domain.DownloadClient, error)
Store(ctx context.Context, client *domain.DownloadClient) error
Update(ctx context.Context, client *domain.DownloadClient) error
Delete(ctx context.Context, clientID int32) error
Test(ctx context.Context, client domain.DownloadClient) error
GetArrTags(ctx context.Context, id int32) ([]*domain.ArrTag, error)
}
type downloadClientHandler struct {
encoder encoder
service downloadClientService
}
func newDownloadClientHandler(encoder encoder, service downloadClientService) *downloadClientHandler {
return &downloadClientHandler{
encoder: encoder,
service: service,
}
}
func (h downloadClientHandler) Routes(r chi.Router) {
r.Get("/", h.listDownloadClients)
r.Post("/", h.store)
r.Put("/", h.update)
r.Post("/test", h.test)
r.Route("/{clientID}", func(r chi.Router) {
r.Get("/", h.findByID)
r.Delete("/", h.delete)
r.Get("/arr/tags", h.findArrTagsByID)
})
}
func (h downloadClientHandler) listDownloadClients(w http.ResponseWriter, r *http.Request) {
clients, err := h.service.List(r.Context())
if err != nil {
h.encoder.Error(w, err)
return
}
h.encoder.StatusResponse(w, http.StatusOK, clients)
}
func (h downloadClientHandler) findByID(w http.ResponseWriter, r *http.Request) {
clientID, err := strconv.ParseInt(chi.URLParam(r, "clientID"), 10, 32)
if err != nil {
h.encoder.Error(w, err)
return
}
client, err := h.service.FindByID(r.Context(), int32(clientID))
if err != nil {
if errors.Is(err, domain.ErrRecordNotFound) {
h.encoder.NotFoundErr(w, errors.New("download client with id %d not found", clientID))
}
h.encoder.Error(w, err)
return
}
h.encoder.StatusResponse(w, http.StatusOK, client)
}
func (h downloadClientHandler) findArrTagsByID(w http.ResponseWriter, r *http.Request) {
clientID, err := strconv.ParseInt(chi.URLParam(r, "clientID"), 10, 32)
if err != nil {
h.encoder.Error(w, err)
return
}
client, err := h.service.GetArrTags(r.Context(), int32(clientID))
if err != nil {
if errors.Is(err, domain.ErrRecordNotFound) {
h.encoder.NotFoundErr(w, errors.New("download client with id %d not found", clientID))
}
h.encoder.Error(w, err)
return
}
h.encoder.StatusResponse(w, http.StatusOK, client)
}
func (h downloadClientHandler) store(w http.ResponseWriter, r *http.Request) {
var data *domain.DownloadClient
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
err := h.service.Store(r.Context(), data)
if err != nil {
h.encoder.Error(w, err)
return
}
h.encoder.StatusResponse(w, http.StatusCreated, data)
}
func (h downloadClientHandler) test(w http.ResponseWriter, r *http.Request) {
var data domain.DownloadClient
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
if err := h.service.Test(r.Context(), data); err != nil {
h.encoder.Error(w, err)
return
}
h.encoder.NoContent(w)
}
func (h downloadClientHandler) update(w http.ResponseWriter, r *http.Request) {
var data *domain.DownloadClient
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
err := h.service.Update(r.Context(), data)
if err != nil {
h.encoder.Error(w, err)
return
}
h.encoder.StatusResponse(w, http.StatusCreated, data)
}
func (h downloadClientHandler) delete(w http.ResponseWriter, r *http.Request) {
clientID, err := strconv.ParseInt(chi.URLParam(r, "clientID"), 10, 32)
if err != nil {
h.encoder.Error(w, err)
return
}
if err = h.service.Delete(r.Context(), int32(clientID)); err != nil {
h.encoder.Error(w, err)
return
}
h.encoder.NoContent(w)
}