| 1 | package opsgeniescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ |
| 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 | "errorsscip-go gomod github.com/golang/go/src . errors/" |
| 8 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 9 | "ioscip-go gomod github.com/golang/go/src . io/" |
| 10 | "net/httpscip-go gomod github.com/golang/go/src . `net/http`/" |
| 11 | "net/urlscip-go gomod github.com/golang/go/src . `net/url`/" |
| 12 | "timescip-go gomod github.com/golang/go/src . time/" |
| 13 | |
| 14 | "github.com/nicholas-fedor/shoutrrr/pkg/formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/" |
| 15 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/" |
| 16 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 17 | ) |
| 18 | |
| 19 | // Service provides OpsGenie as a notification service. |
| 20 | type Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service# struct { |
| 21 | standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/.Standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#Standard. |
| 22 | |
| 23 | Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#Config. *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config# |
| 24 | pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#pkr. formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver# |
| 25 | httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#httpClient. 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# |
| 26 | } |
| 27 | |
| 28 | // alertEndpointTemplate is the OpsGenie API endpoint template for sending alerts. |
| 29 | const ( |
| 30 | alertEndpointTemplatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/alertEndpointTemplate. = "https://%s:%d/v2/alerts" |
| 31 | MaxMessageLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/MaxMessageLength. = 130 // MaxMessageLength is the maximum length of the alert message field in OpsGenie. |
| 32 | httpSuccessMaxscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/httpSuccessMax. = 299 // httpSuccessMax is the maximum HTTP status code for a successful response. |
| 33 | defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/defaultHTTPTimeout. = 10 * timescip-go gomod github.com/golang/go/src . time/.Secondscip-go gomod github.com/golang/go/src . time/Second. // defaultHTTPTimeout is the default timeout for HTTP requests. |
| 34 | ) |
| 35 | |
| 36 | // ErrUnexpectedStatus indicates that OpsGenie returned an unexpected HTTP status code. |
| 37 | var ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrUnexpectedStatus. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("OpsGenie notification returned unexpected HTTP status code") |
| 38 | |
| 39 | // GetID returns the service identifier. |
| 40 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#) GetIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#GetID().() string { |
| 41 | return Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Scheme. |
| 42 | } |
| 43 | |
| 44 | // Initialize configures the service with a URL and logger. |
| 45 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#) Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#Initialize().(serviceURL *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#, logger typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.StdLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#) error { |
| 46 | s.SetLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Logger#SetLogger().(logger) |
| 47 | s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#Config. = &Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#{} |
| 48 | s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#pkr. = formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.NewPropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/NewPropKeyResolver().(s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#Config.) |
| 49 | |
| 50 | return s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#Config..setURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#setURL().(&s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#pkr., serviceURL) |
| 51 | } |
| 52 | |
| 53 | // Send delivers a notification message to OpsGenie. |
| 54 | // See: https://docs.opsgenie.com/docs/alert-api#create-alert |
| 55 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#) Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#Send().(message string, params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#) error { |
| 56 | config := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#Config. |
| 57 | serviceURL := fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().( |
| 58 | alertEndpointTemplatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/alertEndpointTemplate., |
| 59 | config.Hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Host., |
| 60 | config.Portscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Port., |
| 61 | ) |
| 62 | |
| 63 | payload, err := s.newAlertPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#newAlertPayload().(message, params) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | return s.sendAlertscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#sendAlert().(serviceURL, config.APIKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#APIKey., &payload) |
| 69 | } |
| 70 | |
| 71 | // SetHTTPClient sets a custom HTTP client for the service. |
| 72 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#) SetHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#SetHTTPClient().(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#) { |
| 73 | s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#httpClient. = client |
| 74 | } |
| 75 | |
| 76 | // newAlertPayload creates a new alert payload for OpsGenie based on the message and parameters. |
| 77 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#) newAlertPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#newAlertPayload().( |
| 78 | message string, |
| 79 | params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#, |
| 80 | ) (AlertPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#, error) { |
| 81 | if params == nil { |
| 82 | params = &typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#{} |
| 83 | } |
| 84 | |
| 85 | // Defensive copy |
| 86 | payloadFields := *s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#Config. |
| 87 | |
| 88 | if err := s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#pkr..UpdateConfigFromParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#UpdateConfigFromParams().(&payloadFields, params); err != nil { |
| 89 | return AlertPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#{}, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("updating payload fields from params: %w", err) |
| 90 | } |
| 91 | |
| 92 | // Use `Message` for the title if available, or if the message is too long |
| 93 | // Use `Description` for the message in these scenarios |
| 94 | title := payloadFields.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Title. |
| 95 | description := message |
| 96 | |
| 97 | if title == "" { |
| 98 | if len(message) > MaxMessageLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/MaxMessageLength. { |
| 99 | title = message[:MaxMessageLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/MaxMessageLength.] |
| 100 | } else { |
| 101 | title = message |
| 102 | description = "" |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if payloadFields.Descriptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Description. != "" && description != "" { |
| 107 | description += "\n" |
| 108 | } |
| 109 | |
| 110 | result := AlertPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#{ |
| 111 | Messagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Message.: title, |
| 112 | Aliasscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Alias.: payloadFields.Aliasscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Alias., |
| 113 | Descriptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Description.: description + payloadFields.Descriptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Description., |
| 114 | Respondersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Responders.: payloadFields.Respondersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Responders., |
| 115 | VisibleToscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#VisibleTo.: payloadFields.VisibleToscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#VisibleTo., |
| 116 | Actionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Actions.: payloadFields.Actionsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Actions., |
| 117 | Tagsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Tags.: payloadFields.Tagsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Tags., |
| 118 | Detailsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Details.: payloadFields.Detailsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Details., |
| 119 | Entityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Entity.: payloadFields.Entityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Entity., |
| 120 | Sourcescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Source.: payloadFields.Sourcescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Source., |
| 121 | Priorityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Priority.: payloadFields.Priorityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Priority., |
| 122 | Userscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#User.: payloadFields.Userscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#User., |
| 123 | Notescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#Note.: payloadFields.Notescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Config#Note., |
| 124 | } |
| 125 | |
| 126 | return result, nil |
| 127 | } |
| 128 | |
| 129 | // sendAlert sends an alert to OpsGenie using the specified URL and API key. |
| 130 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#) sendAlertscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#sendAlert().(serviceURL, apiKey string, payload *AlertPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/AlertPayload#) error { |
| 131 | jsonBody, err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src . `encoding/json`/Marshal().(payload) |
| 132 | if err != nil { |
| 133 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("marshaling alert payload to JSON: %w", err) |
| 134 | } |
| 135 | |
| 136 | jsonBuffer := bytesscip-go gomod github.com/golang/go/src . bytes/.NewBufferscip-go gomod github.com/golang/go/src . bytes/NewBuffer().(jsonBody) |
| 137 | |
| 138 | ctx, cancel := contextscip-go gomod github.com/golang/go/src . context/.WithTimeoutscip-go gomod github.com/golang/go/src . context/WithTimeout().( |
| 139 | contextscip-go gomod github.com/golang/go/src . context/.Backgroundscip-go gomod github.com/golang/go/src . context/Background().(), |
| 140 | defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/defaultHTTPTimeout., |
| 141 | ) |
| 142 | defer cancel() |
| 143 | |
| 144 | req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().( |
| 145 | ctx, |
| 146 | httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost., |
| 147 | serviceURL, |
| 148 | jsonBuffer, |
| 149 | ) |
| 150 | if err != nil { |
| 151 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating HTTP request: %w", err) |
| 152 | } |
| 153 | |
| 154 | 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().("Authorization", "GenieKey "+apiKey) |
| 155 | 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().("Content-Type", "application/json") |
| 156 | |
| 157 | client := s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Service#httpClient. |
| 158 | if client == nil { |
| 159 | client = &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{} |
| 160 | } |
| 161 | |
| 162 | resp, err := client.Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#Do.(req) |
| 163 | if err != nil { |
| 164 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to send notification to OpsGenie: %w", err) |
| 165 | } |
| 166 | |
| 167 | defer func() { _ = resp.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src . io/Closer#Close().() }() |
| 168 | |
| 169 | if resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. < httpscip-go gomod github.com/golang/go/src . `net/http`/.StatusOKscip-go gomod github.com/golang/go/src . `net/http`/StatusOK. || resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. > httpSuccessMaxscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/httpSuccessMax. { |
| 170 | body, err := ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(resp.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body.) |
| 171 | if err != nil { |
| 172 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().( |
| 173 | "%w: %d, cannot read body: %w", |
| 174 | ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrUnexpectedStatus., |
| 175 | resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode., |
| 176 | err, |
| 177 | ) |
| 178 | } |
| 179 | |
| 180 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().( |
| 181 | "%w: %d - %s", |
| 182 | ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrUnexpectedStatus., |
| 183 | resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode., |
| 184 | body, |
| 185 | ) |
| 186 | } |
| 187 | |
| 188 | return nil |
| 189 | } |