options.go 4.8 KB

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