response.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader // import "miniflux.app/v2/internal/googlereader"
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "miniflux.app/v2/internal/http/response"
  9. )
  10. type loginResponse struct {
  11. SID string `json:"SID,omitempty"`
  12. LSID string `json:"LSID,omitempty"`
  13. Auth string `json:"Auth,omitempty"`
  14. }
  15. func (l loginResponse) String() string {
  16. return fmt.Sprintf("SID=%s\nLSID=%s\nAuth=%s\n", l.SID, l.LSID, l.Auth)
  17. }
  18. type userInfoResponse struct {
  19. UserID string `json:"userId"`
  20. UserName string `json:"userName"`
  21. UserProfileID string `json:"userProfileId"`
  22. UserEmail string `json:"userEmail"`
  23. }
  24. type subscriptionResponse struct {
  25. ID string `json:"id"`
  26. Title string `json:"title"`
  27. Categories []subscriptionCategoryResponse `json:"categories"`
  28. URL string `json:"url"`
  29. HTMLURL string `json:"htmlUrl"`
  30. IconURL string `json:"iconUrl"`
  31. }
  32. type subscriptionsResponse struct {
  33. Subscriptions []subscriptionResponse `json:"subscriptions"`
  34. }
  35. type quickAddResponse struct {
  36. NumResults int64 `json:"numResults"`
  37. Query string `json:"query,omitempty"`
  38. StreamID string `json:"streamId,omitempty"`
  39. StreamName string `json:"streamName,omitempty"`
  40. }
  41. type subscriptionCategoryResponse struct {
  42. ID string `json:"id"`
  43. Label string `json:"label,omitempty"`
  44. Type string `json:"type,omitempty"`
  45. }
  46. type itemRef struct {
  47. ID string `json:"id"`
  48. DirectStreamIDs string `json:"directStreamIds,omitempty"`
  49. TimestampUsec string `json:"timestampUsec,omitempty"`
  50. }
  51. type streamIDResponse struct {
  52. ItemRefs []itemRef `json:"itemRefs"`
  53. Continuation int `json:"continuation,omitempty,string"`
  54. }
  55. type tagsResponse struct {
  56. Tags []subscriptionCategoryResponse `json:"tags"`
  57. }
  58. type streamContentItemsResponse struct {
  59. Direction string `json:"direction"`
  60. ID string `json:"id"`
  61. Title string `json:"title"`
  62. Self []contentHREF `json:"self"`
  63. Updated int64 `json:"updated"`
  64. Items []contentItem `json:"items"`
  65. Author string `json:"author"`
  66. }
  67. type contentItem struct {
  68. ID string `json:"id"`
  69. Categories []string `json:"categories"`
  70. Title string `json:"title"`
  71. CrawlTimeMsec string `json:"crawlTimeMsec"`
  72. TimestampUsec string `json:"timestampUsec"`
  73. Published int64 `json:"published"`
  74. Updated int64 `json:"updated"`
  75. Author string `json:"author"`
  76. Alternate []contentHREFType `json:"alternate"`
  77. Summary contentItemContent `json:"summary"`
  78. Content contentItemContent `json:"content"`
  79. Origin contentItemOrigin `json:"origin"`
  80. Enclosure []contentItemEnclosure `json:"enclosure"`
  81. Canonical []contentHREF `json:"canonical"`
  82. }
  83. type contentHREFType struct {
  84. HREF string `json:"href"`
  85. Type string `json:"type"`
  86. }
  87. type contentHREF struct {
  88. HREF string `json:"href"`
  89. }
  90. type contentItemEnclosure struct {
  91. URL string `json:"url"`
  92. Type string `json:"type"`
  93. }
  94. type contentItemContent struct {
  95. Direction string `json:"direction"`
  96. Content string `json:"content"`
  97. }
  98. type contentItemOrigin struct {
  99. StreamID string `json:"streamId"`
  100. Title string `json:"title"`
  101. HTMLUrl string `json:"htmlUrl"`
  102. }
  103. // sendErrorResponse responds with 400 when the request caused the error, and with 500 otherwise.
  104. func sendErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
  105. switch {
  106. case errors.Is(err, errEmptyFeedTitle),
  107. errors.Is(err, errFeedNotFound),
  108. errors.Is(err, errCategoryNotFound),
  109. errors.Is(err, errInvalidFeedStream),
  110. errors.Is(err, errInvalidSubscription):
  111. response.JSONBadRequest(w, r, err)
  112. default:
  113. response.JSONServerError(w, r, err)
  114. }
  115. }
  116. func sendUnauthorizedResponse(w http.ResponseWriter, r *http.Request) {
  117. response.NewBuilder(w, r).
  118. WithStatus(http.StatusUnauthorized).
  119. WithHeader("X-Reader-Google-Bad-Token", "true").
  120. WithHeader("Content-Type", "text/plain; charset=utf-8").
  121. WithBodyAsString("Unauthorized").
  122. Write()
  123. }