feat(download-clients): porla implement rules (#711)

* feat(downloadclients): Porla implement rules

* feat(downloadclients): Porla add basic auth support

* feat(porla): use new token for auth

* feat(porla): update check can download rules
This commit is contained in:
ze0s 2023-02-24 19:17:02 +01:00 committed by GitHub
parent 209e23de4f
commit d100703784
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 248 additions and 59 deletions

View file

@ -1,30 +1,85 @@
package porla
import (
"crypto/tls"
"io"
"log"
"net/http"
"time"
"github.com/autobrr/autobrr/pkg/jsonrpc"
)
var (
DefaultTimeout = 60 * time.Second
)
type Client struct {
Name string
Hostname string
cfg Config
rpcClient jsonrpc.Client
http *http.Client
timeout time.Duration
log *log.Logger
}
type Settings struct {
type Config struct {
Hostname string
AuthToken string
Log *log.Logger
// TLS skip cert validation
TLSSkipVerify bool
// HTTP Basic auth username
BasicUser string
// HTTP Basic auth password
BasicPass string
Timeout int
Log *log.Logger
}
func NewClient(settings Settings) *Client {
func NewClient(cfg Config) *Client {
c := &Client{
rpcClient: jsonrpc.NewClientWithOpts(settings.Hostname+"/api/v1/jsonrpc", &jsonrpc.ClientOpts{
Headers: map[string]string{
"Authorization": "Bearer " + settings.AuthToken,
},
}),
cfg: cfg,
log: log.New(io.Discard, "", log.LstdFlags),
timeout: DefaultTimeout,
}
// override logger if we pass one
if cfg.Log != nil {
c.log = cfg.Log
}
if cfg.Timeout > 0 {
c.timeout = time.Duration(cfg.Timeout) * time.Second
}
c.http = &http.Client{
Timeout: c.timeout,
}
customTransport := http.DefaultTransport.(*http.Transport).Clone()
if cfg.TLSSkipVerify {
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
httpClient := &http.Client{
Timeout: c.timeout,
Transport: customTransport,
}
c.rpcClient = jsonrpc.NewClientWithOpts(cfg.Hostname+"/api/v1/jsonrpc", &jsonrpc.ClientOpts{
Headers: map[string]string{
"X-Porla-Token": cfg.AuthToken,
},
HTTPClient: httpClient,
BasicUser: cfg.BasicUser,
BasicPass: cfg.BasicPass,
})
return c
}