feat(actions): simplify macro parsing (#560)

* refactor(action): parse macros

* feat(action): add ctx to arr clients and test
This commit is contained in:
ze0s 2022-12-10 21:48:19 +01:00 committed by GitHub
parent f6e68fae2b
commit 839eb9f3f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 323 additions and 334 deletions

View file

@ -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")
}

View file

@ -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)
}