| 1 |
package pushbullet |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
|
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient" |
| 13 |
) |
| 14 |
|
| 15 |
// Service providing Pushbullet as a notification service. |
| 16 |
type Service struct { |
| 17 |
standard.Standard |
| 18 |
|
| 19 |
client jsonclient.Client |
| 20 |
Config *Config |
| 21 |
pkr format.PropKeyResolver |
| 22 |
httpClient types.HTTPClient |
| 23 |
} |
| 24 |
|
| 25 |
// Constants. |
| 26 |
const ( |
| 27 |
pushesEndpoint = "https://api.pushbullet.com/v2/pushes" |
| 28 |
) |
| 29 |
|
| 30 |
// Static errors for push validation. |
| 31 |
var ( |
| 32 |
ErrUnexpectedResponseType = errors.New("unexpected response type, expected note") |
| 33 |
ErrResponseBodyMismatch = errors.New("response body mismatch") |
| 34 |
ErrResponseTitleMismatch = errors.New("response title mismatch") |
| 35 |
ErrPushNotActive = errors.New("push notification is not active") |
| 36 |
) |
| 37 |
|
| 38 |
// GetID returns the service identifier. |
| 39 |
func (s *Service) GetID() string { |
| 40 |
return Scheme |
| 41 |
} |
| 42 |
|
| 43 |
// Initialize loads ServiceConfig from serviceURL and sets logger for this Service. |
| 44 |
func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 45 |
s.SetLogger(logger) |
| 46 |
|
| 47 |
s.Config = &Config{ |
| 48 |
Title: "Shoutrrr notification", // Explicitly set default |
| 49 |
} |
| 50 |
s.pkr = format.NewPropKeyResolver(s.Config) |
| 51 |
|
| 52 |
if err := s.Config.setURL(&s.pkr, serviceURL); err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
|
| 56 |
s.client = jsonclient.NewWithHTTPClient(s.httpClientOrDefault()) |
| 57 |
s.client.Headers().Set("Access-Token", s.Config.Token) |
| 58 |
|
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
// Send a push notification via Pushbullet. |
| 63 |
func (s *Service) Send(message string, params *types.Params) error { |
| 64 |
config := *s.Config |
| 65 |
if err := s.pkr.UpdateConfigFromParams(&config, params); err != nil { |
| 66 |
return fmt.Errorf("updating config from params: %w", err) |
| 67 |
} |
| 68 |
|
| 69 |
for _, target := range config.Targets { |
| 70 |
if err := s.doSend(&config, target, message); err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return nil |
| 76 |
} |
| 77 |
|
| 78 |
// SetHTTPClient sets a custom HTTP client for the service. |
| 79 |
func (s *Service) SetHTTPClient(client types.HTTPClient) { |
| 80 |
if client == nil { |
| 81 |
s.httpClient = nil |
| 82 |
s.client = nil |
| 83 |
|
| 84 |
return |
| 85 |
} |
| 86 |
|
| 87 |
if c, ok := client.(*http.Client); ok && c == nil { |
| 88 |
s.httpClient = nil |
| 89 |
s.client = nil |
| 90 |
|
| 91 |
return |
| 92 |
} |
| 93 |
|
| 94 |
s.httpClient = client |
| 95 |
s.client = jsonclient.NewWithHTTPClient(client) |
| 96 |
} |
| 97 |
|
| 98 |
// doSend sends a push notification to a specific target and validates the response. |
| 99 |
func (s *Service) doSend(config *Config, target, message string) error { |
| 100 |
push := NewNotePush(message, config.Title) |
| 101 |
push.SetTarget(target) |
| 102 |
|
| 103 |
response := PushResponse{} |
| 104 |
if err := s.client.Post(pushesEndpoint, push, &response); err != nil { |
| 105 |
errorResponse := &ResponseError{} |
| 106 |
if s.client.ErrorResponse(err, errorResponse) { |
| 107 |
return fmt.Errorf("API error: %w", errorResponse) |
| 108 |
} |
| 109 |
|
| 110 |
return fmt.Errorf("failed to push: %w", err) |
| 111 |
} |
| 112 |
|
| 113 |
// Validate response fields |
| 114 |
if response.Type != "note" { |
| 115 |
return fmt.Errorf("%w: got %s", ErrUnexpectedResponseType, response.Type) |
| 116 |
} |
| 117 |
|
| 118 |
if response.Body != message { |
| 119 |
return fmt.Errorf( |
| 120 |
"%w: got %s, expected %s", |
| 121 |
ErrResponseBodyMismatch, |
| 122 |
response.Body, |
| 123 |
message, |
| 124 |
) |
| 125 |
} |
| 126 |
|
| 127 |
if response.Title != config.Title { |
| 128 |
return fmt.Errorf( |
| 129 |
"%w: got %s, expected %s", |
| 130 |
ErrResponseTitleMismatch, |
| 131 |
response.Title, |
| 132 |
config.Title, |
| 133 |
) |
| 134 |
} |
| 135 |
|
| 136 |
if !response.Active { |
| 137 |
return ErrPushNotActive |
| 138 |
} |
| 139 |
|
| 140 |
return nil |
| 141 |
} |
| 142 |
|
| 143 |
// httpClientOrDefault returns the injected client or a default Client. |
| 144 |
func (s *Service) httpClientOrDefault() *http.Client { |
| 145 |
if s.httpClient != nil { |
| 146 |
if c, ok := s.httpClient.(*http.Client); ok { |
| 147 |
return c |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if s.httpClient == nil { |
| 152 |
return &http.Client{} |
| 153 |
} |
| 154 |
|
| 155 |
return &http.Client{} |
| 156 |
} |