mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(confg): reload on save and refactor logging (#275)
* feat(confg): reload on save * refactor(logging): rework
This commit is contained in:
parent
198528a474
commit
91b094f4f4
56 changed files with 995 additions and 873 deletions
|
@ -13,7 +13,6 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
|
@ -160,8 +159,7 @@ func (c *client) Do(req *http.Request) (*http.Response, error) {
|
|||
func (c *client) get(url string) (*http.Response, error) {
|
||||
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("ggn client request error : %v", url)
|
||||
return nil, err
|
||||
return nil, errors.New(fmt.Sprintf("ggn client request error : %v", url))
|
||||
}
|
||||
|
||||
req.Header.Add("X-API-Key", c.APIKey)
|
||||
|
@ -169,8 +167,7 @@ func (c *client) get(url string) (*http.Response, error) {
|
|||
|
||||
res, err := c.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("ggn client request error : %v", url)
|
||||
return nil, err
|
||||
return nil, errors.New(fmt.Sprintf("ggn client request error : %v", url))
|
||||
}
|
||||
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
|
|
|
@ -9,8 +9,6 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (int, []byte, error) {
|
||||
|
@ -20,8 +18,7 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
|
||||
req, err := http.NewRequest(http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("lidarr client request error : %v", reqUrl)
|
||||
return 0, nil, err
|
||||
return 0, nil, errors.New(fmt.Sprintf("lidarr client request error : %v", reqUrl))
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -32,7 +29,6 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("lidarr client.get request error: %v", reqUrl)
|
||||
return 0, nil, fmt.Errorf("lidarr.http.Do(req): %w", err)
|
||||
}
|
||||
|
||||
|
@ -53,14 +49,12 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("lidarr client could not marshal data: %v", reqUrl)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("lidarr client could not marshal data: %v", reqUrl)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("lidarr client request error: %v", reqUrl)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("lidarr client request error: %v", reqUrl)
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -73,16 +67,13 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("lidarr client request error: %v", reqUrl)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("lidarr client request error: %v", reqUrl)
|
||||
}
|
||||
|
||||
// validate response
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
log.Error().Err(err).Msgf("lidarr client bad request: %v", reqUrl)
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
return nil, errors.New("lidarr: unauthorized: bad credentials")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
log.Error().Err(err).Msgf("lidarr client request error: %v", reqUrl)
|
||||
return nil, errors.New("lidarr: bad request")
|
||||
}
|
||||
|
||||
|
@ -97,14 +88,12 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("lidarr client could not marshal data: %v", reqUrl)
|
||||
return 0, nil, err
|
||||
return 0, nil, fmt.Errorf("lidarr client could not marshal data: %v", reqUrl)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("lidarr client request error: %v", reqUrl)
|
||||
return 0, nil, err
|
||||
return 0, nil, fmt.Errorf("lidarr client request error: %v", reqUrl)
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -115,7 +104,6 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("lidarr client request error: %v", reqUrl)
|
||||
return 0, nil, fmt.Errorf("lidarr.http.Do(req): %w", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,11 +3,10 @@ package lidarr
|
|||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -69,48 +68,43 @@ type SystemStatusResponse struct {
|
|||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get("system/status")
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client get error")
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("lidarr client get error: %w", err)
|
||||
}
|
||||
|
||||
if status == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
}
|
||||
|
||||
log.Trace().Msgf("lidarr system/status response status: %v body: %v", status, string(res))
|
||||
//log.Trace().Msgf("lidarr system/status response status: %v body: %v", status, string(res))
|
||||
|
||||
response := SystemStatusResponse{}
|
||||
err = json.Unmarshal(res, &response)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client error json unmarshal")
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("lidarr client error json unmarshal: %w", err)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
status, res, err := c.postBody("release/push", release)
|
||||
_, res, err := c.postBody("release/push", release)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client post error")
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("lidarr client post error: %w", err)
|
||||
}
|
||||
|
||||
log.Trace().Msgf("lidarr release/push response status: %v body: %v", status, string(res))
|
||||
//log.Trace().Msgf("lidarr release/push response status: %v body: %v", status, string(res))
|
||||
|
||||
pushResponse := PushResponse{}
|
||||
err = json.Unmarshal(res, &pushResponse)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client error json unmarshal")
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("lidarr client error json unmarshal: %w", err)
|
||||
}
|
||||
|
||||
// log and return if rejected
|
||||
if pushResponse.Rejected {
|
||||
rejections := strings.Join(pushResponse.Rejections, ", ")
|
||||
|
||||
log.Trace().Msgf("lidarr push rejected: %s - reasons: %q", release.Title, rejections)
|
||||
return pushResponse.Rejections, nil
|
||||
return pushResponse.Rejections, fmt.Errorf("lidarr push rejected: %s - reasons: %q: err %w", release.Title, rejections, err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
|
|
@ -12,7 +12,6 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
|
@ -100,8 +99,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
|||
func (c *Client) get(url string) (*http.Response, error) {
|
||||
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("ptp client request error : %v", url)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("ptp client request error : %v", url)
|
||||
}
|
||||
|
||||
req.Header.Add("ApiUser", c.APIUser)
|
||||
|
@ -110,8 +108,7 @@ func (c *Client) get(url string) (*http.Response, error) {
|
|||
|
||||
res, err := c.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("ptp client request error : %v", url)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("ptp client request error : %v", url)
|
||||
}
|
||||
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue