Ver Fonte

refactor(googlereader): rename/unexport response types and functions

Frédéric Guillot há 8 meses atrás
pai
commit
5eab4753e8

+ 14 - 14
internal/googlereader/handler.go

@@ -200,7 +200,7 @@ func (h *handler) clientLoginHandler(w http.ResponseWriter, r *http.Request) {
 		slog.String("username", username),
 	)
 
-	result := login{SID: token, LSID: token, Auth: token}
+	result := loginResponse{SID: token, LSID: token, Auth: token}
 	if output == "json" {
 		json.OK(w, r, result)
 		return
@@ -387,7 +387,7 @@ func (h *handler) editTagHandler(w http.ResponseWriter, r *http.Request) {
 		}
 	}
 
-	OK(w, r)
+	sendOkayResponse(w)
 }
 
 func (h *handler) quickAddHandler(w http.ResponseWriter, r *http.Request) {
@@ -669,7 +669,7 @@ func (h *handler) editSubscriptionHandler(w http.ResponseWriter, r *http.Request
 		return
 	}
 
-	OK(w, r)
+	sendOkayResponse(w)
 }
 
 func (h *handler) streamItemContentsHandler(w http.ResponseWriter, r *http.Request) {
@@ -731,7 +731,7 @@ func (h *handler) streamItemContentsHandler(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
-	result := streamContentItems{
+	result := streamContentItemsResponse{
 		Direction: "ltr",
 		ID:        "user/-/state/com.google/reading-list",
 		Title:     "Reading List",
@@ -843,7 +843,7 @@ func (h *handler) disableTagHandler(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	OK(w, r)
+	sendOkayResponse(w)
 }
 
 func (h *handler) renameTagHandler(w http.ResponseWriter, r *http.Request) {
@@ -910,7 +910,7 @@ func (h *handler) renameTagHandler(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	OK(w, r)
+	sendOkayResponse(w)
 }
 
 func (h *handler) tagListHandler(w http.ResponseWriter, r *http.Request) {
@@ -934,12 +934,12 @@ func (h *handler) tagListHandler(w http.ResponseWriter, r *http.Request) {
 		json.ServerError(w, r, err)
 		return
 	}
-	result.Tags = make([]subscriptionCategory, 0)
-	result.Tags = append(result.Tags, subscriptionCategory{
+	result.Tags = make([]subscriptionCategoryResponse, 0)
+	result.Tags = append(result.Tags, subscriptionCategoryResponse{
 		ID: fmt.Sprintf(userStreamPrefix, userID) + starredStreamSuffix,
 	})
 	for _, category := range categories {
-		result.Tags = append(result.Tags, subscriptionCategory{
+		result.Tags = append(result.Tags, subscriptionCategoryResponse{
 			ID:    fmt.Sprintf(userLabelPrefix, userID) + category.Title,
 			Label: category.Title,
 			Type:  "folder",
@@ -970,13 +970,13 @@ func (h *handler) subscriptionListHandler(w http.ResponseWriter, r *http.Request
 		return
 	}
 
-	result.Subscriptions = make([]subscription, 0)
+	result.Subscriptions = make([]subscriptionResponse, 0)
 	for _, feed := range feeds {
-		result.Subscriptions = append(result.Subscriptions, subscription{
+		result.Subscriptions = append(result.Subscriptions, subscriptionResponse{
 			ID:         fmt.Sprintf(feedPrefix+"%d", feed.ID),
 			Title:      feed.Title,
 			URL:        feed.FeedURL,
-			Categories: []subscriptionCategory{{fmt.Sprintf(userLabelPrefix, userID) + feed.Category.Title, feed.Category.Title, "folder"}},
+			Categories: []subscriptionCategoryResponse{{fmt.Sprintf(userLabelPrefix, userID) + feed.Category.Title, feed.Category.Title, "folder"}},
 			HTMLURL:    feed.SiteURL,
 			IconURL:    h.feedIconURL(feed),
 		})
@@ -1015,7 +1015,7 @@ func (h *handler) userInfoHandler(w http.ResponseWriter, r *http.Request) {
 		json.ServerError(w, r, err)
 		return
 	}
-	userInfo := userInfo{UserID: fmt.Sprint(user.ID), UserName: user.Username, UserProfileID: fmt.Sprint(user.ID), UserEmail: user.Username}
+	userInfo := userInfoResponse{UserID: fmt.Sprint(user.ID), UserName: user.Username, UserProfileID: fmt.Sprint(user.ID), UserEmail: user.Username}
 	json.OK(w, r, userInfo)
 }
 
@@ -1339,5 +1339,5 @@ func (h *handler) markAllAsReadHandler(w http.ResponseWriter, r *http.Request) {
 		}
 	}
 
-	OK(w, r)
+	sendOkayResponse(w)
 }

+ 12 - 12
internal/googlereader/middleware.go

@@ -51,7 +51,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 					slog.String("user_agent", r.UserAgent()),
 					slog.Any("error", err),
 				)
-				Unauthorized(w, r)
+				sendUnauthorizedResponse(w)
 				return
 			}
 
@@ -62,7 +62,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 					slog.String("client_ip", clientIP),
 					slog.String("user_agent", r.UserAgent()),
 				)
-				Unauthorized(w, r)
+				sendUnauthorizedResponse(w)
 				return
 			}
 		} else {
@@ -74,7 +74,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 					slog.String("client_ip", clientIP),
 					slog.String("user_agent", r.UserAgent()),
 				)
-				Unauthorized(w, r)
+				sendUnauthorizedResponse(w)
 				return
 			}
 			fields := strings.Fields(authorization)
@@ -84,7 +84,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 					slog.String("client_ip", clientIP),
 					slog.String("user_agent", r.UserAgent()),
 				)
-				Unauthorized(w, r)
+				sendUnauthorizedResponse(w)
 				return
 			}
 			if fields[0] != "GoogleLogin" {
@@ -93,7 +93,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 					slog.String("client_ip", clientIP),
 					slog.String("user_agent", r.UserAgent()),
 				)
-				Unauthorized(w, r)
+				sendUnauthorizedResponse(w)
 				return
 			}
 			auths := strings.Split(fields[1], "=")
@@ -103,7 +103,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 					slog.String("client_ip", clientIP),
 					slog.String("user_agent", r.UserAgent()),
 				)
-				Unauthorized(w, r)
+				sendUnauthorizedResponse(w)
 				return
 			}
 			if auths[0] != "auth" {
@@ -112,7 +112,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 					slog.String("client_ip", clientIP),
 					slog.String("user_agent", r.UserAgent()),
 				)
-				Unauthorized(w, r)
+				sendUnauthorizedResponse(w)
 				return
 			}
 			token = auths[1]
@@ -126,7 +126,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 				slog.String("user_agent", r.UserAgent()),
 				slog.String("token", token),
 			)
-			Unauthorized(w, r)
+			sendUnauthorizedResponse(w)
 			return
 		}
 		var integration *model.Integration
@@ -139,7 +139,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 				slog.String("user_agent", r.UserAgent()),
 				slog.Any("error", err),
 			)
-			Unauthorized(w, r)
+			sendUnauthorizedResponse(w)
 			return
 		}
 		expectedToken := getAuthToken(integration.GoogleReaderUsername, integration.GoogleReaderPassword)
@@ -149,7 +149,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 				slog.String("client_ip", clientIP),
 				slog.String("user_agent", r.UserAgent()),
 			)
-			Unauthorized(w, r)
+			sendUnauthorizedResponse(w)
 			return
 		}
 		if user, err = m.store.UserByID(integration.UserID); err != nil {
@@ -159,7 +159,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 				slog.String("user_agent", r.UserAgent()),
 				slog.Any("error", err),
 			)
-			Unauthorized(w, r)
+			sendUnauthorizedResponse(w)
 			return
 		}
 
@@ -169,7 +169,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 				slog.String("client_ip", clientIP),
 				slog.String("user_agent", r.UserAgent()),
 			)
-			Unauthorized(w, r)
+			sendUnauthorizedResponse(w)
 			return
 		}
 

+ 26 - 33
internal/googlereader/response.go

