mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(clients): add whisparr (#218)
* feat(clients): add whisparr * feat: add client connection test
This commit is contained in:
parent
2f358473f3
commit
9d0ab6ea52
11 changed files with 338 additions and 3 deletions
87
pkg/whisparr/client.go
Normal file
87
pkg/whisparr/client.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package whisparr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (*http.Response, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v3/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("whisparr 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("whisparr 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) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v3/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("whisparr 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("whisparr 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("whisparr client request error: %v", reqUrl)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate response
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
log.Error().Err(err).Msgf("whisparr client bad request: %v", reqUrl)
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
log.Error().Err(err).Msgf("whisparr client request error: %v", reqUrl)
|
||||
return nil, errors.New("whisparr: bad request")
|
||||
}
|
||||
|
||||
// return raw response and let the caller handle json unmarshal of body
|
||||
return res, nil
|
||||
}
|
133
pkg/whisparr/whisparr.go
Normal file
133
pkg/whisparr/whisparr.go
Normal file
|
@ -0,0 +1,133 @@
|
|||
package whisparr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Hostname string
|
||||
APIKey string
|
||||
|
||||
// basic auth username and password
|
||||
BasicAuth bool
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
Test() (*SystemStatusResponse, error)
|
||||
Push(release Release) ([]string, error)
|
||||
}
|
||||
|
||||
type client struct {
|
||||
config Config
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func New(config Config) Client {
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 10,
|
||||
}
|
||||
|
||||
c := &client{
|
||||
config: config,
|
||||
http: httpClient,
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
type Release struct {
|
||||
Title string `json:"title"`
|
||||
DownloadUrl string `json:"downloadUrl"`
|
||||
Size int64 `json:"size"`
|
||||
Indexer string `json:"indexer"`
|
||||
DownloadProtocol string `json:"downloadProtocol"`
|
||||
Protocol string `json:"protocol"`
|
||||
PublishDate string `json:"publishDate"`
|
||||
}
|
||||
|
||||
type PushResponse struct {
|
||||
Approved bool `json:"approved"`
|
||||
Rejected bool `json:"rejected"`
|
||||
TempRejected bool `json:"temporarilyRejected"`
|
||||
Rejections []string `json:"rejections"`
|
||||
}
|
||||
|
||||
type SystemStatusResponse struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
res, err := c.get("system/status")
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("whisparr 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("whisparr client error reading body")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response := SystemStatusResponse{}
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("whisparr client error json unmarshal")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Trace().Msgf("whisparr system/status response: %+v", response)
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
res, err := c.post("release/push", release)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("whisparr client post error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if res == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("whisparr client error reading body")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pushResponse := make([]PushResponse, 0)
|
||||
err = json.Unmarshal(body, &pushResponse)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("whisparr client error json unmarshal")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Trace().Msgf("whisparr release/push response body: %+v", string(body))
|
||||
|
||||
// log and return if rejected
|
||||
if pushResponse[0].Rejected {
|
||||
rejections := strings.Join(pushResponse[0].Rejections, ", ")
|
||||
|
||||
log.Trace().Msgf("whisparr push rejected: %s - reasons: %q", release.Title, rejections)
|
||||
return pushResponse[0].Rejections, nil
|
||||
}
|
||||
|
||||
// success true
|
||||
return nil, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue