ntfy/client/options.go

66 lines
1.3 KiB
Go
Raw Normal View History

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