refactor(http): implement shared transport and clients (#1288)

* fix(http): flip to a shared transport and clients

* nice threads

* that is terrible

* fake uri for magnet

* lazy locking

* why bother with r's

* flip magic params to struct

* refactor(http-clients): use separate clients with shared transport

* refactor(http-clients): add missing license header

* refactor(http-clients): defer and fix errors

---------

Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
Kyle Sanderson 2023-12-29 14:49:22 -08:00 committed by GitHub
parent 2a4fb7750b
commit 3234f0d919
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 537 additions and 391 deletions

View file

@ -5,7 +5,6 @@ package notification
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
@ -15,6 +14,7 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/dustin/go-humanize"
"github.com/rs/zerolog"
@ -50,12 +50,18 @@ const (
type discordSender struct {
log zerolog.Logger
Settings domain.Notification
httpClient *http.Client
}
func NewDiscordSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
return &discordSender{
log: log.With().Str("sender", "discord").Logger(),
Settings: settings,
httpClient: &http.Client{
Timeout: time.Second * 30,
Transport: sharedhttp.Transport,
},
}
}
@ -80,27 +86,20 @@ func (a *discordSender) Send(event domain.NotificationEvent, payload domain.Noti
req.Header.Set("Content-Type", "application/json")
//req.Header.Set("User-Agent", "autobrr")
t := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := http.Client{Transport: t, Timeout: 30 * time.Second}
res, err := client.Do(req)
res, err := a.httpClient.Do(req)
if err != nil {
a.log.Error().Err(err).Msgf("discord client request error: %v", event)
return errors.Wrap(err, "could not make request: %+v", req)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
a.log.Error().Err(err).Msgf("discord client request error: %v", event)
return errors.Wrap(err, "could not read data")
}
defer res.Body.Close()
a.log.Trace().Msgf("discord status: %v response: %v", res.StatusCode, string(body))
// discord responds with 204, Notifiarr with 204 so lets take all 200 as ok

View file

@ -13,6 +13,7 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/rs/zerolog"
)
@ -26,6 +27,8 @@ type gotifySender struct {
log zerolog.Logger
Settings domain.Notification
builder NotificationBuilderPlainText
httpClient *http.Client
}
func NewGotifySender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
@ -33,6 +36,10 @@ func NewGotifySender(log zerolog.Logger, settings domain.Notification) domain.No
log: log.With().Str("sender", "gotify").Logger(),
Settings: settings,
builder: NotificationBuilderPlainText{},
httpClient: &http.Client{
Timeout: time.Second * 30,
Transport: sharedhttp.Transport,
},
}
}
@ -56,21 +63,20 @@ func (s *gotifySender) Send(event domain.NotificationEvent, payload domain.Notif
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "autobrr")
client := http.Client{Timeout: 30 * time.Second}
res, err := client.Do(req)
res, err := s.httpClient.Do(req)
if err != nil {
s.log.Error().Err(err).Msgf("gotify client request error: %v", event)
return errors.Wrap(err, "could not make request: %+v", req)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
s.log.Error().Err(err).Msgf("gotify client request error: %v", event)
return errors.Wrap(err, "could not read data")
}
defer res.Body.Close()
s.log.Trace().Msgf("gotify status: %v response: %v", res.StatusCode, string(body))
if res.StatusCode != http.StatusOK {

View file

@ -9,6 +9,7 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/rs/zerolog"
)
@ -26,6 +27,8 @@ type lunaSeaSender struct {
log zerolog.Logger
Settings domain.Notification
builder NotificationBuilderPlainText
httpClient *http.Client
}
func (s *lunaSeaSender) rewriteWebhookURL(url string) string {
@ -38,6 +41,10 @@ func NewLunaSeaSender(log zerolog.Logger, settings domain.Notification) domain.N
log: log.With().Str("sender", "lunasea").Logger(),
Settings: settings,
builder: NotificationBuilderPlainText{},
httpClient: &http.Client{
Timeout: time.Second * 30,
Transport: sharedhttp.Transport,
},
}
}
@ -64,8 +71,7 @@ func (s *lunaSeaSender) Send(event domain.NotificationEvent, payload domain.Noti
req.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: 30 * time.Second}
res, err := client.Do(req)
res, err := s.httpClient.Do(req)
if err != nil {
s.log.Error().Err(err).Msg("lunasea client request error")
return errors.Wrap(err, "could not make request")

View file

@ -5,7 +5,6 @@ package notification
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"net/http"
@ -13,6 +12,7 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/rs/zerolog"
)
@ -45,6 +45,8 @@ type notifiarrSender struct {
log zerolog.Logger
Settings domain.Notification
baseUrl string
httpClient *http.Client
}
func NewNotifiarrSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
@ -52,6 +54,10 @@ func NewNotifiarrSender(log zerolog.Logger, settings domain.Notification) domain
log: log.With().Str("sender", "notifiarr").Logger(),
Settings: settings,
baseUrl: "https://notifiarr.com/api/v1/notification/autobrr",
httpClient: &http.Client{
Timeout: time.Second * 30,
Transport: sharedhttp.Transport,
},
}
}
@ -77,27 +83,20 @@ func (s *notifiarrSender) Send(event domain.NotificationEvent, payload domain.No
req.Header.Set("User-Agent", "autobrr")
req.Header.Set("X-API-Key", s.Settings.APIKey)
t := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := http.Client{Transport: t, Timeout: 30 * time.Second}
res, err := client.Do(req)
res, err := s.httpClient.Do(req)
if err != nil {
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", event)
return errors.Wrap(err, "could not make request: %+v", req)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", event)
return errors.Wrap(err, "could not read data")
}
defer res.Body.Close()
s.log.Trace().Msgf("notifiarr status: %v response: %v", res.StatusCode, string(body))
if res.StatusCode != http.StatusOK {

View file

@ -14,6 +14,7 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/rs/zerolog"
)
@ -33,6 +34,8 @@ type pushoverSender struct {
Settings domain.Notification
baseUrl string
builder NotificationBuilderPlainText
httpClient *http.Client
}
func NewPushoverSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
@ -40,6 +43,11 @@ func NewPushoverSender(log zerolog.Logger, settings domain.Notification) domain.
log: log.With().Str("sender", "pushover").Logger(),
Settings: settings,
baseUrl: "https://api.pushover.net/1/messages.json",
builder: NotificationBuilderPlainText{},
httpClient: &http.Client{
Timeout: time.Second * 30,
Transport: sharedhttp.Transport,
},
}
}
@ -81,21 +89,20 @@ func (s *pushoverSender) Send(event domain.NotificationEvent, payload domain.Not
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "autobrr")
client := http.Client{Timeout: 30 * time.Second}
res, err := client.Do(req)
res, err := s.httpClient.Do(req)
if err != nil {
s.log.Error().Err(err).Msgf("pushover client request error: %v", event)
return errors.Wrap(err, "could not make request: %+v", req)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
s.log.Error().Err(err).Msgf("pushover client request error: %v", event)
return errors.Wrap(err, "could not read data")
}
defer res.Body.Close()
s.log.Trace().Msgf("pushover status: %v response: %v", res.StatusCode, string(body))
if res.StatusCode != http.StatusOK {

View file

@ -14,11 +14,12 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/rs/zerolog"
)
// Reference: https://core.telegram.org/bots/api#sendmessage
// TelegramMessage Reference: https://core.telegram.org/bots/api#sendmessage
type TelegramMessage struct {
ChatID string `json:"chat_id"`
Text string `json:"text"`
@ -31,6 +32,8 @@ type telegramSender struct {
Settings domain.Notification
ThreadID int
builder NotificationBuilderPlainText
httpClient *http.Client
}
func NewTelegramSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
@ -46,6 +49,11 @@ func NewTelegramSender(log zerolog.Logger, settings domain.Notification) domain.
log: log.With().Str("sender", "telegram").Logger(),
Settings: settings,
ThreadID: threadID,
builder: NotificationBuilderPlainText{},
httpClient: &http.Client{
Timeout: time.Second * 30,
Transport: sharedhttp.Transport,
},
}
}
@ -76,21 +84,20 @@ func (s *telegramSender) Send(event domain.NotificationEvent, payload domain.Not
req.Header.Set("Content-Type", "application/json")
//req.Header.Set("User-Agent", "autobrr")
client := http.Client{Timeout: 30 * time.Second}
res, err := client.Do(req)
res, err := s.httpClient.Do(req)
if err != nil {
s.log.Error().Err(err).Msgf("telegram client request error: %v", event)
return errors.Wrap(err, "could not make request: %+v", req)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
s.log.Error().Err(err).Msgf("telegram client request error: %v", event)
return errors.Wrap(err, "could not read data")
}
defer res.Body.Close()
s.log.Trace().Msgf("telegram status: %v response: %v", res.StatusCode, string(body))
if res.StatusCode != http.StatusOK {