options.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // WithClick makes the notification action open the given URL as opposed to entering the detail view
  39. func WithClick(url string) PublishOption {
  40. return WithHeader("X-Click", url)
  41. }
  42. // WithEmail instructs the server to also send the message to the given e-mail address
  43. func WithEmail(email string) PublishOption {
  44. return WithHeader("X-Email", email)
  45. }
  46. // WithNoCache instructs the server not to cache the message server-side
  47. func WithNoCache() PublishOption {
  48. return WithHeader("X-Cache", "no")
  49. }
  50. // WithNoFirebase instructs the server not to forward the message to Firebase
  51. func WithNoFirebase() PublishOption {
  52. return WithHeader("X-Firebase", "no")
  53. }
  54. // WithSince limits the number of messages returned from the server. The parameter since can be a Unix
  55. // timestamp (see WithSinceUnixTime), a duration (WithSinceDuration) the word "all" (see WithSinceAll).
  56. func WithSince(since string) SubscribeOption {
  57. return WithQueryParam("since", since)
  58. }
  59. // WithSinceAll instructs the server to return all messages for the given topic from the server
  60. func WithSinceAll() SubscribeOption {
  61. return WithSince("all")
  62. }
  63. // WithSinceDuration instructs the server to return all messages since the given duration ago
  64. func WithSinceDuration(since time.Duration) SubscribeOption {
  65. return WithSinceUnixTime(time.Now().Add(-1 * since).Unix())
  66. }
  67. // WithSinceUnixTime instructs the server to return only messages newer or equal to the given timestamp
  68. func WithSinceUnixTime(since int64) SubscribeOption {
  69. return WithSince(fmt.Sprintf("%d", since))
  70. }
  71. // WithPoll instructs the server to close the connection after messages have been returned. Don't use this option
  72. // directly. Use Client.Poll instead.
  73. func WithPoll() SubscribeOption {
  74. return WithQueryParam("poll", "1")
  75. }
  76. // WithScheduled instructs the server to also return messages that have not been sent yet, i.e. delayed/scheduled
  77. // messages (see WithDelay). The messages will have a future date.
  78. func WithScheduled() SubscribeOption {
  79. return WithQueryParam("scheduled", "1")
  80. }
  81. // WithFilter is a generic subscribe option meant to be used to filter for certain messages only
  82. func WithFilter(param, value string) SubscribeOption {
  83. return WithQueryParam(param, value)
  84. }
  85. // WithMessageFilter instructs the server to only return messages that match the exact message
  86. func WithMessageFilter(message string) SubscribeOption {
  87. return WithQueryParam("message", message)
  88. }
  89. // WithTitleFilter instructs the server to only return messages with a title that match the exact string
  90. func WithTitleFilter(title string) SubscribeOption {
  91. return WithQueryParam("title", title)
  92. }
  93. // WithPriorityFilter instructs the server to only return messages with the matching priority. Not that messages
  94. // without priority also implicitly match priority 3.
  95. func WithPriorityFilter(priority int) SubscribeOption {
  96. return WithQueryParam("priority", fmt.Sprintf("%d", priority))
  97. }
  98. // WithTagsFilter instructs the server to only return messages that contain all of the given tags
  99. func WithTagsFilter(tags []string) SubscribeOption {
  100. return WithQueryParam("tags", strings.Join(tags, ","))
  101. }
  102. // WithHeader is a generic option to add headers to a request
  103. func WithHeader(header, value string) RequestOption {
  104. return func(r *http.Request) error {
  105. if value != "" {
  106. r.Header.Set(header, value)
  107. }
  108. return nil
  109. }
  110. }
  111. // WithQueryParam is a generic option to add query parameters to a request
  112. func WithQueryParam(param, value string) RequestOption {
  113. return func(r *http.Request) error {
  114. if value != "" {
  115. q := r.URL.Query()
  116. q.Add(param, value)
  117. r.URL.RawQuery = q.Encode()
  118. }
  119. return nil
  120. }
  121. }