Debian Codegraph @

pkg/services/specialized/generic/generic_example_test.go

raw symbols SCIP
1 package genericscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src . fmt/"
5 "ioscip-go gomod github.com/golang/go/src . io/"
6 "logscip-go gomod github.com/golang/go/src . log/"
7 "net/httpscip-go gomod github.com/golang/go/src . `net/http`/"
8 "net/urlscip-go gomod github.com/golang/go/src . `net/url`/"
9
10 "github.com/jarcoal/httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/"
11
12 "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/"
13 )
14
15 // Example demonstrates basic webhook sending to a generic endpoint.
16 func Examplescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Example().() {
17 // Activate HTTP mocking for the example
18 httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.Activatescip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/Activate().()
19
20 defer httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.DeactivateAndResetscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/DeactivateAndReset().()
21
22 // Mock a successful webhook response
23 httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.RegisterResponderscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/RegisterResponder().("POST", "https://api.example.com/webhook",
24 httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.NewStringResponderscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/NewStringResponder().(200, "OK"))
25
26 // Create and initialize the service
27 service := &Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#{}
28 serviceURL, _ := urlscip-go gomod github.com/golang/go/src . `net/url`/.Parsescip-go gomod github.com/golang/go/src . `net/url`/Parse().("generic://api.example.com/webhook")
29
30 err := service.Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Initialize().(serviceURL, logscip-go gomod github.com/golang/go/src . log/.Newscip-go gomod github.com/golang/go/src . log/New().(ioscip-go gomod github.com/golang/go/src . io/.Discardscip-go gomod github.com/golang/go/src . io/Discard., "", 0))
31 if err != nil {
32 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("Error:", err)
33
34 return
35 }
36
37 // Send a notification
38 err = service.Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Send().("Hello, webhook!", nil)
39 if err != nil {
40 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("Error:", err)
41
42 return
43 }
44
45 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("Notification sent successfully")
46 // Output: Notification sent successfully
47 }
48
49 // ExampleJSONTemplate demonstrates sending a JSON-formatted payload.
50 func ExampleJSONTemplatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ExampleJSONTemplate().() {
51 // Activate HTTP mocking for the example
52 httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.Activatescip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/Activate().()
53
54 defer httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.DeactivateAndResetscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/DeactivateAndReset().()
55
56 // Mock a successful webhook response that accepts any JSON payload
57 httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.RegisterResponderscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/RegisterResponder().("POST", "https://api.example.com/webhook",
58 httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.NewStringResponderscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/NewStringResponder().(200, "OK"))
59
60 // Create and initialize the service with JSON template
61 service := &Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#{}
62 serviceURL, _ := urlscip-go gomod github.com/golang/go/src . `net/url`/.Parsescip-go gomod github.com/golang/go/src . `net/url`/Parse().("generic://api.example.com/webhook?template=json")
63
64 err := service.Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Initialize().(serviceURL, logscip-go gomod github.com/golang/go/src . log/.Newscip-go gomod github.com/golang/go/src . log/New().(ioscip-go gomod github.com/golang/go/src . io/.Discardscip-go gomod github.com/golang/go/src . io/Discard., "", 0))
65 if err != nil {
66 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("Error:", err)
67
68 return
69 }
70
71 // Send a notification with title
72 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#{"title": "Alert"}
73
74 err = service.Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Send().("System is down", &params)
75 if err != nil {
76 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("Error:", err)
77
78 return
79 }
80
81 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("JSON notification sent successfully")
82 // Output: JSON notification sent successfully
83 }
84
85 // Example_customHeaders demonstrates sending with custom HTTP headers.
86 func Example_customHeadersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Example_customHeaders().() {
87 // Activate HTTP mocking for the example
88 httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.Activatescip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/Activate().()
89
90 defer httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.DeactivateAndResetscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/DeactivateAndReset().()
91
92 // Mock a successful webhook response that checks for custom header
93 httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.RegisterResponderscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/RegisterResponder().("POST", "https://api.example.com/webhook",
94 func(req *httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) (*httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#, error) {
95 if req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src . `net/http`/Header#Get().("Authorization") == "Bearer token123" &&
96 req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src . `net/http`/Header#Get().("X-Custom") == "value" {
97 return httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.NewStringResponsescip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/NewStringResponse().(200, "OK"), nil
98 }
99
100 return httpmockscip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/.NewStringResponsescip-go gomod github.com/jarcoal/httpmock v1.4.2 `github.com/jarcoal/httpmock`/NewStringResponse().(401, "Unauthorized"), nil
101 })
102
103 // Create and initialize the service with custom headers
104 service := &Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#{}
105 serviceURL, _ := urlscip-go gomod github.com/golang/go/src . `net/url`/.Parsescip-go gomod github.com/golang/go/src . `net/url`/Parse().(
106 "generic://api.example.com/webhook?@Authorization=Bearer%20token123&@X-Custom=value",
107 )
108
109 err := service.Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Initialize().(serviceURL, logscip-go gomod github.com/golang/go/src . log/.Newscip-go gomod github.com/golang/go/src . log/New().(ioscip-go gomod github.com/golang/go/src . io/.Discardscip-go gomod github.com/golang/go/src . io/Discard., "", 0))
110 if err != nil {
111 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("Error:", err)
112
113 return
114 }
115
116 // Send a notification
117 err = service.Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Send().("Authenticated message", nil)
118 if err != nil {
119 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("Error:", err)
120
121 return
122 }
123
124 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("Notification with custom headers sent successfully")
125 // Output: Notification with custom headers sent successfully
126 }
127
128 // Example_configurationParsing demonstrates parsing various configuration options from URL.
129 func Example_configurationParsingscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Example_configurationParsing().() {
130 // Create a service URL with various configuration options
131 serviceURL, _ := urlscip-go gomod github.com/golang/go/src . `net/url`/.Parsescip-go gomod github.com/golang/go/src . `net/url`/Parse().(
132 "generic://webhook.example.com/alert?template=json&disabletls=yes&method=POST&titlekey=alert_title&messagekey=alert_message&$source=shoutrrr&@Authorization=Bearer%20token",
133 )
134
135 // Initialize the service
136 service := &Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#{}
137
138 err := service.Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Initialize().(serviceURL, logscip-go gomod github.com/golang/go/src . log/.Newscip-go gomod github.com/golang/go/src . log/New().(ioscip-go gomod github.com/golang/go/src . io/.Discardscip-go gomod github.com/golang/go/src . io/Discard., "", 0))
139 if err != nil {
140 fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().("Error:", err)
141
142 return
143 }
144
145 // Display parsed configuration
146 fmtscip-go gomod github.com/golang/go/src . fmt/.Printfscip-go gomod github.com/golang/go/src . fmt/Printf().("Webhook URL: %s\n", service.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config..WebhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#WebhookURL().().Stringscip-go gomod github.com/golang/go/src . `net/url`/URL#String().())
147 fmtscip-go gomod github.com/golang/go/src . fmt/.Printfscip-go gomod github.com/golang/go/src . fmt/Printf().("Template: %s\n", service.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config..Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#Template.)
148 fmtscip-go gomod github.com/golang/go/src . fmt/.Printfscip-go gomod github.com/golang/go/src . fmt/Printf().("TLS Disabled: %t\n", service.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config..DisableTLSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#DisableTLS.)
149 fmtscip-go gomod github.com/golang/go/src . fmt/.Printfscip-go gomod github.com/golang/go/src . fmt/Printf().("Method: %s\n", service.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config..RequestMethodscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#RequestMethod.)
150 fmtscip-go gomod github.com/golang/go/src . fmt/.Printfscip-go gomod github.com/golang/go/src . fmt/Printf().("Title Key: %s\n", service.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config..TitleKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#TitleKey.)
151 fmtscip-go gomod github.com/golang/go/src . fmt/.Printfscip-go gomod github.com/golang/go/src . fmt/Printf().("Message Key: %s\n", service.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config..MessageKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#MessageKey.)
152 fmtscip-go gomod github.com/golang/go/src . fmt/.Printfscip-go gomod github.com/golang/go/src . fmt/Printf().("Extra Data: %v\n", service.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config..extraDatascip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#extraData.)
153 fmtscip-go gomod github.com/golang/go/src . fmt/.Printfscip-go gomod github.com/golang/go/src . fmt/Printf().("Headers: %v\n", service.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config..headersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#headers.)
154
155 // Output:
156 // Webhook URL: http://webhook.example.com/alert
157 // Template: json
158 // TLS Disabled: true
159 // Method: POST
160 // Title Key: alert_title
161 // Message Key: alert_message
162 // Extra Data: map[source:shoutrrr]
163 // Headers: map[Authorization:Bearer token]
164 }