Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
fix(autobrr): host url with multiple slash (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
zze0s authored Jul 21, 2024
1 parent 5169e09 commit cdca795
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions pkg/autobrr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net/http"
"net/http/httputil"
"net/url"
"runtime"
"strconv"
"time"
Expand Down Expand Up @@ -72,14 +73,15 @@ func (c *Client) Test(ctx context.Context) error {
}

func (c *Client) GetFilters(ctx context.Context) ([]Filter, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.Host+"/api/filters", nil)
reqUrl, err := url.JoinPath(c.Host, "/api/filters")
if err != nil {
return nil, err
}

req.SetBasicAuth(c.BasicUser, c.BasicPass)
req.Header.Add("X-API-Token", c.APIKey)
c.buildUserAgent(req)
req, err := c.newRequest(ctx, http.MethodGet, reqUrl, nil)
if err != nil {
return nil, err
}

res, err := c.client.Do(req)
if err != nil {
Expand Down Expand Up @@ -112,14 +114,15 @@ func (c *Client) UpdateFilterByID(ctx context.Context, filterID int, filter Upda
return err
}

req, err := http.NewRequestWithContext(ctx, http.MethodPatch, c.Host+"/api/filters/"+id, bytes.NewBuffer(body))
reqUrl, err := url.JoinPath(c.Host, "/api/filters/", id)
if err != nil {
return err
}

req.SetBasicAuth(c.BasicUser, c.BasicPass)
req.Header.Add("X-API-Token", c.APIKey)
c.buildUserAgent(req)
req, err := c.newRequest(ctx, http.MethodPatch, reqUrl, bytes.NewBuffer(body))
if err != nil {
return err
}

res, err := c.client.Do(req)
if err != nil {
Expand All @@ -141,6 +144,19 @@ func (c *Client) UpdateFilterByID(ctx context.Context, filterID int, filter Upda
return nil
}

func (c *Client) newRequest(ctx context.Context, method string, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, err
}

req.SetBasicAuth(c.BasicUser, c.BasicPass)
req.Header.Add("X-API-Token", c.APIKey)
c.buildUserAgent(req)

return req, nil
}

func (c *Client) buildUserAgent(req *http.Request) {
agent := fmt.Sprintf("omegabrr/%s (%s %s)", buildinfo.Version, runtime.GOOS, runtime.GOARCH)

Expand Down

0 comments on commit cdca795

Please sign in to comment.