autobrr/pkg/arr/lidarr/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

198 lines
5.1 KiB
Go

// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package lidarr
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"path"
"github.com/autobrr/autobrr/pkg/errors"
)
func (c *Client) get(ctx context.Context, endpoint string) (int, []byte, error) {
u, err := url.Parse(c.config.Hostname)
if err != nil {
return 0, nil, errors.Wrap(err, "could not parse url: %s", c.config.Hostname)
}
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
reqUrl := u.String()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, http.NoBody)
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr Client request error : %v", reqUrl)
}
if c.config.BasicAuth {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
c.setHeaders(req)
resp, err := c.http.Do(req)
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr.http.Do(req)")
}
defer resp.Body.Close()
if resp.Body == nil {
return resp.StatusCode, nil, errors.New("response body is nil")
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, resp.Body); err != nil {
return resp.StatusCode, nil, errors.Wrap(err, "lidarr.io.Copy error")
}
return resp.StatusCode, buf.Bytes(), nil
}
func (c *Client) getJSON(ctx context.Context, endpoint string, params url.Values, data any) error {
u, err := url.Parse(c.config.Hostname)
if err != nil {
return errors.Wrap(err, "could not parse url: %s", c.config.Hostname)
}
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
reqUrl := u.String()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, http.NoBody)
if err != nil {
return errors.Wrap(err, "could not build request")
}
if c.config.BasicAuth {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
c.setHeaders(req)
req.URL.RawQuery = params.Encode()
resp, err := c.http.Do(req)
if err != nil {
return errors.Wrap(err, "lidarr.http.Do(req): %+v", req)
}
defer resp.Body.Close()
if resp.Body == nil {
return errors.New("response body is nil")
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return errors.Wrap(err, "could not unmarshal data")
}
return nil
}
func (c *Client) post(ctx context.Context, endpoint string, data interface{}) (*http.Response, error) {
u, err := url.Parse(c.config.Hostname)
if err != nil {
return nil, errors.Wrap(err, "could not parse url: %s", c.config.Hostname)
}
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
reqUrl := u.String()
jsonData, err := json.Marshal(data)
if err != nil {
return nil, errors.Wrap(err, "lidarr Client could not marshal data: %v", reqUrl)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
if err != nil {
return nil, errors.Wrap(err, "lidarr Client request error: %v", reqUrl)
}
if c.config.BasicAuth {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
req.Header.Add("X-Api-Key", c.config.APIKey)
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
req.Header.Set("User-Agent", "autobrr")
res, err := c.http.Do(req)
if err != nil {
return res, errors.Wrap(err, "lidarr Client request error: %v", reqUrl)
}
// validate response
if res.StatusCode == http.StatusUnauthorized {
return res, errors.New("lidarr: unauthorized: bad credentials")
} else if res.StatusCode != http.StatusOK {
return res, errors.New("lidarr: bad request")
}
// return raw response and let the caller handle json unmarshal of body
return res, nil
}
func (c *Client) postBody(ctx context.Context, endpoint string, data interface{}) (int, []byte, error) {
u, err := url.Parse(c.config.Hostname)
if err != nil {
return 0, nil, errors.Wrap(err, "could not parse url: %s", c.config.Hostname)
}
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
reqUrl := u.String()
jsonData, err := json.Marshal(data)
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr Client could not marshal data: %v", reqUrl)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr Client request error: %v", reqUrl)
}
if c.config.BasicAuth {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
c.setHeaders(req)
resp, err := c.http.Do(req)
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr.http.Do(req)")
}
defer resp.Body.Close()
if resp.Body == nil {
return resp.StatusCode, nil, errors.New("response body is nil")
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, resp.Body); err != nil {
return resp.StatusCode, nil, errors.Wrap(err, "lidarr.io.Copy")
}
if resp.StatusCode == http.StatusBadRequest {
return resp.StatusCode, buf.Bytes(), nil
} else if resp.StatusCode < 200 || resp.StatusCode > 401 {
return resp.StatusCode, buf.Bytes(), errors.New("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)
}