| 1 | package gotifyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src . bytes/" |
| 5 | "contextscip-go gomod github.com/golang/go/src . context/" |
| 6 | "encoding/jsonscip-go gomod github.com/golang/go/src . `encoding/json`/" |
| 7 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 8 | "ioscip-go gomod github.com/golang/go/src . io/" |
| 9 | "net/httpscip-go gomod github.com/golang/go/src . `net/http`/" |
| 10 | |
| 11 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 12 | "github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/" |
| 13 | ) |
| 14 | |
| 15 | // Sender handles HTTP request execution and response processing. |
| 16 | type Senderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/Sender# interface { |
| 17 | SendRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/Sender#SendRequest.(client typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#, url string, request *MessageRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest#, headers httpscip-go gomod github.com/golang/go/src . `net/http`/.Headerscip-go gomod github.com/golang/go/src . `net/http`/Header#) error |
| 18 | } |
| 19 | |
| 20 | // DefaultSender provides the default implementation of Sender. |
| 21 | type DefaultSenderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender# struct{} |
| 22 | |
| 23 | // SendRequest handles the HTTP request. |
| 24 | // This function executes the actual HTTP POST request to the Gotify API endpoint, |
| 25 | // handling both successful responses and error conditions with appropriate error wrapping. |
| 26 | // Parameters: |
| 27 | // - client: HTTP client to use for the request |
| 28 | // - url: The complete API endpoint URL to send the request to |
| 29 | // - request: The JSON payload to send in the request body |
| 30 | // - headers: Optional headers to set on the request |
| 31 | // |
| 32 | // Returns: error if the request fails or server returns an error, nil on success. |
| 33 | func (s *DefaultSenderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#) SendRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#SendRequest().( |
| 34 | client typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#, |
| 35 | url string, |
| 36 | request *MessageRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest#, |
| 37 | headers httpscip-go gomod github.com/golang/go/src . `net/http`/.Headerscip-go gomod github.com/golang/go/src . `net/http`/Header#, |
| 38 | ) error { |
| 39 | // Prepare response structure to capture API response |
| 40 | response := &messageResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/messageResponse#{} |
| 41 | |
| 42 | var err error |
| 43 | |
| 44 | if len(headers) == 0 { |
| 45 | // Use JSON client for standard requests - this will handle error extraction |
| 46 | jsonClient := jsonclientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/.NewWithHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/NewWithHTTPClient().(client) |
| 47 | |
| 48 | err = jsonClient.Postscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/Client#Post.(url, request, response) |
| 49 | if err != nil { |
| 50 | // Try to extract structured error |
| 51 | errorRes := &responseErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/responseError#{} |
| 52 | if jsonClient.ErrorResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/Client#ErrorResponse.(err, errorRes) { |
| 53 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("server error: %w", errorRes) |
| 54 | } |
| 55 | |
| 56 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%s: %w", ErrSendFailedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ErrSendFailed..Error(), err) |
| 57 | } |
| 58 | |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // Use direct HTTP client when custom headers are needed |
| 63 | body, err := s.sendRequestWithHeadersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#sendRequestWithHeaders().(client, url, request, headers) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | err = jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().(body, response) |
| 69 | if err != nil { |
| 70 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%s: %w", ErrParseResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ErrParseResponse..Error(), err) |
| 71 | } |
| 72 | |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | // extractErrorResponse attempts to extract a structured error from a failed request. |
| 77 | func (s *DefaultSenderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#) extractErrorResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#extractErrorResponse().(body []byte, errorRes *responseErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/responseError#) bool { |
| 78 | return jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().(body, errorRes) == nil |
| 79 | } |
| 80 | |
| 81 | // handleResponseError checks the response status and extracts error information if present. |
| 82 | func (s *DefaultSenderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#) handleResponseErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#handleResponseError().(res *httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#, body []byte) error { |
| 83 | // Handle all HTTP error status codes (>= 400 covers both 4xx client errors and 5xx server errors) |
| 84 | if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. >= httpscip-go gomod github.com/golang/go/src . `net/http`/.StatusBadRequestscip-go gomod github.com/golang/go/src . `net/http`/StatusBadRequest. { |
| 85 | errorRes := &responseErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/responseError#{} |
| 86 | if s.extractErrorResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#extractErrorResponse().(body, errorRes) { |
| 87 | // Segment based on error category for more deterministic error handling |
| 88 | if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. >= httpscip-go gomod github.com/golang/go/src . `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src . `net/http`/StatusInternalServerError. { |
| 89 | // 5xx: Server-side errors (e.g., internal server error, bad gateway) |
| 90 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("server error (%d): %w", res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode., errorRes) |
| 91 | } |
| 92 | // 4xx: Client-side errors (e.g., bad request, unauthorized, not found) |
| 93 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("client error (%d): %w", res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode., errorRes) |
| 94 | } |
| 95 | |
| 96 | // Fallback when no structured error response is available |
| 97 | if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. >= httpscip-go gomod github.com/golang/go/src . `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src . `net/http`/StatusInternalServerError. { |
| 98 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %v", ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ErrUnexpectedStatus., res.Statusscip-go gomod github.com/golang/go/src . `net/http`/Response#Status.) |
| 99 | } |
| 100 | |
| 101 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %v", ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ErrUnexpectedStatus., res.Statusscip-go gomod github.com/golang/go/src . `net/http`/Response#Status.) |
| 102 | } |
| 103 | |
| 104 | return nil |
| 105 | } |
| 106 | |
| 107 | // sendRequestWithHeaders sends a request with custom headers using the underlying HTTP client. |
| 108 | // This method is used when per-request headers are needed, bypassing the jsonclient |
| 109 | // to avoid modifying shared header state. |
| 110 | // Parameters: |
| 111 | // - client: HTTP client to use |
| 112 | // - url: The complete API endpoint URL to send the request to |
| 113 | // - request: The JSON payload to send in the request body |
| 114 | // - headers: Custom headers to set on the request |
| 115 | // |
| 116 | // Returns: the response body as bytes if successful, or an error. |
| 117 | func (s *DefaultSenderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#) sendRequestWithHeadersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#sendRequestWithHeaders().( |
| 118 | client typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#, |
| 119 | url string, |
| 120 | request *MessageRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest#, |
| 121 | headers httpscip-go gomod github.com/golang/go/src . `net/http`/.Headerscip-go gomod github.com/golang/go/src . `net/http`/Header#, |
| 122 | ) ([]byte, error) { |
| 123 | body, err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src . `encoding/json`/Marshal().(request) |
| 124 | if err != nil { |
| 125 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%s: %w", ErrMarshalRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ErrMarshalRequest..Error(), err) |
| 126 | } |
| 127 | |
| 128 | req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().( |
| 129 | contextscip-go gomod github.com/golang/go/src . context/.Backgroundscip-go gomod github.com/golang/go/src . context/Background().(), |
| 130 | httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost., |
| 131 | url, |
| 132 | bytesscip-go gomod github.com/golang/go/src . bytes/.NewReaderscip-go gomod github.com/golang/go/src . bytes/NewReader().(body), |
| 133 | ) |
| 134 | if err != nil { |
| 135 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%s: %w", ErrCreateRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ErrCreateRequest..Error(), err) |
| 136 | } |
| 137 | |
| 138 | s.setRequestHeadersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#setRequestHeaders().(req, headers) |
| 139 | |
| 140 | res, err := client.Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#Do.(req) |
| 141 | if err != nil { |
| 142 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%s: %w", ErrSendRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ErrSendRequest..Error(), err) |
| 143 | } |
| 144 | |
| 145 | defer func() { _ = res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src . io/Closer#Close().() }() |
| 146 | |
| 147 | body, err = ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body.) |
| 148 | if err != nil { |
| 149 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%s: %w", ErrReadResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ErrReadResponse..Error(), err) |
| 150 | } |
| 151 | |
| 152 | if err := s.handleResponseErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#handleResponseError().(res, body); err != nil { |
| 153 | return nil, err |
| 154 | } |
| 155 | |
| 156 | return body, nil |
| 157 | } |
| 158 | |
| 159 | // setRequestHeaders sets the Content-Type and custom headers on the HTTP request. |
| 160 | func (s *DefaultSenderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#) setRequestHeadersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultSender#setRequestHeaders().(req *httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#, headers httpscip-go gomod github.com/golang/go/src . `net/http`/.Headerscip-go gomod github.com/golang/go/src . `net/http`/Header#) { |
| 161 | req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src . `net/http`/Header#Set().("Content-Type", "application/json") |
| 162 | |
| 163 | for key, values := range headers { |
| 164 | for _, value := range values { |
| 165 | req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Addscip-go gomod github.com/golang/go/src . `net/http`/Header#Add().(key, value) |
| 166 | } |
| 167 | } |
| 168 | } |