mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
feat(download-clients): improve errors for starr apps (#250)
* feat(actions): improve errors for starr apps * fix: tests expected error * feat: radarr improve logging * feat: sonarr improve logging and errors * feat: lidarr improve logging and errors
This commit is contained in:
parent
e5a95415a1
commit
3d018404a9
8 changed files with 279 additions and 107 deletions
|
@ -4,6 +4,8 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
@ -11,7 +13,7 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (*http.Response, error) {
|
||||
func (c *client) get(endpoint string) (int, []byte, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
@ -19,27 +21,29 @@ func (c *client) get(endpoint string) (*http.Response, 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 nil, err
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
req.SetBasicAuth(c.config.Username, c.config.Password)
|
||||
}
|
||||
|
||||
req.Header.Add("X-Api-Key", c.config.APIKey)
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
c.setHeaders(req)
|
||||
|
||||
res, err := c.http.Do(req)
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("lidarr client request error : %v", reqUrl)
|
||||
return nil, err
|
||||
log.Error().Err(err).Msgf("lidarr client.get request error: %v", reqUrl)
|
||||
return 0, nil, fmt.Errorf("lidarr.http.Do(req): %w", err)
|
||||
}
|
||||
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
defer resp.Body.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err = io.Copy(&buf, resp.Body); err != nil {
|
||||
return resp.StatusCode, nil, fmt.Errorf("lidarr.io.Copy: %w", err)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *client) post(endpoint string, data interface{}) (*http.Response, error) {
|
||||
|
@ -85,3 +89,56 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
// return raw response and let the caller handle json unmarshal of body
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
req.SetBasicAuth(c.config.Username, c.config.Password)
|
||||
}
|
||||
|
||||
c.setHeaders(req)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err = io.Copy(&buf, resp.Body); err != nil {
|
||||
return resp.StatusCode, nil, fmt.Errorf("lidarr.io.Copy: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return resp.StatusCode, buf.Bytes(), fmt.Errorf("lidarr: bad request: %v (status: %s): %s", resp.Request.RequestURI, resp.Status, buf.String())
|
||||
}
|
||||
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *client) setHeaders(req *http.Request) {
|
||||
if req.Body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
req.Header.Set("X-Api-Key", c.config.APIKey)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package lidarr
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -67,60 +67,44 @@ type SystemStatusResponse struct {
|
|||
}
|
||||
|
||||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
res, err := c.get("system/status")
|
||||
status, res, err := c.get("system/status")
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client get error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client error reading body")
|
||||
return nil, 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))
|
||||
|
||||
response := SystemStatusResponse{}
|
||||
err = json.Unmarshal(body, &response)
|
||||
err = json.Unmarshal(res, &response)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client error json unmarshal")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Trace().Msgf("lidarr system/status response: %+v", response)
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
res, err := c.post("release/push", release)
|
||||
status, res, err := c.postBody("release/push", release)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client post error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if res == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client error reading body")
|
||||
return nil, err
|
||||
}
|
||||
log.Trace().Msgf("lidarr release/push response status: %v body: %v", status, string(res))
|
||||
|
||||
pushResponse := PushResponse{}
|
||||
err = json.Unmarshal(body, &pushResponse)
|
||||
err = json.Unmarshal(res, &pushResponse)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("lidarr client error json unmarshal")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Trace().Msgf("lidarr release/push response body: %+v", string(body))
|
||||
|
||||
// log and return if rejected
|
||||
if pushResponse.Rejected {
|
||||
rejections := strings.Join(pushResponse.Rejections, ", ")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue