Skip to content

Commit 5eab475

Browse files
committed
refactor(googlereader): rename/unexport response types and functions
1 parent bf46642 commit 5eab475

File tree

3 files changed

+52
-59
lines changed

3 files changed

+52
-59
lines changed

internal/googlereader/handler.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (h *handler) clientLoginHandler(w http.ResponseWriter, r *http.Request) {
200200
slog.String("username", username),
201201
)
202202

203-
result := login{SID: token, LSID: token, Auth: token}
203+
result := loginResponse{SID: token, LSID: token, Auth: token}
204204
if output == "json" {
205205
json.OK(w, r, result)
206206
return
@@ -387,7 +387,7 @@ func (h *handler) editTagHandler(w http.ResponseWriter, r *http.Request) {
387387
}
388388
}
389389

390-
OK(w, r)
390+
sendOkayResponse(w)
391391
}
392392

393393
func (h *handler) quickAddHandler(w http.ResponseWriter, r *http.Request) {
@@ -669,7 +669,7 @@ func (h *handler) editSubscriptionHandler(w http.ResponseWriter, r *http.Request
669669
return
670670
}
671671

672-
OK(w, r)
672+
sendOkayResponse(w)
673673
}
674674

675675
func (h *handler) streamItemContentsHandler(w http.ResponseWriter, r *http.Request) {
@@ -731,7 +731,7 @@ func (h *handler) streamItemContentsHandler(w http.ResponseWriter, r *http.Reque
731731
return
732732
}
733733

734-
result := streamContentItems{
734+
result := streamContentItemsResponse{
735735
Direction: "ltr",
736736
ID: "user/-/state/com.google/reading-list",
737737
Title: "Reading List",
@@ -843,7 +843,7 @@ func (h *handler) disableTagHandler(w http.ResponseWriter, r *http.Request) {
843843
return
844844
}
845845

846-
OK(w, r)
846+
sendOkayResponse(w)
847847
}
848848

849849
func (h *handler) renameTagHandler(w http.ResponseWriter, r *http.Request) {
@@ -910,7 +910,7 @@ func (h *handler) renameTagHandler(w http.ResponseWriter, r *http.Request) {
910910
return
911911
}
912912

913-
OK(w, r)
913+
sendOkayResponse(w)
914914
}
915915

916916
func (h *handler) tagListHandler(w http.ResponseWriter, r *http.Request) {
@@ -934,12 +934,12 @@ func (h *handler) tagListHandler(w http.ResponseWriter, r *http.Request) {
934934
json.ServerError(w, r, err)
935935
return
936936
}
937-
result.Tags = make([]subscriptionCategory, 0)
938-
result.Tags = append(result.Tags, subscriptionCategory{
937+
result.Tags = make([]subscriptionCategoryResponse, 0)
938+
result.Tags = append(result.Tags, subscriptionCategoryResponse{
939939
ID: fmt.Sprintf(userStreamPrefix, userID) + starredStreamSuffix,
940940
})
941941
for _, category := range categories {
942-
result.Tags = append(result.Tags, subscriptionCategory{
942+
result.Tags = append(result.Tags, subscriptionCategoryResponse{
943943
ID: fmt.Sprintf(userLabelPrefix, userID) + category.Title,
944944
Label: category.Title,
945945
Type: "folder",
@@ -970,13 +970,13 @@ func (h *handler) subscriptionListHandler(w http.ResponseWriter, r *http.Request
970970
return
971971
}
972972

973-
result.Subscriptions = make([]subscription, 0)
973+
result.Subscriptions = make([]subscriptionResponse, 0)
974974
for _, feed := range feeds {
975-
result.Subscriptions = append(result.Subscriptions, subscription{
975+
result.Subscriptions = append(result.Subscriptions, subscriptionResponse{
976976
ID: fmt.Sprintf(feedPrefix+"%d", feed.ID),
977977
Title: feed.Title,
978978
URL: feed.FeedURL,
979-
Categories: []subscriptionCategory{{fmt.Sprintf(userLabelPrefix, userID) + feed.Category.Title, feed.Category.Title, "folder"}},
979+
Categories: []subscriptionCategoryResponse{{fmt.Sprintf(userLabelPrefix, userID) + feed.Category.Title, feed.Category.Title, "folder"}},
980980
HTMLURL: feed.SiteURL,
981981
IconURL: h.feedIconURL(feed),
982982
})
@@ -1015,7 +1015,7 @@ func (h *handler) userInfoHandler(w http.ResponseWriter, r *http.Request) {
10151015
json.ServerError(w, r, err)
10161016
return
10171017
}
1018-
userInfo := userInfo{UserID: fmt.Sprint(user.ID), UserName: user.Username, UserProfileID: fmt.Sprint(user.ID), UserEmail: user.Username}
1018+
userInfo := userInfoResponse{UserID: fmt.Sprint(user.ID), UserName: user.Username, UserProfileID: fmt.Sprint(user.ID), UserEmail: user.Username}
10191019
json.OK(w, r, userInfo)
10201020
}
10211021

@@ -1339,5 +1339,5 @@ func (h *handler) markAllAsReadHandler(w http.ResponseWriter, r *http.Request) {
13391339
}
13401340
}
13411341

1342-
OK(w, r)
1342+
sendOkayResponse(w)
13431343
}

internal/googlereader/middleware.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
5151
slog.String("user_agent", r.UserAgent()),
5252
slog.Any("error", err),
5353
)
54-
Unauthorized(w, r)
54+
sendUnauthorizedResponse(w)
5555
return
5656
}
5757

@@ -62,7 +62,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
6262
slog.String("client_ip", clientIP),
6363
slog.String("user_agent", r.UserAgent()),
6464
)
65-
Unauthorized(w, r)
65+
sendUnauthorizedResponse(w)
6666
return
6767
}
6868
} else {
@@ -74,7 +74,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
7474
slog.String("client_ip", clientIP),
7575
slog.String("user_agent", r.UserAgent()),
7676
)
77-
Unauthorized(w, r)
77+
sendUnauthorizedResponse(w)
7878
return
7979
}
8080
fields := strings.Fields(authorization)
@@ -84,7 +84,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
8484
slog.String("client_ip", clientIP),
8585
slog.String("user_agent", r.UserAgent()),
8686
)
87-
Unauthorized(w, r)
87+
sendUnauthorizedResponse(w)
8888
return
8989
}
9090
if fields[0] != "GoogleLogin" {
@@ -93,7 +93,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
9393
slog.String("client_ip", clientIP),
9494
slog.String("user_agent", r.UserAgent()),
9595
)
96-
Unauthorized(w, r)
96+
sendUnauthorizedResponse(w)
9797
return
9898
}
9999
auths := strings.Split(fields[1], "=")
@@ -103,7 +103,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
103103
slog.String("client_ip", clientIP),
104104
slog.String("user_agent", r.UserAgent()),
105105
)
106-
Unauthorized(w, r)
106+
sendUnauthorizedResponse(w)
107107
return
108108
}
109109
if auths[0] != "auth" {
@@ -112,7 +112,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
112112
slog.String("client_ip", clientIP),
113113
slog.String("user_agent", r.UserAgent()),
114114
)
115-
Unauthorized(w, r)
115+
sendUnauthorizedResponse(w)
116116
return
117117
}
118118
token = auths[1]
@@ -126,7 +126,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
126126
slog.String("user_agent", r.UserAgent()),
127127
slog.String("token", token),
128128
)
129-
Unauthorized(w, r)
129+
sendUnauthorizedResponse(w)
130130
return
131131
}
132132
var integration *model.Integration
@@ -139,7 +139,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
139139
slog.String("user_agent", r.UserAgent()),
140140
slog.Any("error", err),
141141
)
142-
Unauthorized(w, r)
142+
sendUnauthorizedResponse(w)
143143
return
144144
}
145145
expectedToken := getAuthToken(integration.GoogleReaderUsername, integration.GoogleReaderPassword)
@@ -149,7 +149,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
149149
slog.String("client_ip", clientIP),
150150
slog.String("user_agent", r.UserAgent()),
151151
)
152-
Unauthorized(w, r)
152+
sendUnauthorizedResponse(w)
153153
return
154154
}
155155
if user, err = m.store.UserByID(integration.UserID); err != nil {
@@ -159,7 +159,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
159159
slog.String("user_agent", r.UserAgent()),
160160
slog.Any("error", err),
161161
)
162-
Unauthorized(w, r)
162+
sendUnauthorizedResponse(w)
163163
return
164164
}
165165

@@ -169,7 +169,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
169169
slog.String("client_ip", clientIP),
170170
slog.String("user_agent", r.UserAgent()),
171171
)
172-
Unauthorized(w, r)
172+
sendUnauthorizedResponse(w)
173173
return
174174
}
175175

internal/googlereader/response.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,36 @@ package googlereader // import "miniflux.app/v2/internal/googlereader"
66
import (
77
"fmt"
88
"net/http"
9-
10-
"miniflux.app/v2/internal/http/response"
119
)
1210

13-
type login struct {
11+
type loginResponse struct {
1412
SID string `json:"SID,omitempty"`
1513
LSID string `json:"LSID,omitempty"`
1614
Auth string `json:"Auth,omitempty"`
1715
}
1816

19-
func (l login) String() string {
17+
func (l loginResponse) String() string {
2018
return fmt.Sprintf("SID=%s\nLSID=%s\nAuth=%s\n", l.SID, l.LSID, l.Auth)
2119
}
2220

23-
type userInfo struct {
21+
type userInfoResponse struct {
2422
UserID string `json:"userId"`
2523
UserName string `json:"userName"`
2624
UserProfileID string `json:"userProfileId"`
2725
UserEmail string `json:"userEmail"`
2826
}
2927

30-
type subscription struct {
31-
ID string `json:"id"`
32-
Title string `json:"title"`
33-
Categories []subscriptionCategory `json:"categories"`
34-
URL string `json:"url"`
35-
HTMLURL string `json:"htmlUrl"`
36-
IconURL string `json:"iconUrl"`
28+
type subscriptionResponse struct {
29+
ID string `json:"id"`
30+
Title string `json:"title"`
31+
Categories []subscriptionCategoryResponse `json:"categories"`
32+
URL string `json:"url"`
33+
HTMLURL string `json:"htmlUrl"`
34+
IconURL string `json:"iconUrl"`
35+
}
36+
37+
type subscriptionsResponse struct {
38+
Subscriptions []subscriptionResponse `json:"subscriptions"`
3739
}
3840

3941
type quickAddResponse struct {
@@ -43,14 +45,11 @@ type quickAddResponse struct {
4345
StreamName string `json:"streamName,omitempty"`
4446
}
4547

46-
type subscriptionCategory struct {
48+
type subscriptionCategoryResponse struct {
4749
ID string `json:"id"`
4850
Label string `json:"label,omitempty"`
4951
Type string `json:"type,omitempty"`
5052
}
51-
type subscriptionsResponse struct {
52-
Subscriptions []subscription `json:"subscriptions"`
53-
}
5453

5554
type itemRef struct {
5655
ID string `json:"id"`
@@ -64,10 +63,10 @@ type streamIDResponse struct {
6463
}
6564

6665
type tagsResponse struct {
67-
Tags []subscriptionCategory `json:"tags"`
66+
Tags []subscriptionCategoryResponse `json:"tags"`
6867
}
6968

70-
type streamContentItems struct {
69+
type streamContentItemsResponse struct {
7170
Direction string `json:"direction"`
7271
ID string `json:"id"`
7372
Title string `json:"title"`
@@ -118,21 +117,15 @@ type contentItemOrigin struct {
118117
HTMLUrl string `json:"htmlUrl"`
119118
}
120119

121-
// Unauthorized sends a not authorized error to the client.
122-
func Unauthorized(w http.ResponseWriter, r *http.Request) {
123-
builder := response.New(w, r)
124-
builder.WithStatus(http.StatusUnauthorized)
125-
builder.WithHeader("Content-Type", "text/plain")
126-
builder.WithHeader("X-Reader-Google-Bad-Token", "true")
127-
builder.WithBody("Unauthorized")
128-
builder.Write()
120+
func sendUnauthorizedResponse(w http.ResponseWriter) {
121+
w.Header().Set("Content-Type", "text/plain")
122+
w.Header().Set("X-Reader-Google-Bad-Token", "true")
123+
w.WriteHeader(http.StatusUnauthorized)
124+
w.Write([]byte("Unauthorized"))
129125
}
130126

131-
// OK sends a ok response to the client.
132-
func OK(w http.ResponseWriter, r *http.Request) {
133-
builder := response.New(w, r)
134-
builder.WithStatus(http.StatusOK)
135-
builder.WithHeader("Content-Type", "text/plain")
136-
builder.WithBody("OK")
137-
builder.Write()
127+
func sendOkayResponse(w http.ResponseWriter) {
128+
w.Header().Set("Content-Type", "text/plain")
129+
w.WriteHeader(http.StatusOK)
130+
w.Write([]byte("OK"))
138131
}

0 commit comments

Comments
 (0)