options.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // WithMessage sets the notification message. This is an alternative way to passing the message body.
  15. func WithMessage(message string) PublishOption {
  16. return WithHeader("X-Message", message)
  17. }
  18. // WithTitle adds a title to a message
  19. func WithTitle(title string) PublishOption {
  20. return WithHeader("X-Title", title)
  21. }
  22. // WithPriority adds a priority to a message. The priority can be either a number (1=min, 5=max),
  23. // or the corresponding names (see util.ParsePriority).
  24. func WithPriority(priority string) PublishOption {
  25. return WithHeader("X-Priority", priority)
  26. }
  27. // WithTagsList adds a list of tags to a message. The tags parameter must be a comma-separated list
  28. // of tags. To use a slice, use WithTags instead
  29. func WithTagsList(tags string) PublishOption {
  30. return WithHeader("X-Tags", tags)
  31. }
  32. // WithTags adds a list of a tags to a message
  33. func WithTags(tags []string) PublishOption {
  34. return WithTagsList(strings.Join(tags, ","))
  35. }
  36. // WithDelay instructs the server to send the message at a later date. The delay parameter can be a
  37. // Unix timestamp, a duration string or a natural langage string. See https://ntfy.sh/docs/publish/#scheduled-delivery
  38. // for details.
  39. func WithDelay(delay string) PublishOption {
  40. return WithHeader("X-Delay", delay)
  41. }
  42. // WithClick makes the notification action open the given URL as opposed to entering the detail view
  43. func WithClick(url string) PublishOption {
  44. return WithHeader("X-Click", url)
  45. }
  46. // WithAttach sets a URL that will be used by the client to download an attachment
  47. func WithAttach(attach string) PublishOption {
  48. return WithHeader("X-Attach", attach)
  49. }
  50. // WithFilename sets a filename for the attachment, and/or forces the HTTP body to interpreted as an attachment
  51. func WithFilename(filename string) PublishOption {
  52. return WithHeader("X-Filename", filename)
  53. }
  54. // WithEmail instructs the server to also send the message to the given e-mail address
  55. func WithEmail(email string) PublishOption {
  56. return WithHeader("X-Email", email)
  57. }
  58. // WithNoCache instructs the server not to cache the message server-side
  59. func WithNoCache() PublishOption {
  60. return WithHeader("X-Cache", "no")
  61. }
  62. // WithNoFirebase instructs the server not to forward the message to Firebase
  63. func WithNoFirebase() PublishOption {
  64. return WithHeader("X-Firebase", "no")
  65. }
  66. // WithSince limits the number of messages returned from the server. The parameter since can be a Unix
  67. // timestamp (see WithSinceUnixTime), a duration (WithSinceDuration) the word "all" (see WithSinceAll).
  68. func WithSince(since string) SubscribeOption {
  69. return WithQueryParam("since", since)
  70. }
  71. // WithSinceAll instructs the server to return all messages for the given topic from the server
  72. func WithSinceAll() SubscribeOption {
  73. return WithSince("all")
  74. }
  75. // WithSinceDuration instructs the server to return all messages since the given duration ago
  76. func WithSinceDuration(since time.Duration) SubscribeOption {
  77. return WithSinceUnixTime(time.Now().Add(-1 * since).Unix())
  78. }
  79. // WithSinceUnixTime instructs the server to return only messages newer or equal to the given timestamp
  80. func WithSinceUnixTime(since int64) SubscribeOption {
  81. return WithSince(fmt.Sprintf("%d", since))
  82. }
  83. // WithPoll instructs the server to close the connection after messages have been returned. Don't use this option
  84. // directly. Use Client.Poll instead.
  85. func WithPoll() SubscribeOption {
  86. return WithQueryParam("poll", "1")
  87. }
  88. // WithScheduled instructs the server to also return messages that have not been sent yet, i.e. delayed/scheduled
  89. // messages (see WithDelay). The messages will have a future date.
  90. func WithScheduled() SubscribeOption {
  91. return WithQueryParam("scheduled", "1")
  92. }
  93. // WithFilter is a generic subscribe option meant to be used to filter for certain messages only
  94. func WithFilter(param, value string) SubscribeOption {
  95. return WithQueryParam(param, value)
  96. }
  97. // WithMessageFilter instructs the server to only return messages that match the exact message
  98. func WithMessageFilter(message string) SubscribeOption {
  99. return WithQueryParam("message", message)
  100. }
  101. // WithTitleFilter instructs the server to only return messages with a title that match the exact string
  102. func WithTitleFilter(title string) SubscribeOption {
  103. return WithQueryParam("title", title)
  104. }
  105. // WithPriorityFilter instructs the server to only return messages with the matching priority. Not that messages
  106. // without priority also implicitly match priority 3.
  107. func WithPriorityFilter(priority int) SubscribeOption {
  108. return WithQueryParam("priority", fmt.Sprintf("%d", priority))
  109. }
  110. // WithTagsFilter instructs the server to only return messages that contain all of the given tags
  111. func WithTagsFilter(tags []string) SubscribeOption {
  112. return WithQueryParam("tags", strings.Join(tags, ","))
  113. }
  114. // WithHeader is a generic option to add headers to a request
  115. func WithHeader(header, value string) RequestOption {
  116. return func(r *http.Request) error {
  117. if value != "" {
  118. r.Header.Set(header, value)
  119. }
  120. return nil
  121. }
  122. }
  123. // WithQueryParam is a generic option to add query parameters to a request
  124. func WithQueryParam(param, value string) RequestOption {
  125. return func(r *http.Request) error {
  126. if value != "" {
  127. q := r.URL.Query()
  128. q.Add(param, value)
  129. r.URL.RawQuery = q.Encode()
  130. }
  131. return nil
  132. }
  133. }