autobrr/pkg/whisparr/client.go
Ludvig Lundgren 0e88117702
feat(logging); improve messages and errors (#336)
* feat(logger): add module context

* feat(logger): change errors package

* feat(logger): update tests
2022-07-05 13:31:44 +02:00

79 lines
2 KiB
Go

package whisparr
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"path"
"github.com/autobrr/autobrr/pkg/errors"
)
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 {
return nil, errors.Wrap(err, "could not build request")
}
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 {
return nil, errors.Wrap(err, "could not make request: %+v", req)
}
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 {
return nil, errors.Wrap(err, "could not marshal data: %+v", data)
}
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
if err != nil {
return nil, errors.Wrap(err, "could not build request")
}
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 nil, errors.Wrap(err, "could not make request: %+v", req)
}
// validate response
if res.StatusCode == http.StatusUnauthorized {
return nil, errors.New("unauthorized: bad credentials")
} else if res.StatusCode != http.StatusOK {
return nil, errors.New("whisparr: bad request")
}
// return raw response and let the caller handle json unmarshal of body
return res, nil
}