mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(actions): simplify macro parsing (#560)
* refactor(action): parse macros * feat(action): add ctx to arr clients and test
This commit is contained in:
parent
f6e68fae2b
commit
839eb9f3f3
31 changed files with 323 additions and 334 deletions
|
@ -2,6 +2,7 @@ package lidarr
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -11,12 +12,12 @@ import (
|
|||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (int, []byte, error) {
|
||||
func (c *client) get(ctx context.Context, endpoint string) (int, []byte, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, reqUrl, http.NoBody)
|
||||
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)
|
||||
}
|
||||
|
@ -42,7 +43,7 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
return resp.StatusCode, buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *client) post(endpoint string, data interface{}) (*http.Response, error) {
|
||||
func (c *client) post(ctx context.Context, endpoint string, data interface{}) (*http.Response, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
@ -52,7 +53,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
return nil, errors.Wrap(err, "lidarr client could not marshal data: %v", reqUrl)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
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)
|
||||
}
|
||||
|
@ -81,7 +82,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error) {
|
||||
func (c *client) postBody(ctx context.Context, 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()
|
||||
|
@ -91,7 +92,7 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
return 0, nil, errors.Wrap(err, "lidarr client could not marshal data: %v", reqUrl)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package lidarr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -25,8 +26,8 @@ type Config struct {
|
|||
}
|
||||
|
||||
type Client interface {
|
||||
Test() (*SystemStatusResponse, error)
|
||||
Push(release Release) ([]string, error)
|
||||
Test(ctx context.Context) (*SystemStatusResponse, error)
|
||||
Push(ctx context.Context, release Release) ([]string, error)
|
||||
}
|
||||
|
||||
type client struct {
|
||||
|
@ -89,8 +90,8 @@ type SystemStatusResponse struct {
|
|||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get("system/status")
|
||||
func (c *client) Test(ctx context.Context) (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get(ctx, "system/status")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "lidarr client get error")
|
||||
}
|
||||
|
@ -110,8 +111,8 @@ func (c *client) Test() (*SystemStatusResponse, error) {
|
|||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
status, res, err := c.postBody("release/push", release)
|
||||
func (c *client) Push(ctx context.Context, release Release) ([]string, error) {
|
||||
status, res, err := c.postBody(ctx, "release/push", release)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "lidarr client post error")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package lidarr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
@ -101,7 +102,7 @@ func Test_client_Push(t *testing.T) {
|
|||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.fields.config)
|
||||
|
||||
rejections, err := c.Push(tt.args.release)
|
||||
rejections, err := c.Push(context.Background(), tt.args.release)
|
||||
assert.Equal(t, tt.rejections, rejections)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.Equal(t, tt.err, err)
|
||||
|
@ -170,7 +171,7 @@ func Test_client_Test(t *testing.T) {
|
|||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.cfg)
|
||||
|
||||
got, err := c.Test()
|
||||
got, err := c.Test(context.Background())
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.EqualErrorf(t, err, tt.expectedErr, "Error should be: %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package radarr
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -11,12 +12,12 @@ import (
|
|||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (int, []byte, error) {
|
||||
func (c *client) get(ctx context.Context, endpoint string) (int, []byte, 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)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "could not build request: %v", reqUrl)
|
||||
}
|
||||
|
@ -42,7 +43,7 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
return resp.StatusCode, buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *client) post(endpoint string, data interface{}) (*http.Response, error) {
|
||||
func (c *client) post(ctx context.Context, 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()
|
||||
|
@ -52,7 +53,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
return nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not build request: %v", reqUrl)
|
||||
}
|
||||
|
@ -83,7 +84,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error) {
|
||||
func (c *client) postBody(ctx context.Context, endpoint string, data interface{}) (int, []byte, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v3/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
@ -93,7 +94,7 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
return 0, nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "could not build request: %v", reqUrl)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package radarr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -25,8 +26,8 @@ type Config struct {
|
|||
}
|
||||
|
||||
type Client interface {
|
||||
Test() (*SystemStatusResponse, error)
|
||||
Push(release Release) ([]string, error)
|
||||
Test(ctx context.Context) (*SystemStatusResponse, error)
|
||||
Push(ctx context.Context, release Release) ([]string, error)
|
||||
}
|
||||
|
||||
type client struct {
|
||||
|
@ -88,8 +89,8 @@ func (r *BadRequestResponse) String() string {
|
|||
return fmt.Sprintf("[%v: %v] %v: %v - got value: %v", r.Severity, r.ErrorCode, r.PropertyName, r.ErrorMessage, r.AttemptedValue)
|
||||
}
|
||||
|
||||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get("system/status")
|
||||
func (c *client) Test(ctx context.Context) (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get(ctx, "system/status")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "radarr error running test")
|
||||
}
|
||||
|
@ -108,8 +109,8 @@ func (c *client) Test() (*SystemStatusResponse, error) {
|
|||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
status, res, err := c.postBody("release/push", release)
|
||||
func (c *client) Push(ctx context.Context, release Release) ([]string, error) {
|
||||
status, res, err := c.postBody(ctx, "release/push", release)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error push release")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package radarr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
@ -141,7 +142,7 @@ func Test_client_Push(t *testing.T) {
|
|||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.fields.config)
|
||||
|
||||
rejections, err := c.Push(tt.args.release)
|
||||
rejections, err := c.Push(context.Background(), tt.args.release)
|
||||
assert.Equal(t, tt.rejections, rejections)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.Equal(t, tt.err, err)
|
||||
|
@ -223,7 +224,7 @@ func Test_client_Test(t *testing.T) {
|
|||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.cfg)
|
||||
|
||||
got, err := c.Test()
|
||||
got, err := c.Test(context.Background())
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.EqualErrorf(t, err, tt.expectedErr, "Error should be: %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package readarr
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -11,12 +12,12 @@ import (
|
|||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (int, []byte, error) {
|
||||
func (c *client) get(ctx context.Context, endpoint string) (int, []byte, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, reqUrl, http.NoBody)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
@ -42,7 +43,7 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
return resp.StatusCode, buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *client) post(endpoint string, data interface{}) (*http.Response, error) {
|
||||
func (c *client) post(ctx context.Context, endpoint string, data interface{}) (*http.Response, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
@ -52,7 +53,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
return nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
@ -81,7 +82,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error) {
|
||||
func (c *client) postBody(ctx context.Context, 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()
|
||||
|
@ -93,7 +94,7 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
|
||||
c.Log.Printf("readarr push JSON: %s\n", string(jsonData))
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package readarr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -26,8 +27,8 @@ type Config struct {
|
|||
}
|
||||
|
||||
type Client interface {
|
||||
Test() (*SystemStatusResponse, error)
|
||||
Push(release Release) ([]string, error)
|
||||
Test(ctx context.Context) (*SystemStatusResponse, error)
|
||||
Push(ctx context.Context, release Release) ([]string, error)
|
||||
}
|
||||
|
||||
type client struct {
|
||||
|
@ -92,8 +93,8 @@ type SystemStatusResponse struct {
|
|||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get("system/status")
|
||||
func (c *client) Test(ctx context.Context) (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get(ctx, "system/status")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not make Test")
|
||||
}
|
||||
|
@ -112,8 +113,8 @@ func (c *client) Test() (*SystemStatusResponse, error) {
|
|||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
status, res, err := c.postBody("release/push", release)
|
||||
func (c *client) Push(ctx context.Context, release Release) ([]string, error) {
|
||||
status, res, err := c.postBody(ctx, "release/push", release)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not push release to readarr")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package readarr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
@ -78,7 +79,7 @@ func Test_client_Push(t *testing.T) {
|
|||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.fields.config)
|
||||
|
||||
rejections, err := c.Push(tt.args.release)
|
||||
rejections, err := c.Push(context.Background(), tt.args.release)
|
||||
assert.Equal(t, tt.rejections, rejections)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.Equal(t, tt.err, err)
|
||||
|
@ -147,7 +148,7 @@ func Test_client_Test(t *testing.T) {
|
|||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.cfg)
|
||||
|
||||
got, err := c.Test()
|
||||
got, err := c.Test(context.Background())
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.EqualErrorf(t, err, tt.expectedErr, "Error should be: %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package sonarr
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -11,12 +12,12 @@ import (
|
|||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (int, []byte, error) {
|
||||
func (c *client) get(ctx context.Context, endpoint string) (int, []byte, 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)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
@ -42,7 +43,7 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
return resp.StatusCode, buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *client) post(endpoint string, data interface{}) (*http.Response, error) {
|
||||
func (c *client) post(ctx context.Context, 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()
|
||||
|
@ -52,7 +53,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
return nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
@ -81,7 +82,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error) {
|
||||
func (c *client) postBody(ctx context.Context, endpoint string, data interface{}) (int, []byte, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v3/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
@ -91,7 +92,7 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
return 0, nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package sonarr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -26,8 +27,8 @@ type Config struct {
|
|||
}
|
||||
|
||||
type Client interface {
|
||||
Test() (*SystemStatusResponse, error)
|
||||
Push(release Release) ([]string, error)
|
||||
Test(ctx context.Context) (*SystemStatusResponse, error)
|
||||
Push(ctx context.Context, release Release) ([]string, error)
|
||||
}
|
||||
|
||||
type client struct {
|
||||
|
@ -91,8 +92,8 @@ type SystemStatusResponse struct {
|
|||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get("system/status")
|
||||
func (c *client) Test(ctx context.Context) (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get(ctx, "system/status")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not make Test")
|
||||
}
|
||||
|
@ -111,8 +112,8 @@ func (c *client) Test() (*SystemStatusResponse, error) {
|
|||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
status, res, err := c.postBody("release/push", release)
|
||||
func (c *client) Push(ctx context.Context, release Release) ([]string, error) {
|
||||
status, res, err := c.postBody(ctx, "release/push", release)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not push release to sonarr")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package sonarr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -109,7 +110,7 @@ func Test_client_Push(t *testing.T) {
|
|||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.fields.config)
|
||||
|
||||
rejections, err := c.Push(tt.args.release)
|
||||
rejections, err := c.Push(context.Background(), tt.args.release)
|
||||
assert.Equal(t, tt.rejections, rejections)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.Equal(t, tt.err, err)
|
||||
|
@ -179,7 +180,7 @@ func Test_client_Test(t *testing.T) {
|
|||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.cfg)
|
||||
|
||||
got, err := c.Test()
|
||||
got, err := c.Test(context.Background())
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.EqualErrorf(t, err, tt.expectedErr, "Error should be: %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package whisparr
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
@ -10,12 +11,12 @@ import (
|
|||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (*http.Response, error) {
|
||||
func (c *client) get(ctx context.Context, 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)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
@ -39,7 +40,7 @@ func (c *client) get(endpoint string) (*http.Response, error) {
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func (c *client) post(endpoint string, data interface{}) (*http.Response, error) {
|
||||
func (c *client) post(ctx context.Context, 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()
|
||||
|
@ -49,7 +50,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
return nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package whisparr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
|
@ -24,8 +25,8 @@ type Config struct {
|
|||
}
|
||||
|
||||
type Client interface {
|
||||
Test() (*SystemStatusResponse, error)
|
||||
Push(release Release) ([]string, error)
|
||||
Test(ctx context.Context) (*SystemStatusResponse, error)
|
||||
Push(ctx context.Context, release Release) ([]string, error)
|
||||
}
|
||||
|
||||
type client struct {
|
||||
|
@ -75,8 +76,8 @@ type SystemStatusResponse struct {
|
|||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
res, err := c.get("system/status")
|
||||
func (c *client) Test(ctx context.Context) (*SystemStatusResponse, error) {
|
||||
res, err := c.get(ctx, "system/status")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not test whisparr")
|
||||
}
|
||||
|
@ -99,8 +100,8 @@ func (c *client) Test() (*SystemStatusResponse, error) {
|
|||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
res, err := c.post("release/push", release)
|
||||
func (c *client) Push(ctx context.Context, release Release) ([]string, error) {
|
||||
res, err := c.post(ctx, "release/push", release)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not push release to whisparr: %+v", release)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue