options.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package client
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "time"
  7. )
  8. // RequestOption is a generic request option that can be added to Client calls
  9. type RequestOption = func(r *http.Request) error
  10. // PublishOption is an option that can be passed to the Client.Publish call
  11. type PublishOption = RequestOption
  12. // SubscribeOption is an option that can be passed to a Client.Subscribe or Client.Poll call
  13. type SubscribeOption = RequestOption
  14. // WithTitle adds a title to a message
  15. func WithTitle(title string) PublishOption {
  16. return WithHeader("X-Title", title)
  17. }
  18. // WithPriority adds a priority to a message. The priority can be either a number (1=min, 5=max),
  19. // or the corresponding names (see util.ParsePriority).
  20. func WithPriority(priority string) PublishOption {
  21. return WithHeader("X-Priority", priority)
  22. }
  23. // WithTagsList adds a list of tags to a message. The tags parameter must be a comma-separated list
  24. // of tags. To use a slice, use WithTags instead
  25. func WithTagsList(tags string) PublishOption {
  26. return WithHeader("X-Tags", tags)
  27. }
  28. // WithTags adds a list of a tags to a message
  29. func WithTags(tags []string) PublishOption {
  30. return WithTagsList(strings.Join(tags, ","))
  31. }
  32. // WithDelay instructs the server to send the message at a later date. The delay parameter can be a
  33. // Unix timestamp, a duration string or a natural langage string. See https://ntfy.sh/docs/publish/#scheduled-delivery
  34. // for details.
  35. func WithDelay(delay string) PublishOption {
  36. return WithHeader("X-Delay", delay)
  37. }
  38. // WithNoCache instructs the server not to cache the message server-side
  39. func WithNoCache() PublishOption {
  40. return WithHeader("X-Cache", "no")
  41. }
  42. // WithNoFirebase instructs the server not to forward the message to Firebase
  43. func WithNoFirebase() PublishOption {
  44. return WithHeader("X-Firebase", "no")
  45. }
  46. // WithSince limits the number of messages returned from the server. The parameter since can be a Unix
  47. // timestamp (see WithSinceUnixTime), a duration (WithSinceDuration) the word "all" (see WithSinceAll).
  48. func WithSince(since string) SubscribeOption {
  49. return WithQueryParam("since", since)
  50. }
  51. // WithSinceAll instructs the server to return all messages for the given topic from the server
  52. func WithSinceAll() SubscribeOption {
  53. return WithSince("all")
  54. }
  55. // WithSinceDuration instructs the server to return all messages since the given duration ago
  56. func WithSinceDuration(since time.Duration) SubscribeOption {
  57. return WithSinceUnixTime(time.Now().Add(-1 * since).Unix())
  58. }
  59. // WithSinceUnixTime instructs the server to return only messages newer or equal to the given timestamp
  60. func WithSinceUnixTime(since int64) SubscribeOption {
  61. return WithSince(fmt.Sprintf("%d", since))
  62. }
  63. // WithPoll instructs the server to close the connection after messages have been returned. Don't use this option
  64. // directly. Use Client.Poll instead.
  65. func WithPoll() SubscribeOption {
  66. return WithQueryParam("poll", "1")
  67. }
  68. // WithScheduled instructs the server to also return messages that have not been sent yet, i.e. delayed/scheduled
  69. // messages (see WithDelay). The messages will have a future date.
  70. func WithScheduled() SubscribeOption {
  71. return WithQueryParam("scheduled", "1")
  72. }
  73. // WithHeader is a generic option to add headers to a request
  74. func WithHeader(header, value string) RequestOption {
  75. return func(r *http.Request) error {
  76. if value != "" {
  77. r.Header.Set(header, value)
  78. }
  79. return nil
  80. }
  81. }
  82. // WithQueryParam is a generic option to add query parameters to a request
  83. func WithQueryParam(param, value string) RequestOption {
  84. return func(r *http.Request) error {
  85. if value != "" {
  86. q := r.URL.Query()
  87. q.Add(param, value)
  88. r.URL.RawQuery = q.Encode()
  89. }
  90. return nil
  91. }
  92. }