mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
refactor(http): implement bufio (#1604)
* fix: misc http fixes * feat(io): implement bufio around syscalls * peek-a-boo * this can't be right. * you better be wearing a helmet * jesus christ. * refactor(notifications): check err on non-ok status * fix(notifications): add missing name method * refactor(indexer): api clients * fix(indexer): ptp test --------- Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
parent
d13b421c42
commit
cc0cca9f0d
22 changed files with 465 additions and 304 deletions
|
@ -4,6 +4,7 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
@ -49,6 +50,10 @@ type notifiarrSender struct {
|
|||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func (s *notifiarrSender) Name() string {
|
||||
return "notifiarr"
|
||||
}
|
||||
|
||||
func NewNotifiarrSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
|
||||
return ¬ifiarrSender{
|
||||
log: log.With().Str("sender", "notifiarr").Logger(),
|
||||
|
@ -69,14 +74,12 @@ func (s *notifiarrSender) Send(event domain.NotificationEvent, payload domain.No
|
|||
|
||||
jsonData, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("notifiarr client could not marshal data: %v", m)
|
||||
return errors.Wrap(err, "could not marshal data: %+v", m)
|
||||
return errors.Wrap(err, "could not marshal json request for event: %v payload: %v", event, payload)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, s.baseUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", event)
|
||||
return errors.Wrap(err, "could not create request")
|
||||
return errors.Wrap(err, "could not create request for event: %v payload: %v", event, payload)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
@ -85,23 +88,20 @@ func (s *notifiarrSender) Send(event domain.NotificationEvent, payload domain.No
|
|||
|
||||
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)
|
||||
return errors.Wrap(err, "client request error for event: %v payload: %v", event, payload)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
s.log.Trace().Msgf("notifiarr status: %v response: %v", res.StatusCode, string(body))
|
||||
s.log.Trace().Msgf("response status: %d", res.StatusCode)
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", string(body))
|
||||
return errors.New("bad status: %v body: %v", res.StatusCode, string(body))
|
||||
body, err := io.ReadAll(bufio.NewReader(res.Body))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not read body for event: %v payload: %v", event, payload)
|
||||
}
|
||||
|
||||
return errors.New("unexpected status: %v body: %v", res.StatusCode, string(body))
|
||||
}
|
||||
|
||||
s.log.Debug().Msg("notification successfully sent to notifiarr")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue