1
0
Fork 0
forked from mirrors/ntfy
ntfy/client/options.go

66 lines
1.3 KiB
Go
Raw Normal View History

2021-12-17 01:33:01 +00:00
package client
import (
"net/http"
)
2021-12-17 14:32:59 +00:00
type RequestOption func(r *http.Request) error
type PublishOption = RequestOption
type SubscribeOption = RequestOption
2021-12-17 01:33:01 +00:00
func WithTitle(title string) PublishOption {
2021-12-17 14:32:59 +00:00
return WithHeader("X-Title", title)
2021-12-17 01:33:01 +00:00
}
func WithPriority(priority string) PublishOption {
2021-12-17 14:32:59 +00:00
return WithHeader("X-Priority", priority)
2021-12-17 01:33:01 +00:00
}
func WithTags(tags string) PublishOption {
2021-12-17 14:32:59 +00:00
return WithHeader("X-Tags", tags)
2021-12-17 01:33:01 +00:00
}
func WithDelay(delay string) PublishOption {
2021-12-17 14:32:59 +00:00
return WithHeader("X-Delay", delay)
2021-12-17 01:33:01 +00:00
}
func WithNoCache() PublishOption {
return WithHeader("X-Cache", "no")
}
func WithNoFirebase() PublishOption {
return WithHeader("X-Firebase", "no")
}
2021-12-17 14:32:59 +00:00
func WithSince(since string) SubscribeOption {
return WithQueryParam("since", since)
}
func WithPoll() SubscribeOption {
return WithQueryParam("poll", "1")
}
func WithScheduled() SubscribeOption {
return WithQueryParam("scheduled", "1")
}
func WithHeader(header, value string) RequestOption {
2021-12-17 01:33:01 +00:00
return func(r *http.Request) error {
2021-12-17 14:32:59 +00:00
if value != "" {
r.Header.Set(header, value)
}
2021-12-17 01:33:01 +00:00
return nil
}
}
2021-12-17 14:32:59 +00:00
func WithQueryParam(param, value string) RequestOption {
2021-12-17 01:33:01 +00:00
return func(r *http.Request) error {
2021-12-17 14:32:59 +00:00
if value != "" {
2021-12-17 01:33:01 +00:00
q := r.URL.Query()
2021-12-17 14:32:59 +00:00
q.Add(param, value)
2021-12-17 01:33:01 +00:00
r.URL.RawQuery = q.Encode()
}
return nil
}
}