mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
Feature: Sonarr (#14)
* feat: add sonarr download client * feat(web): add sonarr download client and actions * feat: add sonarr to filter actions
This commit is contained in:
parent
455284a94b
commit
fce6c7149a
15 changed files with 594 additions and 5 deletions
82
pkg/sonarr/client.go
Normal file
82
pkg/sonarr/client.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package sonarr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (*http.Response, error) {
|
||||
reqUrl := fmt.Sprintf("%v/api/v3/%v", c.config.Hostname, endpoint)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("sonarr client request error : %v", reqUrl)
|
||||
return 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")
|
||||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("sonarr client request error : %v", reqUrl)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *client) post(endpoint string, data interface{}) (*http.Response, error) {
|
||||
reqUrl := fmt.Sprintf("%v/api/v3/%v", c.config.Hostname, endpoint)
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("sonarr client could not marshal data: %v", reqUrl)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("sonarr client request error: %v", reqUrl)
|
||||
return 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("Content-Type", "application/json; charset=UTF-8")
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("sonarr client request error: %v", reqUrl)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate response
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
log.Error().Err(err).Msgf("sonarr client bad request: %v", reqUrl)
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
log.Error().Err(err).Msgf("sonarr client request error: %v", reqUrl)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// return raw response and let the caller handle json unmarshal of body
|
||||
return res, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue