options.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package client
  2. import (
  3. "net/http"
  4. )
  5. type RequestOption func(r *http.Request) error
  6. type PublishOption = RequestOption
  7. type SubscribeOption = RequestOption
  8. func WithTitle(title string) PublishOption {
  9. return WithHeader("X-Title", title)
  10. }
  11. func WithPriority(priority string) PublishOption {
  12. return WithHeader("X-Priority", priority)
  13. }
  14. func WithTags(tags string) PublishOption {
  15. return WithHeader("X-Tags", tags)
  16. }
  17. func WithDelay(delay string) PublishOption {
  18. return WithHeader("X-Delay", delay)
  19. }
  20. func WithNoCache() PublishOption {
  21. return WithHeader("X-Cache", "no")
  22. }
  23. func WithNoFirebase() PublishOption {
  24. return WithHeader("X-Firebase", "no")
  25. }
  26. func WithSince(since string) SubscribeOption {
  27. return WithQueryParam("since", since)
  28. }
  29. func WithPoll() SubscribeOption {
  30. return WithQueryParam("poll", "1")
  31. }
  32. func WithScheduled() SubscribeOption {
  33. return WithQueryParam("scheduled", "1")
  34. }
  35. func WithHeader(header, value string) RequestOption {
  36. return func(r *http.Request) error {
  37. if value != "" {
  38. r.Header.Set(header, value)
  39. }
  40. return nil
  41. }
  42. }
  43. func WithQueryParam(param, value string) RequestOption {
  44. return func(r *http.Request) error {
  45. if value != "" {
  46. q := r.URL.Query()
  47. q.Add(param, value)
  48. r.URL.RawQuery = q.Encode()
  49. }
  50. return nil
  51. }
  52. }