aboutsummaryrefslogtreecommitdiffhomepage
path: root/api
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-09-03 14:26:40 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-09-03 14:26:40 -0700
commiteee1f3190355224eef63a4dcdef8c36eb3ca3738 (patch)
tree009b7ca67b96d9be473d8ddf2c8c95f22a6749d1 /api
parent88e81d4d800ff6433518522954197d75203a25c2 (diff)
Refactor HTTP context handling
Diffstat (limited to 'api')
-rw-r--r--api/category.go13
-rw-r--r--api/entry.go16
-rw-r--r--api/feed.go17
-rw-r--r--api/icon.go3
-rw-r--r--api/opml.go6
-rw-r--r--api/user.go26
6 files changed, 29 insertions, 52 deletions
diff --git a/api/category.go b/api/category.go
index 5a57865..e74aa3b 100644
--- a/api/category.go
+++ b/api/category.go
@@ -8,7 +8,6 @@ import (
"errors"
"net/http"
- "miniflux.app/http/context"
"miniflux.app/http/request"
"miniflux.app/http/response/json"
)
@@ -21,8 +20,7 @@ func (c *Controller) CreateCategory(w http.ResponseWriter, r *http.Request) {
return
}
- ctx := context.New(r)
- userID := ctx.UserID()
+ userID := request.UserID(r)
category.UserID = userID
if err := category.ValidateCategoryCreation(); err != nil {
json.BadRequest(w, err)
@@ -57,8 +55,7 @@ func (c *Controller) UpdateCategory(w http.ResponseWriter, r *http.Request) {
return
}
- ctx := context.New(r)
- category.UserID = ctx.UserID()
+ category.UserID = request.UserID(r)
category.ID = categoryID
if err := category.ValidateCategoryModification(); err != nil {
json.BadRequest(w, err)
@@ -76,8 +73,7 @@ func (c *Controller) UpdateCategory(w http.ResponseWriter, r *http.Request) {
// GetCategories is the API handler to get a list of categories for a given user.
func (c *Controller) GetCategories(w http.ResponseWriter, r *http.Request) {
- ctx := context.New(r)
- categories, err := c.store.Categories(ctx.UserID())
+ categories, err := c.store.Categories(request.UserID(r))
if err != nil {
json.ServerError(w, err)
return
@@ -88,8 +84,7 @@ func (c *Controller) GetCategories(w http.ResponseWriter, r *http.Request) {
// RemoveCategory is the API handler to remove a category.
func (c *Controller) RemoveCategory(w http.ResponseWriter, r *http.Request) {
- ctx := context.New(r)
- userID := ctx.UserID()
+ userID := request.UserID(r)
categoryID, err := request.IntParam(r, "categoryID")
if err != nil {
json.BadRequest(w, err)
diff --git a/api/entry.go b/api/entry.go
index 37917c7..7f87888 100644
--- a/api/entry.go
+++ b/api/entry.go
@@ -9,7 +9,6 @@ import (
"net/http"
"time"
- "miniflux.app/http/context"
"miniflux.app/http/request"
"miniflux.app/http/response/json"
"miniflux.app/model"
@@ -30,10 +29,7 @@ func (c *Controller) GetFeedEntry(w http.ResponseWriter, r *http.Request) {
return
}
- ctx := context.New(r)
- userID := ctx.UserID()
-
- builder := c.store.NewEntryQueryBuilder(userID)
+ builder := c.store.NewEntryQueryBuilder(request.UserID(r))
builder.WithFeedID(feedID)
builder.WithEntryID(entryID)
@@ -59,7 +55,7 @@ func (c *Controller) GetEntry(w http.ResponseWriter, r *http.Request) {
return
}
- builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
+ builder := c.store.NewEntryQueryBuilder(request.UserID(r))
builder.WithEntryID(entryID)
entry, err := builder.GetEntry()
@@ -111,7 +107,7 @@ func (c *Controller) GetFeedEntries(w http.ResponseWriter, r *http.Request) {
return
}
- builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
+ builder := c.store.NewEntryQueryBuilder(request.UserID(r))
builder.WithFeedID(feedID)
builder.WithStatus(status)
builder.WithOrder(order)
@@ -164,7 +160,7 @@ func (c *Controller) GetEntries(w http.ResponseWriter, r *http.Request) {
return
}
- builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
+ builder := c.store.NewEntryQueryBuilder(request.UserID(r))
builder.WithStatus(status)
builder.WithOrder(order)
builder.WithDirection(direction)
@@ -200,7 +196,7 @@ func (c *Controller) SetEntryStatus(w http.ResponseWriter, r *http.Request) {
return
}
- if err := c.store.SetEntriesStatus(context.New(r).UserID(), entryIDs, status); err != nil {
+ if err := c.store.SetEntriesStatus(request.UserID(r), entryIDs, status); err != nil {
json.ServerError(w, err)
return
}
@@ -216,7 +212,7 @@ func (c *Controller) ToggleBookmark(w http.ResponseWriter, r *http.Request) {
return
}
- if err := c.store.ToggleBookmark(context.New(r).UserID(), entryID); err != nil {
+ if err := c.store.ToggleBookmark(request.UserID(r), entryID); err != nil {
json.ServerError(w, err)
return
}
diff --git a/api/feed.go b/api/feed.go
index a295451..e303f58 100644
--- a/api/feed.go
+++ b/api/feed.go
@@ -8,7 +8,6 @@ import (
"errors"
"net/http"
- "miniflux.app/http/context"
"miniflux.app/http/request"
"miniflux.app/http/response/json"
)
@@ -31,8 +30,7 @@ func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) {
return
}
- ctx := context.New(r)
- userID := ctx.UserID()
+ userID := request.UserID(r)
if c.store.FeedURLExists(userID, feedInfo.FeedURL) {
json.BadRequest(w, errors.New("This feed_url already exists"))
@@ -72,8 +70,7 @@ func (c *Controller) RefreshFeed(w http.ResponseWriter, r *http.Request) {
return
}
- ctx := context.New(r)
- userID := ctx.UserID()
+ userID := request.UserID(r)
if !c.store.FeedExists(userID, feedID) {
json.NotFound(w, errors.New("Unable to find this feed"))
@@ -103,8 +100,7 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) {
return
}
- ctx := context.New(r)
- userID := ctx.UserID()
+ userID := request.UserID(r)
originalFeed, err := c.store.FeedByID(userID, feedID)
if err != nil {
@@ -140,7 +136,7 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) {
// GetFeeds is the API handler that get all feeds that belongs to the given user.
func (c *Controller) GetFeeds(w http.ResponseWriter, r *http.Request) {
- feeds, err := c.store.Feeds(context.New(r).UserID())
+ feeds, err := c.store.Feeds(request.UserID(r))
if err != nil {
json.ServerError(w, err)
return
@@ -157,7 +153,7 @@ func (c *Controller) GetFeed(w http.ResponseWriter, r *http.Request) {
return
}
- feed, err := c.store.FeedByID(context.New(r).UserID(), feedID)
+ feed, err := c.store.FeedByID(request.UserID(r), feedID)
if err != nil {
json.ServerError(w, err)
return
@@ -179,8 +175,7 @@ func (c *Controller) RemoveFeed(w http.ResponseWriter, r *http.Request) {
return
}
- ctx := context.New(r)
- userID := ctx.UserID()
+ userID := request.UserID(r)
if !c.store.FeedExists(userID, feedID) {
json.NotFound(w, errors.New("Feed not found"))
diff --git a/api/icon.go b/api/icon.go
index 0ad1081..f9c2964 100644
--- a/api/icon.go
+++ b/api/icon.go
@@ -8,7 +8,6 @@ import (
"errors"
"net/http"
- "miniflux.app/http/context"
"miniflux.app/http/request"
"miniflux.app/http/response/json"
)
@@ -26,7 +25,7 @@ func (c *Controller) FeedIcon(w http.ResponseWriter, r *http.Request) {
return
}
- icon, err := c.store.IconByFeedID(context.New(r).UserID(), feedID)
+ icon, err := c.store.IconByFeedID(request.UserID(r), feedID)
if err != nil {
json.ServerError(w, err)
return
diff --git a/api/opml.go b/api/opml.go
index 8554dc1..eb214f3 100644
--- a/api/opml.go
+++ b/api/opml.go
@@ -7,7 +7,7 @@ package api // import "miniflux.app/api"
import (
"net/http"
- "miniflux.app/http/context"
+ "miniflux.app/http/request"
"miniflux.app/http/response/json"
"miniflux.app/http/response/xml"
"miniflux.app/reader/opml"
@@ -16,7 +16,7 @@ import (
// Export is the API handler that export feeds to OPML.
func (c *Controller) Export(w http.ResponseWriter, r *http.Request) {
opmlHandler := opml.NewHandler(c.store)
- opml, err := opmlHandler.Export(context.New(r).UserID())
+ opml, err := opmlHandler.Export(request.UserID(r))
if err != nil {
json.ServerError(w, err)
return
@@ -28,7 +28,7 @@ func (c *Controller) Export(w http.ResponseWriter, r *http.Request) {
// Import is the API handler that import an OPML file.
func (c *Controller) Import(w http.ResponseWriter, r *http.Request) {
opmlHandler := opml.NewHandler(c.store)
- err := opmlHandler.Import(context.New(r).UserID(), r.Body)
+ err := opmlHandler.Import(request.UserID(r), r.Body)
defer r.Body.Close()
if err != nil {
json.ServerError(w, err)
diff --git a/api/user.go b/api/user.go
index 5ad056e..167fd72 100644
--- a/api/user.go
+++ b/api/user.go
@@ -8,15 +8,13 @@ import (
"errors"
"net/http"
- "miniflux.app/http/context"
"miniflux.app/http/request"
"miniflux.app/http/response/json"
)
// CurrentUser is the API handler to retrieve the authenticated user.
func (c *Controller) CurrentUser(w http.ResponseWriter, r *http.Request) {
- ctx := context.New(r)
- user, err := c.store.UserByID(ctx.UserID())
+ user, err := c.store.UserByID(request.UserID(r))
if err != nil {
json.ServerError(w, err)
return
@@ -27,8 +25,7 @@ func (c *Controller) CurrentUser(w http.ResponseWriter, r *http.Request) {
// CreateUser is the API handler to create a new user.
func (c *Controller) CreateUser(w http.ResponseWriter, r *http.Request) {
- ctx := context.New(r)
- if !ctx.IsAdminUser() {
+ if !request.IsAdminUser(r) {
json.Forbidden(w)
return
}
@@ -61,8 +58,7 @@ func (c *Controller) CreateUser(w http.ResponseWriter, r *http.Request) {
// UpdateUser is the API handler to update the given user.
func (c *Controller) UpdateUser(w http.ResponseWriter, r *http.Request) {
- ctx := context.New(r)
- if !ctx.IsAdminUser() {
+ if !request.IsAdminUser(r) {
json.Forbidden(w)
return
}
@@ -106,8 +102,7 @@ func (c *Controller) UpdateUser(w http.ResponseWriter, r *http.Request) {
// Users is the API handler to get the list of users.
func (c *Controller) Users(w http.ResponseWriter, r *http.Request) {
- ctx := context.New(r)
- if !ctx.IsAdminUser() {
+ if !request.IsAdminUser(r) {
json.Forbidden(w)
return
}
@@ -118,14 +113,13 @@ func (c *Controller) Users(w http.ResponseWriter, r *http.Request) {
return
}
- users.UseTimezone(ctx.UserTimezone())
+ users.UseTimezone(request.UserTimezone(r))
json.OK(w, r, users)
}
// UserByID is the API handler to fetch the given user by the ID.
func (c *Controller) UserByID(w http.ResponseWriter, r *http.Request) {
- ctx := context.New(r)
- if !ctx.IsAdminUser() {
+ if !request.IsAdminUser(r) {
json.Forbidden(w)
return
}
@@ -147,14 +141,13 @@ func (c *Controller) UserByID(w http.ResponseWriter, r *http.Request) {
return
}
- user.UseTimezone(ctx.UserTimezone())
+ user.UseTimezone(request.UserTimezone(r))
json.OK(w, r, user)
}
// UserByUsername is the API handler to fetch the given user by the username.
func (c *Controller) UserByUsername(w http.ResponseWriter, r *http.Request) {
- ctx := context.New(r)
- if !ctx.IsAdminUser() {
+ if !request.IsAdminUser(r) {
json.Forbidden(w)
return
}
@@ -176,8 +169,7 @@ func (c *Controller) UserByUsername(w http.ResponseWriter, r *http.Request) {
// RemoveUser is the API handler to remove an existing user.
func (c *Controller) RemoveUser(w http.ResponseWriter, r *http.Request) {
- ctx := context.New(r)
- if !ctx.IsAdminUser() {
+ if !request.IsAdminUser(r) {
json.Forbidden(w)
return
}