@@ -6,34 +6,36 @@ package googlereader // import "miniflux.app/v2/internal/googlereader"
 import (
 	"fmt"
 	"net/http"
-
-	"miniflux.app/v2/internal/http/response"
 )
 
-type login struct {
+type loginResponse struct {
 	SID  string `json:"SID,omitempty"`
 	LSID string `json:"LSID,omitempty"`
 	Auth string `json:"Auth,omitempty"`
 }
 
-func (l login) String() string {
+func (l loginResponse) String() string {
 	return fmt.Sprintf("SID=%s\nLSID=%s\nAuth=%s\n", l.SID, l.LSID, l.Auth)
 }
 
-type userInfo struct {
+type userInfoResponse struct {
 	UserID        string `json:"userId"`
 	UserName      string `json:"userName"`
 	UserProfileID string `json:"userProfileId"`
 	UserEmail     string `json:"userEmail"`
 }
 
-type subscription struct {
-	ID         string                 `json:"id"`
-	Title      string                 `json:"title"`
-	Categories []subscriptionCategory `json:"categories"`
-	URL        string                 `json:"url"`
-	HTMLURL    string                 `json:"htmlUrl"`
-	IconURL    string                 `json:"iconUrl"`
+type subscriptionResponse struct {
+	ID         string                         `json:"id"`
+	Title      string                         `json:"title"`
+	Categories []subscriptionCategoryResponse `json:"categories"`
+	URL        string                         `json:"url"`
+	HTMLURL    string                         `json:"htmlUrl"`
+	IconURL    string                         `json:"iconUrl"`
+}
+
+type subscriptionsResponse struct {
+	Subscriptions []subscriptionResponse `json:"subscriptions"`
 }
 
 type quickAddResponse struct {
@@ -43,14 +45,11 @@ type quickAddResponse struct {
 	StreamName string `json:"streamName,omitempty"`
 }
 
-type subscriptionCategory struct {
+type subscriptionCategoryResponse struct {
 	ID    string `json:"id"`
 	Label string `json:"label,omitempty"`
 	Type  string `json:"type,omitempty"`
 }
-type subscriptionsResponse struct {
-	Subscriptions []subscription `json:"subscriptions"`
-}
 
 type itemRef struct {
 	ID              string `json:"id"`
@@ -64,10 +63,10 @@ type streamIDResponse struct {
 }
 
 type tagsResponse struct {
-	Tags []subscriptionCategory `json:"tags"`
+	Tags []subscriptionCategoryResponse `json:"tags"`
 }
 
-type streamContentItems struct {
+type streamContentItemsResponse struct {
 	Direction string        `json:"direction"`
 	ID        string        `json:"id"`
 	Title     string        `json:"title"`
@@ -118,21 +117,15 @@ type contentItemOrigin struct {
 	HTMLUrl  string `json:"htmlUrl"`
 }
 
-// Unauthorized sends a not authorized error to the client.
-func Unauthorized(w http.ResponseWriter, r *http.Request) {
-	builder := response.New(w, r)
-	builder.WithStatus(http.StatusUnauthorized)
-	builder.WithHeader("Content-Type", "text/plain")
-	builder.WithHeader("X-Reader-Google-Bad-Token", "true")
-	builder.WithBody("Unauthorized")
-	builder.Write()
+func sendUnauthorizedResponse(w http.ResponseWriter) {
+	w.Header().Set("Content-Type", "text/plain")
+	w.Header().Set("X-Reader-Google-Bad-Token", "true")
+	w.WriteHeader(http.StatusUnauthorized)
+	w.Write([]byte("Unauthorized"))
 }
 
-// OK sends a ok response to the client.
-func OK(w http.ResponseWriter, r *http.Request) {
-	builder := response.New(w, r)
-	builder.WithStatus(http.StatusOK)
-	builder.WithHeader("Content-Type", "text/plain")
-	builder.WithBody("OK")
-	builder.Write()
+func sendOkayResponse(w http.ResponseWriter) {
+	w.Header().Set("Content-Type", "text/plain")
+	w.WriteHeader(http.StatusOK)
+	w.Write([]byte("OK"))
 }