-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_action.go
58 lines (49 loc) · 1.79 KB
/
http_action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package gotfy
import (
"encoding/json"
"net/url"
)
// HttpAction allows attaching an HTTP request action to a notification
type HttpAction[X comparable] struct {
Label string `json:"label"` // string - Open garage door Label of the action button in the notification
URL *url.URL `json:"url"` // string - https://ntfy.sh/mytopic URL to which the HTTP request will be sent
Method string `json:"method"` // GET/POST/PUT/... POST GET HTTP method to use for request, default is POST ⚠️
Headers map[string]string `json:"headers"` // map of strings - see above HTTP headers to pass in request. When publishing as JSON, headers are passed as a map. When the simple format is used, use headers.<header1>=<value>.
Body X `json:"body"` // string empty some body, somebody? HTTP body
Clear bool `json:"clear"` // boolean false true Clear notification after HTTP request succeeds. If the request fails, the notification is not cleared.
}
func (h *HttpAction[X]) actionType() ActionButtonType {
return HTTP
}
func (h *HttpAction[X]) MarshalJSON() ([]byte, error) {
m := map[string]any{
"action": "http",
"label": h.Label,
"url": h.URL,
}
if meth := h.Method; meth != "" {
m["method"] = meth
}
if headers := h.Headers; len(headers) > 0 {
m["headers"] = headers
}
// double marshal body to work for HTTP requests.
// the api wants body to be a marshaled JSON string
// so double marshal to escape the string
var zeroVal X
if body := h.Body; body != zeroVal {
buf, err := json.Marshal(h.Body)
if err != nil {
return nil, err
}
str, err := json.Marshal(string(buf))
if err != nil {
return nil, err
}
m["body"] = str
}
if h.Clear {
m["clear"] = true
}
return json.Marshal(m)
}